Integrate with a payment provider
Stripe Prebuilt Checkout page
This guide will describe how to set up payment using the third-party payment service Stripe. As an example, we will use a one-time payment of multiple items (e.g. a user purchasing several items from a web store).
To complete this guide, you'll need a Stripe account. If you don't have that already, go to Stripe to register a new account. We'll use the Stripe test environment for this guide, but the setup would be the same for a production environment.
Step 1
Create your web store as an app in Appfarm. Make sure you have a data source with all the products that can be purchased (e.g. products), and one with all the products the user wants to purchase (i.e. "shopping cart" or "order items").
Furthermore, you'll need two landing views with the URL Path Key set; one for payment success and one for payment failure (e.g. https://[HOSTNAME].appfarm.app/[APPNAME]/success)
Step 2
Go to https://dashboard.stripe.com/test/products. For each product you want to sell, click add product. Then, copy the Pricing API ID (e.g. price_1LVDmlCKVzgiw1WewxzdGvQl) and save it to a property on the corresponding product object.
You may also add these products dynamically using the Stripe Products API.
Step 3
Go to https://dashboard.stripe.com/test/apikeys. Under "Secret key" click "Reveal test key". Copy the key, and save it as a secret under Appfarm Create -> Security > Secrets.
Step 4
Go to Appfarm Create -> App Data -> App Variables, and add a new Runtime Property named "URL".
Step 5
Go to your checkout in your app, and create a new action. Add the action node web request with the following properties:
Base URL:
https://api.stripe.comPath:
/v1/checkout/sessionsQuery Parameters:
success_url: Success URL from Step 1
cancel_url: Failure URL from Step 1
Mode: Payment
Request Headers:
Content-Type: application/x-www-form-urlencoded
Authorization:
Type: Basic Auth
Username: The test key from Step 3
Password:
Null
Method: POST
Body Type: Raw
Body Content: Function:
Content-Type: Custom
Result Mapping:
Data Source: App Variables
URL: url
Step 6
Add action node Open URL and set the Value to App Variables.URL. The payment flow should now work.
Step 7
You may now create the logic in Appfarm to process the status of the payment in Stripe (e.g. to handle a successful payment). This is done with an API endpoint in a Flow. Stripe sends the payment result to the endpoint, and the endpoint's action handles and saves the result.
Go to Flows and create a new Flow (or open an existing one). Give the Flow a root path: when you create an API Endpoints Flow you enter it as Root path, and it shows under the Flow name. Add a new endpoint under API Endpoints. In the endpoint's Endpoint config, set Methods to POST, note the Path, and use Body data to define a schema for the fields you need from Stripe's event. Then add the logic that handles the payment to the endpoint's action.
Here is an example setup of this endpoint, with mapping of incoming JSON to our internal data source Payment. The property Payment status is used in the action to handle whether the payment was successful or not ("paid" is received upon successful payment). The screenshot shows the same mapping on a Service endpoint, which older solutions may still use.

Step 8
Set up a new Role with permission to run the Flow endpoint, and add a Service Account with that role. Create an API key for the Service Account, and copy the key to your notepad. Give the key access to this Flow only, and set an expiry date.
Step 9
This step is for telling Stripe where to send the payment status. Go to https://dashboard.stripe.com/webhooks and add a new endpoint.
Under Endpoint URL, put the following URL: https://[HOSTNAME].appfarm.app/api/flows/[API root path]/[endpoint path]?token=[API key]
Under Select events to listen to add checkout.session.completed.
Testing
You can now test your application. If set up correctly, the payment should work as expected.
Last updated
Was this helpful?