A *purchase case study: Process payments in your program

In this case study we will guide you through the process of creating a program that:

  1. Checks if a user has an active subscription to access some content of your program.
  2. Lets the user subscribe if they don’t have a subscription.
  3. Allow users to manage their subscription.

If your program runs on a web browser, you will need to create a merchant account on Braintree. If it is an app, payments will be managed either by Apple or Google. In this case study, however, we will assume you are just charging users on the web, and so will use Braintree, which has a developer sandbox that is very useful to test and debug your program.

Set up a subscription on Braintree

You can create a Braintree Sandbox account for testing purposes here. When you are ready to go live, follow this link to apply for a Braintree account.

After logging into either account, navigate to Subscriptions and then click on Plans. Once there, click on New Plan.

Follow the instructions on the screen to set it up:

  • You will use Plan ID to identify the plan in your GuidedTrack program.
  • Plan Name should be descriptive and easily readable, as it is the name displayed to your subscribers when they manage their subscription.

Now you are ready to integrate with GuidedTrack! Before you leave the Braintree site, make sure to find your Merchant ID, Public Key and Private Key.

Configure in-app purchases on your program

Go to your program Settings and select the Purchases tab:

Here you can see your three options for integrating purchases (Braintree, iOS or Android). Follow the instructions on the screen to configure your provider. In the case of Braintree, all you need to do is enter the Merchant ID, Public Key and Private Key that you have located on the Braintree website. 

Important: If you are using a Braintree Sandbox account to test your program, remember to come back here to update your credentials once you are ready to go live!

Write your GuidedTrack program

How you manage subscriptions inside your program is independent of the service you are using to process the payments. In other words,  the code in your program is the same regardless of the provider you use (Braintree, Apple or Google) .

Check the status of a subscription

Before serving the content of your program that requires payment, you will want to check if the user has an active subscription that allows them to access it. Here is the syntax you will use:

>>purchaseStatusError = 0
*purchase
	*status
	*success
		>>subscriptionPaid = it["paid"]
		>>subscriptionOngoing = it["ongoing"] 
		>>subscriptionExpiration = it["expiration"]
	*error
		>>purchaseStatusError = 1 
		>>purchaseStatusErrorText = it["message"]

If the user of your program does not have an active subscription and never had it before, there will be an error with the message “no purchase found”.

Otherwise, the association it will have three keys:

  • paid: will take “yes” and “no” for values.
  • ongoing: “yes” when the subscription is set to auto renew and “no” when it is not.
  • expiration: Date when the subscription expires. It is in text format, use the program date format parser – public to convert it into a date variable if needed.

Subscribe a user

Use this syntax so that a user can subscribe:

*purchase: trialPlan
	*frequency: recurring
	*success
		>>userSubscribed = 1
	*error
		>>userSubscribed = 0

The name of the purchase (in the example, trialPlan) must match the Plan ID that you created on Braintree.

Keep in mind that only logged-in users can subscribe. If they are not logged in, there will be an *error and the it object will be { “step” -> purchase, “details” -> { “code” -> unauthorized, “message” -> user not signed in } }. You can configure your program settings (Settings>Login) so that anonymous users are prompted to log in or register when using your program.

Tip: Follow this link to find credit card numbers you can use when testing against your Braintree Sandbox Merchant Account.

Allow users to manage their subscriptions

Use

*purchase
	*management

to open a new tab or window in your user’s browser so that they can manage their subscription.

Your user’s browser may keep the program from opening a new window, you can avoid it by putting the code inside a clickable component:

*component
	Click here to manage your subscription
	*click
		*purchase
			*management	

Managing your subscribers

Deleting a user’s data

If a user that has a subscription requests you to delete all their data, there are two places where to delete: GuidedTrack and Braintree. 

  • Have them access https://www.guidedtrack.com/account, logging in with the user/password they use to access your program. From there they can delete their GuidedTrack data.
  • Have them provide you the email address they use to log into your program. You will use that email to find their Customer ID on the Braintree Control Panel and delete their data.

If you are processing the payments via Google or Apple, users can delete their data themselves in the appropriate app for Android or iOS.