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.

 

  1. Create an Account on Stripe.
  2. Goto Account settings and click “Api Keys” tab.
    stripe1
  3. 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.
  4. Configure the two keys in Config.groovy file.
    grails.plugins.stripe.secretKey = 'your_secret_key'
    grails.plugins.stripe.publishableKey = 'your_publishable_key'
    
  5. Include stripe.js file.
  6. Add the following dependency in BuildConfig.groovy. Make sure to use the latest version as the API keeps changing from version to version.
    runtime 'com.stripe:stripe-java:1.33.0'
    
  7. 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.
     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 details
     var 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();
     }
    
  8. Now the form should be submitted to our server(not to be confused with stripe server).
  9. We can now use the token to charge a customer.
    Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKey
    Map chargeParams = ["amount":amountInCents,"card":params.stripeToken]
    Charge.create(chargeParams);
    

That was all! Hope it helped 🙂