View a markdown version of this page

Test a payment service provider integration in sandbox mode - Amazon Bedrock AgentCore

Test a payment service provider integration in sandbox mode

Before you take live payments, run one test payment through each payment service provider that you plan to use. You set up the resources once, and then you run the same payment intent flow for each payment service provider. Every step uses sandbox or test mode credentials, so no real money moves.

Important
  • Use only sandbox or test mode credentials. Don’t use a payment auth with the LIVE environment, or a Stripe rk_live_ key.

  • Don’t share keys, client secrets, tokens, or approval URLs.

Prerequisites

  • An AWS account and credentials for the AWS Region that you test in.

  • Python 3.10 or later and the latest version of the AWS SDK for Python (Boto3). Earlier versions don’t include the payment intent operations. To upgrade, run pip install --upgrade boto3.

  • A web page that you serve over HTTPS, for the buyer approval widget and the payment service provider checkout. For local testing, you can use HTTP on localhost. For more information, see Get buyer approval for a payment intent.

  • A sandbox account for each payment service provider that you test:

    Payment service provider What you need

    Stripe

    A test mode restricted API key (rk_test_), and its publishable key (pk_test_). For the access that the key needs, see Set up Stripe.

    PayPal

    The client ID and client secret of a sandbox REST app, and a sandbox personal account to pay as the buyer. For more information, see Set up PayPal.

    Amazon Pay

    A registered merchant account, its merchant ID and store ID, an MWS authorization token for AgentCore payments, and a sandbox buyer account. Authorize AgentCore payments once in the Production view of Seller Central, and then test in the Sandbox view of the same account. For which values to copy and which to ignore, see Get your Amazon Pay merchant details. You don’t need to add your test page to the JavaScript origins of your store for sandbox testing. For more information, see Set up Amazon Pay.

Step 1: Create the payment resources

  1. Create a payment auth for each payment service provider. For PayPal and Amazon Pay, set the environment to SANDBOX. For Amazon Pay, also set the region to NA. Stripe has no environment setting, because the rk_test_ prefix sets test mode.

  2. Create one Payment Manager. To call the payment intent operations with your AWS credentials, for Inbound auth, choose Use IAM username.

  3. For Permissions, choose Create and use a new service role. The console gives the role read access to each payment auth that you choose.

  4. Add one connector for each payment service provider to the Payment Manager. For Payment type, choose Payment accounts for e-commerce.

  5. Wait until each connector status is READY. For Amazon Pay, connector creation also checks your MWS authorization token with Amazon Pay.

For the console steps, see Set up a payment service provider connector in the console. Copy the Payment Manager ARN and each connector ID.

Step 2: Run a test payment

The following script runs the same flow for every payment service provider. It stops at each step where the buyer acts in a browser.

import boto3 REGION = "<your-region>" # The AWS Region of your Payment Manager, for example us-east-1. TEST_ORIGIN = "<your-test-page-origin>" # The origin of your test page, for example https://localhost:8443. payments = boto3.client("bedrock-agentcore", region_name=REGION) MANAGER_ARN = "<paymentManagerArn>" CONNECTOR_ID = "<paymentConnectorId>" # Stripe, PayPal, or Amazon Pay USER_ID = "test-buyer-001" # Use the same user ID on every call for a payment intent. common = {"userId": USER_ID, "paymentManagerArn": MANAGER_ARN} # 1. Create the payment intent. intent = payments.create_payment_intent( **common, paymentConnectorId=CONNECTOR_ID, captureStrategy="INSTANT", # Or FUTURE. checkoutDetails={ # Example amount for the test payment. "totalAmount": {"value": "25.00", "currency": "USD"}, "returnUrl": f"{TEST_ORIGIN}/checkout/return", "merchant": {"name": "Example Test Store", "website": TEST_ORIGIN}, }, ) payment_intent_id = intent["paymentIntent"]["paymentIntentId"] common["paymentIntentId"] = payment_intent_id # 2. Get buyer approval. Show the widget on your test page, and choose Approve. # Pass the URL to the widget on your test page. Treat the URL as a secret, and don't log it. approval = payments.create_buyer_approval_url(**common, parentOrigin=TEST_ORIGIN) buyer_approval_url = approval["buyerApprovalUrl"] input("Approve in the widget, and then press Enter.") # 3. Start checkout with the payment service provider. initiated = payments.initiate_payment_intent(**common) partner_intent_data = initiated["partnerIntentData"] input("Pay at the payment service provider, and then press Enter.") # 4. Authorize. For Amazon Pay, paste the amazonCheckoutSessionId from the return URL. session_id = input("Amazon Pay only: paste amazonCheckoutSessionId, or press Enter to skip: ").strip() if session_id: authorized = payments.authorize_payment_intent(**common, partnerCallbackResult=session_id) else: authorized = payments.authorize_payment_intent(**common) print(authorized["paymentIntent"]["status"]) # INSTANT: CAPTURED. FUTURE: AUTHORIZED. # 5. For FUTURE only, capture the funds. # captured = payments.capture_payment_intent(**common)

After the buyer approves, call InitiatePaymentIntent and AuthorizePaymentIntent within 15 minutes. Otherwise, the calls fail, and you start again with a new payment intent.

Step 3: Pay at the payment service provider

