Sessions and events
Learn how to create payment request sessions, attach event listeners, and handle the payment lifecycle with the Shop Pay Wallet API.
Anchor to Configure the APIConfigure the API
Use the shopId and clientId from your onboarding email to configure the API. For the full list of options, see configure parameters.
Anchor to Create a sessionCreate a session
Create a session to make a payment request. shop_id (integer) can be retrieved from the shop object in the Admin API.
PaymentRequest fields are defined here.
Anchor to Attach event listenersAttach event listeners
Use ShopPayPaymentRequestSessionCreate on your server to create a session.
Listen to events that may change calculations such as when a delivery method type, shipping address, delivery method, pickup location, pickup location filter, or discount code changes. Recalculate the payment request and update the session.
If you can't ship to the selected address, then return a shippingAddressError. Refer to Handle errors.
Confirm the payment once the user clicks the Pay now button in the Shop Pay popup.
The server confirmation must invoke the ShopPayPaymentRequestSessionSubmit mutation to confirm that the payment is to be processed.
Use ShopPayPaymentRequestSessionSubmit on your server to submit the session.
This event is dispatched when the payment is complete. Close the Shop Pay popup and redirect the user to the order confirmation page.
This event is dispatched when a payment attempt fails. The event contains information about why the payment failed.
Buyers have the option to select a different payment method to retry their checkout after a failure. Don't call session.close() so that users can retry with a new payment method without disrupting their session.
Buyers have the option to select a different payment method to retry their checkout after a failure. Don't call session.close() so that users can retry with a new payment method without disrupting their session.
This event is dispatched when the checkout window is closed.
Only call the corresponding complete call once for each event.
Only call the corresponding complete call once for each event.
Anchor to Handle errorsHandle errors
Every complete call accepts an optional array of ShopPayUserError objects. Use it to tell the buyer what went wrong without ending the session. The type of each error controls where its message appears in the Shop Pay dialog:
generalError: Displayed at the top of the checkout.discountCodeError: Displayed near the discount code field.shippingAddressError: Displayed near the shipping address selection.
Always set a message. A shippingAddressError or generalError without one isn't displayed to the buyer, and a discountCodeError without one falls back to an unlocalized "Invalid discount code".
You can return errors on their own, or alongside an updatedPaymentRequest. If you omit the updated payment request, then the current one continues to be used, including any values that the error invalidates.
Anchor to Reject a shipping addressReject a shipping address
Return a shippingAddressError when you can't ship to the selected address. Also return an updated payment request with an empty deliveryMethods array, so that the rates calculated for the previous address aren't offered for this one:
Anchor to Reject a discount codeReject a discount code
Return a discountCodeError when a code is invalid or expired. Set discountCodes on the updated payment request to only the codes that you accepted, because Shop Pay treats the codes you return as the applied set:
Anchor to Show a general errorShow a general error
Use generalError for a failure that isn't tied to the discount code field or the shipping address, such as a call to your server that fails. Omit updatedPaymentRequest to leave the current one in place:
The same pattern applies to a validation failure at payment confirmation, as shown in the paymentconfirmationrequested example. If you pass an updatedPaymentRequest to completePaymentConfirmationRequest(), then you must also pass at least one error, otherwise the update is ignored.