Prerequisites
- Ensure you have a PayPal business account approved for Expanded Checkout.
- Ensure to set up developer, sandbox, and production environment accounts.
- Ensure to complete the account provisioning process to enable ACH payments.
Load PayPal JavaScript SDK
PayPal’s JavaScript SDK provides the necessary pre-built tools to render the Pay with Bank (ACH) button on your webpage and handle payment authorization. Include the JavaScript SDK as a<script> tag in the HTML file that renders your webpage.
Load and use JavaScript SDK v6 along with v5
If you have an existing JavaScript SDK v5 integration and want to integrate JavaScript SDK v6 on the same page, add thedata-namespace attribute to your existing v5 <script> tag.
- You access the v5 SDK via
window.paypal_v5instead ofwindow.paypal. - JavaScript SDK v6 can attach to
window.paypalwithout naming collisions.
Create browser-safe client token
The browser-safe token is a client-side access token that authorizes an app to use the JavaScript SDK resources. A browser-safe token is not the same as a server-side access token. The server-side access token helps PayPal authenticate an app when the app accesses PayPal REST API resources.
/v1/oauth2/token endpoint and include the following:
- Encoded app credentials Client ID : Secret in Base64 format, in the Authorization header.
- Data parameters:
| Parameter | Action |
|---|---|
grant_type | Set the parameter value to client_credentials to specify that the app is requesting to exchange client ID and secret for an access token. |
response_type | Set the parameter value to client_token to request a client-side access token. |
access_token response parameter.
- Merchants
- Partners
Initialize JavaScript SDK and create SDK instance
In your client-side code, use thewindow.paypal.createInstance() method with the following options to create a PayPal SDK instance:
| Option | Action |
|---|---|
clientToken | Pass the browser-safe client token. |
components | Set to ["bank-ach-payments"] to load and render ACH components. |
partnerAttributionId [Partner-specific attribute] | Set to your BN code to identify as a PayPal partner. See BN code for more information. |
createBankAchOneTimePaymentSession() method for creating ACH payment sessions and the findEligibleMethods() method for checking payment method availability.
- Merchants
- Partners
Verify eligibility
In your client-side code, use the following methods to determine if you are eligible to offer ACH as a payment method:findEligibleMethods(): Returns all the eligible payment methods.isEligible(): Indicates if ACH is an eligible payment method.
- Merchants
- Partners
Call
Response: Contains an object with the
findEligibleMethods() with an input options object containing the following parameters:| Parameter | Action |
|---|---|
currencyCode | Set to the three-letter currency code. Example: USD. |
paymentFlow | Set to ONE_TIME_PAYMENT. JavaScript SDK retrieves all payment methods through which the merchant is eligible to accept one-time payments. |
amount | Set to the total order amount that includes item cost, applicable taxes, shipping costs, handling cost, insurance cost, and discounts if any. Example: 100.00. |
isEligible() method. Use the method to verify if the eligible payment methods include ACH.Render Pay with Bank button
After confirming eligibility, render the Pay with Bank - ACH button on your webpage:- Define the container for the ACH button: In the HTML file corresponding to the webpage where you want to render the button, include a container element.
-
Create payment session: Use
sdkInstance.createBankAchOneTimePaymentSession()to create a payment session and register theonApprove(),onCancel(),onComplete(), andonError()event handlers. -
Attach the onClick event handler and display the button: Use
addEventListener()to attach theonClickevent handler that triggers theonClickfunction when customers select the Pay with Bank - ACH button.
Create order and start authentication flow
Create anonClick event-handler function that:
- Starts the authentication flow.
- Creates an order and passes the order ID to JavaScript SDK.
- Merchants
- Partners
In your client-side code:
In your server-side code, include the code to:
- Include an event-handler function that is triggered when the user clicks the Pay with Bank - ACH button. The function calls the
createOrder()function, receives the order ID from the server-side code, passes it to the JavaScript SDK, and starts the authentication (account verification) flow.
JavaScript SDK uses the order ID to confirm the payment source as
bank.ach_debit to PayPal servers.- Include the
createOrder()function that constructs a payload, calls the server-side code to create an order, and passes the payload. Include the following parameters in the payload:
| Parameter | Action |
|---|---|
intent | Set to CAPTURE to capture payment immediately after approval. |
purchase_units[].amount.currency_code | Set to the three-letter currency code. Example: USD. |
purchase_units[].amount.value | Set to the total order amount that includes item cost, applicable taxes, shipping costs, handling cost, insurance cost, and discounts if any. Example: 100.00.If there is a mismatch between purchase_units[].amount.value and the amount specified in the findEligibleMethods() method, PayPal considers purchase_units[].amount.value as the total order amount. |
- Use a valid full-scope
Beareraccess token, make a POST call to the/v2/checkout/ordersendpoint, and pass the payload received from client-side. - Receive the response to the Create Order call. The response contains the order ID.
- Pass the order ID to the client-side code.
Handle events
Event handlers manage the different outcomes when your customers attempt to make ACH payments. In your code, create event-handler functions that handle customer approval for payment, payment cancellation, error scenarios, and payment capture completion.a. Handle approval and capture payment
In your client-side code:- Include the
onApprove()event-handler function that receives the order ID after the customer successfully verifies their bank account, calls thecaptureOrder()function, and passes the order ID. - Include the
captureOrder()function that:- Receives the order ID from the
onApprove()event. - Calls the server-side code to capture the payment.
- Receives the captured order details from the server-side code.
- Displays the transaction results to the customer.
- Receives the order ID from the
- Merchants
- Partners
In your server-side code, include the code to:
- Use a valid full-scope
Beareraccess token and make a POST call to the/v2/checkout/orders/{order_id}/captureendpoint with the order ID received from client-side as the path parameter. - Receive the captured order details from the PayPal server and pass them to the client-side code.
b. Handle cancellation
In your app code, include theonCancel() event-handler function that handles payment cancellation. Payment cancellation can occur when the customer cancels account verification or payment processing. JavaScript SDK passes the cancellation details including the order ID to the function’s data parameter.
c. Handle errors
In your app code, include theonError(data) event-handler function to process errors that occur during bank account verification or payment process. JavaScript SDK passes error message and error details to the function’s data parameter.
d. [Optional] Handle completion
In your app code, you can include theonComplete(data) event-handler function to perform necessary clean-up operations.
Test and go live
Test the end-to-end integration flow- Load your integration page and verify ACH eligibility checking works correctly.
- Click the Pay with Bank button to start the bank account authentication process.
- Verify the order is created successfully when the authentication flow begins as documented in the Create order and start authentication flow section.
-
Complete the authentication flow and verify the
onApprovecallback receives the order ID as documented in the Handle approval and capture payment section. When the bank selection popup appears, use the following sandbox test credentials:- Bank selection: Select Demo Bank from the list of available banks.
- Username:
pyplopenbankingubsb.site16441.2credential - Password:
site16441.2
- Confirm your server successfully captures the payment using the order ID as documented in the Handle approval and capture payment section.
- Verify the authentication flow starts correctly with order creation as documented in the Create order and start authentication flow section.
- Cancel the authentication process and verify the
onCancelcallback executes with the order ID as documented in the Handle cancellation section. - Test error scenarios and verify the
onErrorcallback handles failures appropriately as documented in the Handle errors section. - Confirm the
onCompletecallback executes when the flow finishes as documented in the Handle completion section. - Test idempotent retry logic for 5xx errors during order capture as documented in the Handle approval and capture payment section.
-
Switch from sandbox to production API endpoints:
- Change SDK URL from
https://www.sandbox.paypal.com/web-sdk/v6/coretohttps://www.paypal.com/web-sdk/v6/core. - Update API base URL from
https://api-m.sandbox.paypal.comtohttps://api-m.paypal.com.
- Change SDK URL from
-
Replace sandbox credentials with production credentials:
- Use your production client ID and secret.
- Ensure your production PayPal business account is approved for Expanded Checkout and ACH payments.
- Deploy and test your production integration to confirm everything works correctly.