Between InitiatePaymentIntent and AuthorizePaymentIntent, the buyer pays with the payment service provider’s own checkout. Use the following test steps for each payment service provider.

Example
Stripe
  1. Mount the Stripe Payment Element with publishableKey and clientSecret from partnerIntentData. For a sample, see Complete checkout with your payment service provider.

  2. Pay with the Stripe test card 4242 4242 4242 4242, any future expiry date, and any CVC.

  3. Call AuthorizePaymentIntent with no partnerCallbackResult.

  4. In the Stripe Dashboard, in test mode, check that the payment succeeded.

To test other payment methods, turn them on in the Stripe Dashboard. Methods that redirect the buyer, such as Klarna, return the buyer to the return URL that you set. Methods that settle later, such as ACH Direct Debit, leave the capture CAPTURE_PENDING. Call AuthorizePaymentIntent again later to refresh the status.

PayPal
  1. Render the PayPal button with your sandbox client ID, and return orderId from partnerIntentData in the createOrder callback.

  2. Sign in with your sandbox personal account, and approve the payment.

  3. Call AuthorizePaymentIntent with no partnerCallbackResult.

  4. In your sandbox merchant account, check the payment under Activity.

Amazon Pay
  1. Render the Amazon Pay button on your test page with the values in partnerIntentData. For a sample, see Complete checkout with your payment service provider.

  2. Choose the button, sign in as your sandbox buyer, and pay.

  3. Amazon Pay returns the buyer to your returnUrl with an amazonCheckoutSessionId query parameter. Call AuthorizePaymentIntent with this value as partnerCallbackResult.

  4. In Seller Central, in the Sandbox view, check the transaction.

Amazon Pay allows one capture of the full amount for a FUTURE payment intent.

Expected results

Payment service provider INSTANT FUTURE SETUP

Stripe

CAPTURED after AuthorizePaymentIntent

AUTHORIZED after AuthorizePaymentIntent, then CAPTURED after CapturePaymentIntent

SAVED after the buyer saves a payment method, then CAPTURED after CapturePaymentIntent

PayPal

CAPTURED after AuthorizePaymentIntent

AUTHORIZED after AuthorizePaymentIntent, then CAPTURED after CapturePaymentIntent

Not supported

Amazon Pay

CAPTURED after AuthorizePaymentIntent

AUTHORIZED after AuthorizePaymentIntent, then CAPTURED after CapturePaymentIntent

Not supported

To test SETUP with Stripe, see Save a payment method for later charges.

Clean up

After you test, call CancelPaymentIntent on each FUTURE payment intent that you didn’t capture, so that the payment service provider releases the test hold.

Test EUR payments

Payment intents support USD and EUR. To test EUR, set currency to EUR on totalAmount and on every other amount. Your payment service provider account must also accept EUR.

  • Stripe: a test mode account can charge in EUR. To test methods that require EUR, such as iDEAL or SEPA Direct Debit, turn them on in the Stripe Dashboard.

  • PayPal: in your sandbox merchant account, set Block payments to accept payments in a currency that you don’t hold. Otherwise, the capture waits until you accept each payment manually.

  • Amazon Pay: Amazon Pay charges in the ledger currency of the merchant account. Use a merchant account in the EU region, in its Sandbox view. Authorize the EU developer ID, and set the payment auth region to EU. On your page, load the EU Amazon Pay script.

A currency other than USD or EUR returns a ValidationException from CreatePaymentIntent.

Troubleshooting

Symptom Resolution

AuthorizePaymentIntent returns The buyer approval for this payment intent is no longer valid.

More than 15 minutes passed since the buyer approval. Create a new payment intent. For Stripe, if the buyer already confirmed the payment, AuthorizePaymentIntent still records the result.

InitiatePaymentIntent returns Buyer approval must be confirmed for payment intent version: <version>

The buyer didn’t finish the approval in the widget. Show the widget again, and choose Approve.

The widget doesn’t start.

The widget doesn’t run in browsers that automation software controls. Open your test page in your own browser.

Creating the Stripe payment auth fails with a message about a secret key.

Use a restricted key (rk_test_), not a secret key (sk_test_).

The Amazon Pay connector doesn’t reach READY.

Amazon Pay rejected the MWS authorization token. Authorize AgentCore payments again in Seller Central. Update the payment auth (credential provider) with the new token, and then create the connector again.

Seller Central doesn’t show MWS Authorizations.

Switch Seller Central to the Production view. A developer account that can use only the Sandbox view doesn’t have this section, so register a merchant account instead. For more information, see Get an Amazon Pay merchant account.

The Amazon Pay button doesn’t render.

Check that createCheckoutSessionConfig includes publicKeyId, and that sandbox is true for a SANDBOX provider. For LIVE payments, also check that the page origin is in the JavaScript origins of your store.

InitiatePaymentIntent returns a currency error from the payment service provider.

Your payment service provider account doesn’t accept the currency. Check the currency settings of your Stripe or PayPal account.

A payment auth is missing from the Payment auth list.

Choose the refresh icon next to Payment auth. Payment resources are Regional, so also check that the console is in the Region where you created the payment auth.

The connector shows IAM permissions not attached.

The service role didn’t get read access to the payment auth. Add the permissions to the role, or create the Payment Manager again with Create and use a new service role.

For more errors, see Agentic commerce payment errors.