Blog

  • Stripe integration in grails

    Posted by Sumit Rathi | Last Updated: 17-Mar-15

    While working on a project, I came across a requirement where I had to add payment part. So, In this blog I am going to share the details of how we can integrate stripe.
     
     
    Integration steps:
    1. Create a stripe account. click here to create an account on stripe.
    2. We will need to set two configuration items in Config.groovy
    
    	
    		grails.oodles.stripe.secretKey="sk_test..........." 
             grails.oodles.stripe.publishableKey="pk_test......"
    
    Logged in your stripe account, click on the top-right link for Your Account and then Account Settings.
     
    Here, you can find your API Keys.
     
    3. Next we need to include the JavaScript library provided by Stripe. 
    Stripe.js makes it easy to collect credit card (and other similarly sensitive) details without having the information touch your server.
    4. Add these jar files in your lib  stripe-java-latest.jar
    5. Add these fields in your .gsp
      <form action="" method="POST" id="payment-form">
       <span class="payment-errors"> </span>
    
       <div class="form-row">
         <label>
           <span>Card Number </span>
           <input type="text" size="20" data-stripe="number"/>
         </label>
       </div>
    
       <div class="form-row">
         <label>
           <span>CVC </span>
           <input type="text" size="4" data-stripe="cvc"/>
         </label>
       </div>
    
      <div class="form-row">
        <label>
          <span>Expiration (MM/YYYY)</span>
          <input type="text" size="2" data-stripe="exp-month"/>
        </label>
        
        <input type="text" size="4" data-stripe="exp-year"/>
      </div>
    
      <button type="submit">Submit Payment</button>
    </form>
    
    6. If the card information entered by the user returned an error, it gets displayed on the page.
    If no errors were returned (i.e. a single-use token was created successfully), add the returned token to the form in the stripeToken field and submit the form to the server.
    we can make your payment request like
     Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKey
    Map chargeParams = ["amount":params.amount,"card":params.stripeToken]
    
    		try {
    			Charge.create(chargeParams);
    			returnObject.put("status", "success")
    			returnObject.put("message", "Transaction complete successfully.")
    		} catch (CardException e) {
    			// Since it's a decline, CardException will be caught
    				returnObject.put("status", "error")
    				returnObject.put("message", e.message)
    		} catch (InvalidRequestException e) {
    			// Invalid parameters were supplied to Stripe's API
    				returnObject.put("status", "error")
    				returnObject.put("message", e.message)
    		} catch (AuthenticationException e) {
    			// Authentication with Stripe's API failed
    			// (maybe you changed API keys recently)
    				returnObject.put("status", "error")
    				returnObject.put("message", e.message)
    	  } catch (APIConnectionException e) {                      
    			// Network communication with Stripe failed
    				returnObject.put("status", "error")
    				returnObject.put("message", e.message)
    	  } catch (StripeException e) {
    			// Display a very generic error to the user, and maybe send
    			// yourself an email
    				returnObject.put("status", "error")
    				returnObject.put("message", e.message)
    	  }
     
    Hope it helps :)
    Sumit Rathi
    sumit.rathi@oodlestechnologies.com
     

Tags: grails

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com