In a recent project, I got a requirement to integrate a Payment gateway. I choose Stripe as it is PCI Level 1 complaint which is the most stringent level of all 4.
Following are the steps to integrate stripe in Grails.
- Create an Account on Stripe.
- Goto Account settings and click “Api Keys” tab.
- Stripe provides a publishable key and a private key. Publishable key is used by the java-script code to
generate tokens and secret key is used for API calls on the server-side. - Configure the two keys in Config.groovy file.
1234[php]grails.plugins.stripe.secretKey = 'your_secret_key'grails.plugins.stripe.publishableKey = 'your_publishable_key'[/php] - Include stripe.js file.
- Add the following dependency in BuildConfig.groovy. Make sure to use the latest version as the API keeps changing from version to version.
123[php]runtime 'com.stripe:stripe-java:1.33.0'[/php] - Add html form and javascript code in your gsp page with proper field names provided by stripe. Sample code can be downloaded from stripe’s website. The Javascript will first generate a token that represents the credit card information. If a token is generated successfully, a hidden input is appended with stripe token as its value.
1234567891011121314[php]if (response.error) {// Show the errors on the form$form.find('.payment-errors').text(response.error.message);$form.find('button').prop('disabled', false);} else {// response contains id and card, which contains additional card detailsvar token = response.id;// Insert the token into the form so it gets submitted to the server$form.append($('<input type="hidden" name="stripeToken" />').val(token));// and submit$form.get(0).submit();}[/php] - Now the form should be submitted to our server(not to be confused with stripe server).
- We can now use the token to charge a customer.
12345[php]Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKeyMap chargeParams = ["amount":amountInCents,"card":params.stripeToken]Charge.create(chargeParams);[/php]
That was all! Hope it helped 🙂
Trackbacks/Pingbacks