Adding Online Payment Support in Online Event Frontend via Stripe
Open Event Frontend involves ticketing system which supports both paid and free tickets. To buy a paid ticket Open Event provides several options such as debit card, credit card, cheque, bank transfer and onsite payments. So to add support for debit and credit card payments Open Event uses stripe checkout. Using stripe users can enter their card details and pay for their ticket.
Given below are some steps which are to be followed for successfully charging a user for ticket using his/her card.
- We get publishable key for organizer’s stripe account using OAuth. See this blog.
- We render stripe checkout button using stripe publishable key. This helps us identify which user to credit after payment.
- After clicking checkout button user is prompted to enter his/her card details and verify payment.
- After user’s verification stripe generates a payment token is which is used by open event frontend to charge the user for stipulated amount.
- We send this token to open event server which processes the token and charge the user.
- We get error or success message from open event server as per the process outcome.
To render the stripe checkout button we use ember-cli-stripe. Below is the code which helps us to understand how stripe checkout button is rendered.
// app/templates/orders/placed.hbs {{stripe-checkout locale='auto' name="Open Event" description=paymentDescription amount=paymentAmount key=model.event.stripeAuthorization.stripePublishableKey onToken=(action "processStripeToken") onClosed=(action "checkoutClosed") onOpened=(action "checkoutOpened") }}
Full code can be seen here.
We see that we pass different parameters to stripe button which helps stripe identify how to render the button and what information to display. We have also passed the actions onToken(), onClosed() and onOpened(). All these actions are triggered at different instances based on what event occurs.
onToken(): This action is triggered when user has verified his/her purchase and we stripe has generated the payment token. Stripe passes the token back to client (open event frontend server) to process. We have handled this error via different name “processStripeToken()”. We will see cose for these actions below.
onClosed(): This action is called when checkout prompt is closed. We have not used this action in open event frontend. But this can be used to trigger some event in case your application need some action when checkout prompt is closed.
onOpened(): This action is called when checkout prompt is opened. We have not used this action in open event frontend. But this can be used to trigger some event in case your application need some action when checkout prompt is opened.
Code for these actions are given below. Full code file can be seen here.
// app/controllers/orders/placed.js processStripeToken(token) { // Send this token to server to process payment let order = this.get('model'); let chargePayload = { 'data': { 'attributes': { 'stripe' : token.id, 'paypal_payer_id' : null, 'paypal_payment_id' : null }, 'type': 'charge' } }; let config = { skipDataTransform: true }; chargePayload = JSON.stringify(chargePayload); return this.get('loader').post(`orders/${order.identifier}/charge`, chargePayload, config) .then(charge => { if (charge.data.attributes.status) { this.get('notify').success(charge.data.attributes.message); this.transitionToRoute('orders.view', order.identifier); } else { this.get('notify').error(charge.data.attributes.message); } }); }
In above code snippet for processStipeToken() we are processing the stripe payment token received from stripe after user verifies his/her payment. We pass this token to charge endpoint of open event server. After charging the user server send a response which is displayed on frontend.
In this way we achieve the functionality of adding stripe payment support in open event frontend. Please follow the links below for further clarification and detailed overview.
Resources:
- Link to PR: Part 1, Part 2
- Charge endpoint: open event server: https://open-event-api-dev.herokuapp.com/#orders-charge-post
- Stripe checkout docs: https://stripe.com/checkout