Happy Memorial Day to those in the U.S!

In the last chapter we created some backdoor, moderator apis for managing the inventory of our credits system! If you want to get caught up on this series, checkout Part 1 here, Part 2 here, Part 3 here and Part 4 here.

Today we are going to leverage our backdoor apis to bootstrap our inventory. Then we’re going to write mutations to buy credits and then in the next chapter we’ll write a mutation to buy something with our credits.

Just like the last post, I’m going to write code and write this post at the same time!

First let’s add a Sku for a credit:

Now let’s add a Pricing Table:

Okay now we need to write some business logic to take money and turn it into credits. Now, this credits system isn’t going to deal itself with actually processing payments. So for our use case, we’ll be operating under a couple assumptions:

Payments are processed by another service All payments are successful

We’re going to mock this experience out when the time comes.

Going back to our schema, this is what we are trying to implement:

Now I’m going to write out the shell of the resolver:

Purchasing in the system is probably our most complex thing to implement.

First we need to check if the Skus we are trying to purchase actually exists:

Let’s see it fail:

Let’s see it not fail:

Second, we need to change our types structure to create a type for a payment method input. We’ll accept a token or a cardId to sort of mimic of stripe processes payments.

Let’s see this fail:

and now success:

Next, from our skus, let’s calculate the total price in US_CENTS that we’ll be passing to our payment service.

Next we’ll send the payment data to the payment service, and since we’re just mocking it out, it’ll come back successful.

I’m gonna assume I have a GraphQL binding to a payment service and pass it through our context:

Then i’ll add that to our purchase resolver:

Now that we know that the payment was successful, we can create finalized transaction that can be fetched to display a ledger in a user interface.

Last, let’s increment the user’s account balance.

We check to see if the user has a credit account if not bootstrap it and return the balance. Or update it.

Nice our balance is updated!

Alright that’s it for today! see ya next time!