Create Stripe Account on https://stripe.com/
The First step is to create an account on Stripe. All you need to do for this is to fill out a form and then confirm the account via Email.
API Keys
After confirming your account via Email, you can find your Stripe API Keys in “Developers > API Keys” from the navigation menu.
https://dashboard.stripe.com/account/apikeys
You will find 2 types of keys there:
a. Publishable Key: pk_test_xxx… //{PUBLISHABLE_KEY}
b. Secret Key: sk_test_xxx… //{SECRET_KEY}
Publishable Key is like a public key. This key can be shared with anyone and will also be printed on your HTML Page in a hidden input field.
Secret Key is not for Public. This key should not be shared with anyone and not be printed anywhere on pages visible to Users.
To view live keys, you need to activate your account via “Activate Your Account” from the navigation menu.
https://dashboard.stripe.com/account/details
Add Dependencies
Grails:
1 |
compile "org.grails.plugins:stripe:2.10" |
Maven:
1 2 3 4 5 |
<dependency> <groupId>com.stripe</groupId> <artifactId>stripe-java</artifactId> <version>4.4.0</version> </dependency> |
Repository:
1 |
https://mvnrepository.com/artifact/com.stripe/stripe-java |
Front End Code – using Custom Form (HTML + JavaScript)
index.gsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<g:form method="post" controller="" action=""> <div id="stripe-card-number"></div> <div id="stripe-card-expires"></div> <div id="stripe-card-cvc"></div> <input type="text" name="stripe-zip" id="stripe-zip" /> <input type="hidden" name="stripetoken" id="stripetoken" /> </g:form> <script src="https://js.stripe.com/v3/"></script> <script type="text/javascript"> var stripe = Stripe({PUBLISHABLE_KEY}); var elements = stripe.elements(); var cardNumberElement = elements.create('cardNumber', { placeholder: '', }); cardNumberElement.mount('#stripe-card-number'); var cardExpiryElement = elements.create('cardExpiry', { placeholder: '', }); cardExpiryElement.mount('#stripe-card-expires'); var cardCvcElement = elements.create('cardCvc', { placeholder: '', }); cardCvcElement.mount('#stripe-card-cvc'); document.querySelector('form').addEventListener('submit', function(e) { e.preventDefault(); var options = { address_zip: document.getElementById('stripe-zip').value }; stripe.createToken(cardNumberElement, options).then(setOutcome); }); function setOutcome(result) { if (result.token) { var form = document.querySelector('form'); $("#stripetoken").val(result.token.id); form.submit(); } else if (result.error) { alert("Invalid Card Details!"); return false; } } </script> |
Back End Code (Grails)
StripeController.groovy
1 |
grails create-controller com.test.StripeController |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//Create a new Customer with Card def newCustomer() { Stripe.apiKey = {SECRET_KEY} Map customerparams = new HashMap() customerparams.put("description", "Description of Customer") customerparams.put("source", {TOKEN_ID}) //returned by stripe.js Customer customer = Customer.create(customerparams) //save the customer.id and card.id in database for future use. } //Create a Card in a Customer def createCard(){ Stripe.apiKey = {SECRET_KEY} Customer customer = Customer.retrieve({CUSTOMER_ID}); //saved in your database when you generated this customer. Map params = new HashMap() params.put("source", {TOKEN_ID}) // returned by stripe.js customer.getSources().create(params) //save card.id in database for future use. } //Retreive a Card def retrieveCard(){ Stripe.apiKey = {SECRET_KEY} Customer customer = Customer.retrieve({CUSTOMER_ID}) //saved in your database when you generated this customer. Card source = (Card) customer.getSources().retrieve({CARD_ID}) //saved in your database when you generated this Card. } //Update a Card def updateCard(){ Stripe.apiKey = {SECRET_KEY} Customer customer = Customer.retrieve({CUSTOMER_ID}) //saved in your database when you generated this customer. Card card = (Card) customer.getSources().retrieve({CARD_ID}) //saved in your database when you generated this Card. Map updateParams = new HashMap() updateParams.put("name", {NEW_NAME}) card.update(updateParams) } //Delete a Card def deleteCard(){ Stripe.apiKey = {SECRET_KEY} Customer customer = Customer.retrieve({CUSTOMER_ID}) //saved in your database when you generated this customer. customer.getSources().retrieve({CARD_ID}).delete() //saved in your database when you generated this Card. } def listCards(){ Stripe.apiKey = {SECRET_KEY} Map sourcesParams = new HashMap() sourcesParams.put("object", "card") Customer.retrieve({CUSTOMER_ID}).getSources().all(sourcesParams) //saved in your database when you generated this customer. } |
Response
Response from stripe.createToken()
This response is returned by Stripe.js on index.gsp when we call the method stripe.createToken() in Javascript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
com.stripe.model.Token JSON: { "id": "tok_1CZbalIA77r0bjXvuoUoCByI", //{TOKEN_ID} "object": "token", "card": { "id": "card_1CZbalIA77r0bjXvCzGCTee1", //{CARD_ID} "object": "card", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": null, "address_zip_check": null, "brand": "Visa", "country": "US", "cvc_check": null, "dynamic_last4": null, "exp_month": 8, "exp_year": 2019, "fingerprint": "W7Ib2GHZOTKEzNAf", "funding": "credit", "last4": "4242", "metadata": { }, "name": null, "tokenization_method": null }, "client_ip": null, "created": 1528191399, "livemode": false, "type": "card", "used": false } |
Response from Customer.create(customerparams)
This response is returned by Stripe in Controller when we call the method Customer.create(customerparams)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
com.stripe.model.Card JSON: { "id": "card_1CaeYsIA77r0bjXv0QroS9NQ", //{CARD_ID} "object": "card", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "address_state": null, "address_zip": null, "address_zip_check": null, "brand": "Visa", "country": "US", "customer": "cus_D0fuCTG1Vc06wS", //{CUSTOMER_ID} "cvc_check": null, "dynamic_last4": null, "exp_month": 8, "exp_year": 2019, "fingerprint": "W7Ib2GHZOTKEzNAf", "funding": "credit", "last4": "4242", "metadata": { }, "name": null, "tokenization_method": null } |
Really helpfull.. Thanks..
Very usefull thanku so much Sajal