

# Complete checkout with your payment service provider

After the buyer approves the payment intent, start the payment service provider checkout and show it to the buyer on your web page.

## Step 1: Initiate the payment intent
<a name="payments-agentic-commerce-checkout-initiate"></a>

From your checkout backend, call `InitiatePaymentIntent`. AgentCore payments retrieves your payment service provider credentials from AgentCore Identity and starts a checkout with the payment service provider. It returns the data that your page needs in `partnerIntentData`, and the status becomes `INITIATED`.

```python
# payments and payment_intent_id come from "Create a payment intent".
initiated = payments.initiate_payment_intent(
    userId="user-123",
    paymentManagerArn="<paymentManagerArn>",
    paymentIntentId=payment_intent_id,
)

partner_intent_data = initiated["partnerIntentData"]
```

In this example, the Python variable `partner_intent_data` holds the `partnerIntentData` field of the response.

 `InitiatePaymentIntent` succeeds only when the buyer approved the current version of the payment intent and the approval hasn’t expired. Otherwise, it returns a `ConflictException`. For the time limits, see [Approval statuses and time limits](payments-buyer-approval.md#payments-buyer-approval-status). A retry of a successful call returns the stored result, even after the approval expires.

## Step 2: Show the payment service provider checkout on your page
<a name="payments-agentic-commerce-checkout-page"></a>

Pass `partnerIntentData` to your web page, and use your payment service provider’s own frontend. Payment details go only to the payment service provider.

**Example**  
AgentCore payments builds the Amazon Pay checkout session payload and signs it as your Solution Provider. `partnerIntentData` contains the following keys:  
+  `merchantId`: your Amazon Pay merchant ID.
+  `publicKeyId`: the ID of the Solution Provider public key that signed the payload.
+  `ledgerCurrency`: the ledger currency of your merchant account, for example `USD`.
+  `sandbox`: the JSON boolean `true` for a `SANDBOX` provider, otherwise `false`.
+  `productType`: `PayOnly`.
+  `placement`: `Checkout`. You can change this value on your page if you show the button somewhere else.
+  `buttonPayload`: the signed checkout session payload, as a JSON string.
+  `signature`: the signature of `buttonPayload`.
+  `algorithm`: the signing algorithm, `AMZN-PAY-RSASSA-PSS-V2`.
Don’t change `buttonPayload`. Amazon Pay checks the signature against the exact payload, and rejects the checkout if the payload changed.  
For `LIVE` payments, serve the page from an origin that is in the **JavaScript origins** of your store in Seller Central. You don’t need to add origins for `SANDBOX` testing. For more information, see [Set up Amazon Pay](payments-amazon-pay.md#payments-amazon-pay-prerequisites). Render the button with the Amazon Pay JavaScript library. The following example uses the `NA` script. For the `EU` script URL, see [Add the Amazon Pay button](https://developer.amazon.com/docs/amazon-pay-checkout/add-the-amazon-pay-button.html) in the Amazon Pay documentation.  

```html
<div id="AmazonPayButton"></div>
<script src="https://static-na.payments-amazon.com/checkout.js"></script>
<script>
  // "partnerIntentData" holds the value that your server returned from InitiatePaymentIntent.
  amazon.Pay.renderButton('#AmazonPayButton', {
    merchantId: partnerIntentData.merchantId,
    publicKeyId: partnerIntentData.publicKeyId,
    ledgerCurrency: partnerIntentData.ledgerCurrency,
    sandbox: partnerIntentData.sandbox,
    productType: partnerIntentData.productType,
    placement: partnerIntentData.placement,
    createCheckoutSessionConfig: {
      payloadJSON: partnerIntentData.buttonPayload,
      signature: partnerIntentData.signature,
      publicKeyId: partnerIntentData.publicKeyId,
      algorithm: partnerIntentData.algorithm
    }
  });
</script>
```
When the buyer completes checkout, Amazon Pay redirects the browser to your `returnUrl` and adds an `amazonCheckoutSessionId` query parameter, for example `https://shop.example.com/checkout/amazon-pay/return?amazonCheckoutSessionId=EXAMPLE-SESSION-ID`. You pass this value to `AuthorizePaymentIntent`.  
If the buyer cancels on the Amazon Pay payment selection page, Amazon Pay returns the browser to the page that showed the button. AgentCore payments doesn’t set a separate cancel URL. No funds move, and the payment intent stays `INITIATED`. To let the buyer try again, show the button again. To end the checkout, call `CancelPaymentIntent`.
AgentCore payments finds or creates a Stripe Customer for the payment intent’s `userId`, and then creates a Stripe PaymentIntent. `partnerIntentData` contains the following keys:  
+  `clientSecret`: the Stripe PaymentIntent client secret.
+  `publishableKey`: your Stripe publishable key. This key is present only if you added a publishable key to the credential provider. For more information, see [Set up Stripe](payments-stripe.md).
Treat `clientSecret` as sensitive. Send it only to the buyer’s browser.
On your web page, confirm the payment with Stripe.js and the Payment Element, using `publishableKey` and `clientSecret`. For more information, see [Payment Element](https://docs.stripe.com/payments/payment-element) in the Stripe documentation.  

```javascript
// "partnerIntentData" holds the value that your server returned from InitiatePaymentIntent.
// If your credential provider has no publishable key, load the publishable key from your own configuration.
const stripe = Stripe(partnerIntentData.publishableKey ?? STRIPE_PUBLISHABLE_KEY);
const elements = stripe.elements({ clientSecret: partnerIntentData.clientSecret });
elements.create('payment').mount('#payment-element');

document.querySelector('#pay').addEventListener('click', async () => {
  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: { return_url: 'https://shop.example.com/checkout/complete' },
    redirect: 'if_required',
  });
  if (error) {
    showError(error.message);
    return;
  }
  // Tell your backend to call AuthorizePaymentIntent.
  await fetch('/checkout/authorize', { method: 'POST' });
});
```
Pass a `return_url` to `confirmPayment`. AgentCore payments turns on automatic payment methods for the Stripe PaymentIntent, so Stripe requires a `return_url`. With `redirect: 'if_required'`, Stripe redirects the buyer only for payment methods that need a redirect. If Stripe redirects the buyer, `confirmPayment` doesn’t return on this page. In that case, call `AuthorizePaymentIntent` from your `return_url` page. Call `AuthorizePaymentIntent` only after the buyer confirms the payment. An earlier call returns a `ConflictException`.  
For a `SETUP` payment intent, `partnerIntentData` contains `setupCheckoutUrl` instead. For more information, see [Save a payment method for later charges](payments-agentic-commerce-save-payment-method.md).
AgentCore payments creates a PayPal Orders v2 order. `partnerIntentData` contains its `orderId`.  

1. On your web page, load the PayPal JavaScript SDK with your own PayPal client ID. For more information, see [JavaScript SDK](https://developer.paypal.com/sdk/js/) in the PayPal documentation.

1. Render the PayPal button, and return `orderId` from its `createOrder` callback.

1. In the button’s `onApprove` callback, tell your backend to call `AuthorizePaymentIntent`.
The buyer signs in to PayPal and approves the order. You don’t need to save any value from PayPal for the next step. `AuthorizePaymentIntent` takes only the payment intent ID, and AgentCore payments finds the PayPal order from the payment intent.

Next, [authorize the payment](payments-agentic-commerce-authorize-capture.md).