Configure payment gateways
Accept Payments from Users
Each payment gateway has its own method for handling payments and refunds. The Platfone Firebase Backend provides a unified interface for managing payments and refunds from multiple payment gateways. It includes support for popular gateways and can be extended to support additional ones.
Supported Payment Gateways
The Platfone Firebase Backend currently supports the following payment gateways:
How It Works
When a user makes a payment, the backend verifies the transaction and adds a document to the payments collection in the Firestore database. This document contains payment information such as the unique payment ID, Firebase user ID, payment gateway, payment amount, and payment status. Each update to a Firestore document triggers a function that synchronizes the balance with Platfone Retail API and marks the document as processed. A positive amount increases the balance, while a negative amount decreases it (for refunds).
Stripe
The Platfone Firebase Backend supports payments via the Stripe payment gateway.
Basic Process Overview:
- The app requests a payment intent from the Firebase Cloud Function.
- The app sends the payment intent to Stripe.
- Stripe processes the payment and sends a webhook event to the Firebase Cloud Function.
- The payment is verified and added to the Firestore database with either a positive amount (for payments) or a negative amount (for refunds).
Configuration:
- Choose
Yeswhen prompted by the installer to enable Stripe. - Copy the Stripe webhook URL as printed by the installer.
- Set up the Stripe webhook:
- Go to the Stripe Dashboard and add a new endpoint.
- Set the endpoint URL to the copied Stripe URL from the installer.
- Select the
payment_intent.amount_capturable_updated,charge.refundedandreview.closedevents, then click “Add endpoint” to save. - Click “Reveal” to get the signing secret, which you’ll set as the
STRIPE_WEBHOOK_SECRETduring the Platfone Firebase Backend installation.
- Get your Stripe API keys:
- Go to Stripe Dashboard to get your
Publishable keyandSecret key. You will be prompted for them during the backend installation.
- Go to Stripe Dashboard to get your
- For instructions on configuring Stripe payments in your app, refer to the example apps:
PayPal
The Platfone Firebase Backend supports payments via the PayPal Advanced Checkout integration.
Basic Process Overview:
Client-Side Integration:
- The app integrates PayPal’s JavaScript SDK to render PayPal buttons and card fields.
- Users interact with these components to initiate payments.
Server-Side Order Creation:
- The app sends a request to a Firebase Cloud Function to create an order.
- The Cloud Function utilizes PayPal’s Orders API to create the order and returns an
orderIDto the client.
Payment Approval:
- The client uses the
orderIDto approve the payment through PayPal’s SDK components.
- The client uses the
Order Capture:
- Upon approval, the client notifies the Firebase Cloud Function to capture the order.
- The Cloud Function captures the payment via PayPal’s Orders API and updates the Firestore database with the transaction details, recording positive amount.
Webhook Notification:
- PayPal sends a webhook
PAYMENT.CAPTURE.REFUNDEDevent to a designated Firebase Cloud Function upon payment refund. - The Cloud Function verifies the event and updates the Firestore database with the transaction details, recording negative amount.
- PayPal sends a webhook
Configuration:
Enable PayPal Integration:
- During the backend installation, choose
Yeswhen prompted to enable PayPal.
- During the backend installation, choose
Obtain PayPal API Credentials:
- Log in to the PayPal Developer Dashboard.
- Navigate to Apps & Credentials and create a new REST API app.
- Retrieve your
Client IDandSecret, which will be required during the backend installation.
Set Up Webhook:
- Copy the PayPal webhook URL provided by the installer.
- In the PayPal Developer Dashboard, go to Webhooks under your application settings.
- Add a new webhook endpoint with the copied URL.
- Subscribe to the following events:
- “Payment capture refunded”
PAYMENT.CAPTURE.REFUNDED(for refunds)
- “Payment capture refunded”
- Save the webhook configuration.
Cryptomus
The Platfone Firebase Backend supports payments via the Cryptomus cryptocurrency payment gateway.
Basic Process Overview:
Client-Side Integration:
- The app integrates Cryptomus’s payment interface to facilitate cryptocurrency transactions.
- Users initiate payments through the app, selecting their preferred cryptocurrency.
Server-Side Invoice Creation:
- The app sends a request to a Firebase Cloud Function to create a payment invoice.
- The Cloud Function utilizes Cryptomus’s API to generate the invoice and returns the payment details to the client.
Payment Processing:
- Users complete the payment using the provided cryptocurrency details.
- Cryptomus processes the payment and confirms the transaction.
Webhook Notification:
- Cryptomus sends a webhook event to a designated Firebase Cloud Function upon payment completion.
- The Cloud Function verifies the payment and updates the Firestore database with the transaction details, recording positive amounts for payments and negative amounts for refunds.
Configuration:
Enable Cryptomus Integration:
- During the backend installation, choose
Yeswhen prompted to enable Cryptomus.
- During the backend installation, choose
Obtain Cryptomus API Credentials:
- Sign up on the Cryptomus website.
- Create a new merchant project to manage your payments.
- Generate your API keys from the merchant dashboard; these will be required during the backend installation.
Set Up Webhook:
- Copy the Cryptomus webhook URL provided by the installer.
- In your Cryptomus merchant dashboard, navigate to the webhook settings.
- Add a new webhook endpoint with the copied URL.
- Configure the webhook to listen for relevant events, such as payment completions and refunds.
- Save the webhook configuration.
Client-Side Integration:
- Integrate Cryptomus’s payment interface into your app to facilitate cryptocurrency transactions.
- Ensure the client-side logic handles the initiation of payments and interacts with the Firebase Cloud Functions as needed.
For a visual walkthrough, you might find the following video helpful: https://www.youtube.com/watch?v=x71wO2kDRiI
Apple App Store
The Platfone Firebase Backend supports in-app purchases through the Apple App Store.
Basic Process Overview:
- The payment transaction provided by the app is verified and added to the Firestore database with a positive amount.
- When a
REFUNDApple App Store notification is received, it is verified and added to the Firestore database with a negative amount.
Configuration:
- Choose
Yeswhen prompted by the installer to enable Apple App Store in-app purchases. - Copy the Apple App Store webhook URL as printed by the installer.
- Set up the notification URL in App Store Connect:
- Go to App Store Connect.
- In the
App Informationsection, add the copied webhook URL as the notification URL.
- For instructions on configuring in-app purchases in your app, refer to the Apple in-app purchases section of the iOS example app README.
Google Play Store
The Platfone Firebase Backend supports in-app purchases through the Google Play Store.
Basic Process Overview:
- The payment transaction provided by the app is verified and added to the Firestore database with a positive amount.
- When a
ONE_TIME_PRODUCT_REFUNDEDGoogle Play Store notification is received, it is verified and added to the Firestore database with a negative amount.
Configuration:
Choose
Yeswhen prompted by the installer to enable Google Play in-app purchases.Go to the Google Play Console and select your app.
Configure products under
Products -> In-app products.Create a service account key:
- Go to the Google Cloud Console.
- Select the project linked to your Play Store app, then click
Create Service Account. - Enter a name and description for the service account (e.g., Platfone-firebase-backend), then click
Create. - Click
Create Keyand selectJSONas the key type. - Save the JSON file as
google-play-service-account.json.
Grant the service account access to app financial data:
- In the Google Play Console, go to
Users and permissionsand clickInvite new users. - Paste the email address from the service account JSON file and select
Financeas the role. - In the app permission tab, click
Add appand select your app. - Allow the service account to access the financial data of the app.
- Click
Invite user.
- In the Google Play Console, go to
Configure a Google Pub/Sub topic:
- Go to the Google Cloud Console.
- Click on
Create a topicand enter aTopic ID(a name for the topic). - Click
Create. - Choose
Subscriptionfor the created topic and clickEDIT. - Change
Delivery typetoPushand paste the webhook URL from the installer into theEndpoint URL. - Change the
Retry policytoRetry after exponential backoff delay. - Click
Update.
Grant the Google Play Store access to the Google Pub/Sub topic:
- In the Google Cloud Console, select the topic you created.
- Click on
ADD PRINCIPAL. - In the new principal field, enter
[email protected]. - Select the
RoleasPub/Sub Publisher.
Configure Google Play Store notifications:
- In the Google Play Console, select your app.
- Under the
Monetization setupsection inMonetize, checkEnable real-time notifications. - Fill in the
Topic namewith the name of the topic you created in the Google Cloud Console. - Set
Notification contentto includeSubscriptions, voided purchases, and all one-time products. - Click
Save changes.
Continue with the Platfone Firebase Backend installation and provide:
- The path to the service account key file (
google-play-service-account.json). - The Google Play package name of your app.
- JSON object with Product IDs and amount to be topped up. An example JSON is printed by the backend installer.
- The path to the service account key file (
Adding Custom Payment Gateways
To add a custom payment gateway, you need to:
Add a new function similar to existing in
purchases.tsthat will:Handle incoming payment notification from custom payment gateway
Verify the payment and its authenticity
Add a document to the
paymentscollection in Firestore (governed byFIRESTORE_PAYMENTS_COLLECTIONvariable) with at least the following fields:id: Unique ID for the payment. This can be the payment ID from the payment gateway.customer_id: Firebase UID of the user making the payment.amount: The amount of the payment in USD cents. Use a negative amount for refunds.gateway: The name of the payment gateway.is_synced: Should be set tofalse. A boolean flag to indicate if the payment has been synced with the Retail API.
Field constraints can be found in the Create customer transaction endpoint of the Retail API.
The function should be added to the
index.js.Add the necessary configuration variables to the
installer/src/params.yamlfile or.envfile.Re-run the installer to deploy the new function.