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 the Shop sales channel in your Shopify admin 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. shopId (integer) can be retrieved from the shop object in the GraphQL Admin API.
PaymentRequest fields are defined here.
Anchor to Set a fixed pickup locationSet a fixed pickup location
To display one pickup location without allowing the buyer to change it, set buyerCapabilities.changePickupLocation to false and provide exactly one pickup location:
Use fixedPickupPaymentRequest instead of initialPaymentRequest when you create the session. Shop Pay displays the selected location without Change location and doesn't dispatch pickuplocationchanged or pickuplocationfilterchanged events.
Every updatedPaymentRequest is a full replacement. Include both pickupLocations and buyerCapabilities in each update that should keep the location fixed. If you set changePickupLocation to false with zero or multiple pickup locations, then Shop Pay logs a warning and allows the buyer to change the location.
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.
If the merchant processes payments through more than one business entity, then you can also change the session's entity in this listener, before you call completeShippingAddressChange(). Refer to Process payments through multiple business entities.
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.