0
votes

Hi I am writing a REST web application and confused on how to handle the payment via Stripe.

I am unclear on when to create order, below is 2 approaches I have thought:

First approach

  • User enters order details and clicks place order.
  • Order gets created in database with a boolean flag is_active and orderId is sent to UI.
  • user gets redirected to stripe payment page.
  • user enters card details and we get stripe token for charging card
  • token with orderId sent to backend.
  • token is used in backend to send request to Stripe to charge user, if charge success then mark order as active otherwise report failure to user.

Second approach

  • User enters details and clicks place order.
  • user gets redirected to stripe payment page.
  • user enters card details and we get stripe token for charging card
  • token with orderdetails sent to backend.
  • token is used in backend to send request to Stripe to charge user, if charge success then create order otherwise report failure to user.

which of the above two approach should I use or is there any other way through which I should be handling the payments?

1

1 Answers

0
votes

I would only use the first option if building an order was such a lengthy or high-stakes process that you would want to persist a dead candidate order.

The second approach is fine. I would suggest splitting the process of saving a card off from the process of submitting an order; so you have two API calls that can do their error handing separately.

  • User enters order details and clicks "Next".
  • User is sent through the Stripe payment page (or your own on-site stripe integration).
  • The Stripe token is sent to the backend and saved as a/the payment method for the user.
  • The user clicks "Submit".
  • Order details are sent to the backend.
  • Token is used to request the charge, etc...