Stripe integration in grails

Posted By : Sumit Rathi | 30-Jun-2014
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
 

About Author

Author Image
Sumit Rathi

Sumit is a highly experienced Backend Developer with a diverse technological background. He possesses an extensive understanding of the latest technologies and has practical expertise in Core Java, Spring Boot, Hibernate, as well as relational databases like MySQL and PostgreSQL. His proficiency extends to API implementations, Microservices, and Web Services development. Sumit actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including MyHelpa, NOWcast Church Engagement Development, OptaPlanner - Employee Rostering, Optaplanner customization for a medical practice, Handymen Application Upgradation II, LabVue, and more. He showcases exceptional analytical skills and a creative mindset. Furthermore, he has a passion for reading books and exploring new technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..