Payment
Initialize payment¶
There are 2 supported payment procedures: - Checkout, where stripe provides the UI - "payment intent", the client must use stripe libraries to create its own UI.
You can call init payment endpoint only if the order is in status Placed or Payment. If the order is in status Payment you already have initialized a payment session. You will get the same response again, but you can't change the process - thus provided InitpaymentData is not taken into account. The URL of the payment endpoint:
PUT https://api.discover.swiss/test/market/v1/orders/{orderNumber}/payment
Web checkout¶
Using the checkout process of Stripe takes only two steps:
- We create a session object on the backend and send it to Stripe. The session already contains all the required information for the payment.
- checkout: the client uses the session Id to redirect the customer to the payment page. otherwise: Client creates its own form and sends the payment to stripe
You must send callbacks URL as part of the payment initialization in the body of the request. Stripe will redirect to those URL after payment done:
{
"createCheckoutSession" : true,
"successUrl": "https://my.server/payment_ok",
"errorUrl": "https://my.server/payment_failed"
}
{
"initPaymentData":
{
"sessionId": "cs_test_Px7s8kZKHpYwRfrlETarNX1MQNwEBnixCmpaTzVfPl7stpBOfIaLGm6B",
"clientSecret": "pi_5MDVV8EJdYTfvwwn0aqpMMMM_secret_5MDVVQLhbHPmtjty6x7sfSSSS",
"paymentApiKey": "pk_test_BFvwJBVHlQbtxmftXMbF3gs00052KLsbTk",
"result": true
},
"validationMessages": []
}
var stripe = window['Stripe']('pk_test_BFvwJBVHlQbtxmftXMbF3gs00052KLsbTk');
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{ CHECKOUT_SESSION_ID }} placeholder.
sessionId: response.initPaymentData.sessionId
}).then(function (result) {
// do something
}, function (result) {
// using `result.error.message`.
this.errorText = result.error.message;
});
- Checkout sample: https://stripe.com/docs/checkout/quickstart
Payment intents¶
This is the default.
{
"createCheckoutSession" : false
}
{
"initPaymentData": {
"clientSecret": "pi_5MDVV8EJdYTfvwwn0aqpMMMM_secret_5MDVVQLhbHPmtjty6x7sfSSSS",
"paymentApiKey": "pk_test_BFvwJBVHlQbtxmftXMbF3gs00052KLsbTk",
"result": true
},
"validationMessages": []
}
Info
Stripe offers libraries that fit different ways of implementation depending on the client's preferences
In the sample app we used JavaScript and the Angular elements-wrapper https://github.com/AckerApple/stripe-angular
More information:
Free orders¶
If an order has a total amount of 0, there is no payment needed and the fulfillment process starts immediately.
This is the case if you do not get any information about how to do the payment (no clientSecret)
Response
{
"initPaymentData": {
"result": true
},
"validationMessages": []
}
Pay¶
Once the payment successfully proceed payment the following things happen:
- the client gets redirected to the success page which must be implemented by the client → the client-side ordering process has finished
- stripe calls our server to confirm the payment → the fulfillment process gets started
Warning
We only reserve money of the customer's card. The charging of the cards occurs only if fulfillment has completed. On a partial fulfillment only the successfully processsed items are charged.
Stripe sends a checkout.session.completed event to the webhook we registered. This webhook is an endpoint by discover.swiss API which is not accessible to the public and is the same for all partners.
When the webhook is called, it does the following:
- Validate the message and signature from Stripe
- Save the payment id into the order and change the status to Paid. If this phase fails the webhook will return an error to Stripe. This will cause Stripe to repeat the webhook call.
- Start the fulfillment and delivery processes. If something fails in this process the webhook still returns an OK response to Stripe.