Authorize, capture, or cancel a payment
After the buyer completes checkout with the payment service provider, authorize the payment. For a FUTURE payment, capture the funds later, or cancel to release them.
Authorize the payment
From your checkout backend, call AuthorizePaymentIntent. AgentCore payments confirms the payment with the payment service provider.
# payments and payment_intent_id come from "Create a payment intent". authorized = payments.authorize_payment_intent( userId="user-123", paymentManagerArn="<paymentManagerArn>", paymentIntentId=payment_intent_id, partnerCallbackResult="EXAMPLE-SESSION-ID", # Amazon Pay only ) status = authorized["paymentIntent"]["status"]
The result depends on the capture strategy:
-
INSTANT: the status becomesCAPTURED, and the response includes acapture. -
FUTURE: the status becomesAUTHORIZED. The payment service provider holds the funds, andpartnerAuthorizationReferenceidentifies the hold.
If the payment service provider accepted an INSTANT capture but hasn’t settled it, the capture status is CAPTURE_PENDING, and the payment intent stays INITIATED. For example, a Stripe ACH Direct Debit payment settles later. Call AuthorizePaymentIntent again later to refresh it.
Call AuthorizePaymentIntent within 15 minutes of the buyer approval. After that, it returns a ConflictException because the payment mandate expired. Stripe is the exception: if the buyer already confirmed the payment, AuthorizePaymentIntent still records the result after the 15 minutes.
AuthorizePaymentIntent is idempotent. If a call times out, retry it with the same clientToken.
Example
Capture the funds
For a FUTURE payment intent in AUTHORIZED status, call CapturePaymentIntent after you fulfill the order. To capture the remaining authorized amount, omit captureAmount.
captured = payments.capture_payment_intent( userId="user-123", paymentManagerArn="<paymentManagerArn>", paymentIntentId=payment_intent_id, clientToken="capture-order-1001-shipment-1-0000000000000000", # reuse to retry this capture )
After a partial capture, the status is PARTIALLY_CAPTURED. After the full amount is captured, the status is CAPTURED. To retry a capture, reuse its clientToken. A new clientToken creates a new capture.
The capture rules depend on the payment service provider:
-
Amazon Pay: one final capture for each charge. Omit
captureAmount. A partial capture returns aValidationException. -
Stripe: to capture part of the hold, pass
captureAmount. More than one capture for a payment requires Stripe multicapture on your Stripe account. -
PayPal: more than one partial capture. Pass
captureAmountfor each partial capture, or omit it to capture the rest of the hold.
Amazon Pay and PayPal can accept a capture without settling it. The capture status is then CAPTURE_PENDING, and it isn’t counted in capturedAmount. Don’t retry with a new clientToken. Call GetPaymentIntent later to see the final capture status.
Cancel the payment
To release an authorization or abandon a payment intent that you haven’t captured, call CancelPaymentIntent. The status becomes CANCELLED. For an INSTANT or FUTURE payment intent, you can cancel only in CREATED, INITIATED, or AUTHORIZED status. For any other status, including PARTIALLY_CAPTURED, CancelPaymentIntent returns a ConflictException. A SETUP payment intent can also be cancelled in SAVE_PENDING or SAVED status. A CREATED payment intent is cancelled without a call to the payment service provider.
Example
For Amazon Pay and PayPal, CancelPaymentIntent also returns a ConflictException within 2 minutes of an AuthorizePaymentIntent attempt, because the authorization might still complete. Wait, and then retry.
To cancel a SETUP payment intent, see Save a payment method for later charges.
Errors
-
If the payment service provider declines the payment,
AuthorizePaymentIntentreturns aValidationException. -
An
AuthorizePaymentIntentcall while the payment service provider is still processing the payment returns aConflictException. Retry later with the sameclientToken. -
An explicit deny from a resource control policy or a service control policy returns an
AccessDeniedException(HTTP 403) that names the policy type.
For error messages and fixes, see Agentic commerce payment errors.