

# 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
<a name="payments-agentic-commerce-test-sandbox-prereqs"></a>
+ 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](payments-buyer-approval.md).
+ A sandbox account for each payment service provider that you test:


<table>
<thead>
  <tr><th>Payment service provider</th><th>What you need</th></tr>
</thead>
<tbody>
  <tr><td>Stripe</td><td>A test mode restricted API key (<code>rk_test_</code>), and its publishable key (<code>pk_test_</code>). For the access that the key needs, see <a href="payments-stripe.md">Set up Stripe</a>.</td></tr>
  <tr><td>PayPal</td><td>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 <a href="payments-paypal.md">Set up PayPal</a>.</td></tr>
  <tr><td>Amazon Pay</td><td>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 <b>Production</b> view of Seller Central, and then test in the <b>Sandbox</b> view of the same account. For which values to copy and which to ignore, see <a href="payments-amazon-pay.md#payments-amazon-pay-prerequisites">Get your Amazon Pay merchant details</a>. You don’t need to add your test page to the <b>JavaScript origins</b> of your store for sandbox testing. For more information, see <a href="payments-amazon-pay.md">Set up Amazon Pay</a>.</td></tr>
</tbody>
</table>


## Step 1: Create the payment resources
<a name="payments-agentic-commerce-test-sandbox-setup"></a>

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.

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

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

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

1. 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](payments-agentic-commerce-setup.md#payments-agentic-commerce-setup-console). Copy the Payment Manager ARN and each connector ID.

## Step 2: Run a test payment
<a name="payments-agentic-commerce-test-sandbox-flow"></a>

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

```python
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
<a name="payments-agentic-commerce-test-sandbox-pay"></a>

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**  

1. Mount the Stripe Payment Element with `publishableKey` and `clientSecret` from `partnerIntentData`. For a sample, see [Complete checkout with your payment service provider](payments-agentic-commerce-checkout.md).

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

1. Call `AuthorizePaymentIntent` with no `partnerCallbackResult`.

1. 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.

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

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

1. Call `AuthorizePaymentIntent` with no `partnerCallbackResult`.

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

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](payments-agentic-commerce-checkout.md).

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

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

1. 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
<a name="payments-agentic-commerce-test-sandbox-results"></a>


| 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](payments-agentic-commerce-save-payment-method.md).

## Clean up
<a name="payments-agentic-commerce-test-sandbox-cleanup"></a>

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
<a name="payments-agentic-commerce-test-sandbox-eur"></a>

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
<a name="payments-agentic-commerce-test-sandbox-troubleshoot"></a>


| 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](payments-amazon-pay.md#payments-amazon-pay-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](payments-troubleshooting.md#payments-troubleshooting-checkout).