Accept an Affirm payment (2024)

Stripe users can use the Payment Intents API– a single integration path for creating payments using any supported method–to accept Affirm payments from customers in the following countries:

Accepting Affirm payments on your website consists of:

  • Creating an object to track a payment
  • Collecting payment method information
  • Submitting the payment to Stripe for processing
  • Handling the Affirm redirect and relevant webhook events
Set up StripeServer-side

First, create a Stripe account or sign in.

Use our official libraries to access the Stripe API from your application:

Command Line

# Available as a gemsudo gem install stripe

Gemfile

# If you use bundler, you can add this line to your Gemfilegem 'stripe'

Create a PaymentIntentServer-side

A PaymentIntent is an object that represents your intent to collect payment from a customer and tracks the lifecycle of the payment process through each stage.

First, create a PaymentIntent on your server and specify the amount to collect and the currency. If you already have an integration using the Payment Intents API, add affirm to the list of payment method types for your PaymentIntent.

You can manage payment methods from the Dashboard. Stripe handles the return of eligible payment methods based on factors such as the transaction’s amount, currency, and payment flow. To manually list the payment method, specify affirm in the payment_method_types instead.

curl https://api.stripe.com/v1/payment_intents \ -u

sk_test_4eC39HqLyjWDarjtT1zdp7dc

: \ -d amount=6000 \ -d currency=usd \ -d "payment_method_types[]"=affirm

Command Line

curl https://api.stripe.com/v1/payment_intents \ -u

sk_test_4eC39HqLyjWDarjtT1zdp7dc

: \ -d amount=6000 \ -d currency=usd \ -d "automatic_payment_methods[enabled]"="true" \

In the latest version of the API, specifying the automatic_payment_methods parameter is optional because Stripe enables its functionality by default.

Retrieve the client secretAccept an Affirm payment (3)

The PaymentIntent includes a client secret that the client side uses to securely complete the payment process. You can use different approaches to pass the client secret to the client side.

Retrieve the client secret from an endpoint on your server, using the browser’s fetch function. This approach is best if your client side is a single-page application, particularly one built with a modern frontend framework like React. Create the server endpoint that serves the client secret:

get '/secret' do intent = # ... Create or retrieve the PaymentIntent {client_secret: intent.client_secret}.to_jsonend

And then fetch the client secret with JavaScript on the client side:

(async () => { const response = await fetch('/secret'); const {client_secret: clientSecret} = await response.json(); // Render the form using the clientSecret})();

Collect payment method details and submitClient-side

When a customer clicks to pay with Affirm, we recommend you use Stripe.js to submit the payment to Stripe. Stripe.js is our foundational JavaScript library for building payment flows. It will automatically handle integration complexities, and enables you to easily extend your integration to other payment methods in the future.

Include the Stripe.js script on your checkout page by adding it to the head of your HTML file.

checkout.html

<head> <title>Checkout</title> <script src="https://js.stripe.com/v3/"></script></head>

Create an instance of Stripe.js with the following JavaScript on your checkout page.

script.js

// Set your publishable key. Remember to change this to your live publishable key in production!// See your keys here: https://dashboard.stripe.com/apikeysvar stripe = Stripe(

'pk_test_TYooMQauvdEDq54NiTphI7jx'

);

Rather than sending the entire PaymentIntent object to the client, use its client secret from Step 1. This is different from your API keys that authenticate Stripe API requests.

Handle the client secret carefully, because it can complete the charge. Don’t log it, embed it in URLs, or expose it to anyone but the customer.

Improve payment success rates with additional details

We recommend passing shipping and billing details to improve conversion rates, although these are not required.

This integration guide suggests passing the shipping and billing information on the client after the customer selects their payment method.

If you pass these fields, the shipping address should include valid data in line1, city, state, postal_code, and country. Similarly, billing details must include valid data in all of line1, city, state, postal_code, and country.

Confirm the PaymentIntent

Use stripe.confirmAffirmPayment to handle the redirect away from your page and to complete the payment. You must also pass a return_url to this function to indicate where Stripe redirects the user after they complete the payment on the Affirm website or mobile application.

On Affirm’s payments page, the customer selects the payment options available to them. See the overview page for more details. You can’t limit or pre-select payment options on the Affirm payments page—deferring this choice to the consumer maximizes their opportunity to transact with you.

script.js

// Redirects away from the clientstripe.confirmAffirmPayment( '{{PAYMENT_INTENT_CLIENT_SECRET}}', { payment_method: { // Billing information is optional but recommended to pass in. billing_details: { email: 'jenny@rosen.com', name: 'Jenny Rosen', address: { line1: '1234 Main Street', city: 'San Francisco', state: 'CA', country: 'US', postal_code: '94111', }, }, }, // Shipping information is optional but recommended to pass in. shipping: { name: 'Jenny Rosen', address: { line1: '1234 Main Street', city: 'San Francisco', state: 'CA', country: 'US', postal_code: '94111', }, }, // Return URL where the customer should be redirected after the authorization. return_url: 'https://example.com/checkout/complete', }).then(function(result) { if (result.error) { // Inform the customer that there was an error. console.log(result.error.message); }});

When your customer submits a payment, Stripe redirects them to the return_url and includes the following URL query parameters. The return page can use them to get the status of the PaymentIntent so it can display the payment status to the customer.

When you specify the return_url, you can also append your own query parameters for use on the return page.

ParameterDescription
payment_intentThe unique identifier for the PaymentIntent.
payment_intent_client_secretThe client secret of the PaymentIntent object.

When the customer is redirected back to your site, you can use the payment_intent_client_secret to query for the PaymentIntent and display the transaction status to your customer.

Test Affirm integration

Test your Affirm integration with your test API keys by viewing the redirect page. You can test the successful payment case by authenticating the payment on the redirect page. The PaymentIntent transitions from requires_action to succeeded. To test the case where the user fails to authenticate, use your test API keys and view the redirect page. On the redirect page, click X in the top left corner. The PaymentIntent will transition from requires_action to requires_payment_method.

Failed paymentsAccept an Affirm payment (10)

Affirm takes into account multiple factors when deciding to accept or decline a transaction (for example, the length of time buyer has used Affirm, the outstanding amount the customer has to repay, and the value of the current order).

Always present additional payment options such as card in your checkout flow, as Affirm payments have a higher rate of decline than many payment methods. In these cases, the PaymentMethod is detached and the PaymentIntent object’s status automatically transitions to requires_payment_method.

Other than a payment being declined, for an Affirm PaymentIntent with a status of requires_action, customers are expected to complete the payment within 12 hours after they are redirected to the Affirm site. If no action is taken after 12 hours, the PaymentMethod is detached and the PaymentIntent object’s status automatically transitions to requires_payment_method.

In these cases, inform your customer to try again with a different payment option presented in your checkout flow.

Error codesAccept an Affirm payment (11)

These are the common error codes and corresponding recommended actions:

Error codeRecommended action
invalid_amount_too_smallEnter an amount within Affirm’s default transaction limits, for the country.
invalid_amount_too_largeEnter an amount within Affirm’s default transaction limits, for the country.
missing_required_parameterCheck the error message for more information on the required parameter.
nonexistent_countryEnter a valid two-letter ISO country code for the country property in the shipping and billing details.
payment_intent_invalid_currencyEnter the appropriate currency. Affirm only supports payments in your local currency.
payment_intent_redirect_confirmation_without_return_urlProvide a return_url when confirming a PaymentIntent with Affirm.
payment_method_invalid_parameterCheck the error message for more information on the parameter.
Accept an Affirm payment (2024)
Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5950

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.