Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ionic payment with Stripe, then create a record at Skygear.

On Ionic side

1. Create a ionic project

  • ionic start ionicStripe blank

2. Install the stripe plugin

  • ionic cordova plugin add cordova-plugin-stripe
  • npm install --save @ionic-native/stripe

3. add stripe to module.ts

 * import { Stripe } from '@ionic-native/stripe';

4. On the payment page :

  • import stripe again import { Stripe } from "@ionic-native/stripe"; and add it to constructor

5. Get credit card information (for example read it from a form)

 cardinfo: any = {
    number: 4242424242424242,
    expMonth: 8,
    expYear: 2020, 
    cvc: 213
  }

6. Next, generate token with stripe ans to make payment,

in my case, I record all the orders in my database before starting the payment process with stripe

this.stripe.setPublishableKey('pk_test**************');
this.stripe.createCardToken(this.cardinfo).then((response)=>{
    // I get my token here response.id
  }); 

7. i need to save the orders in my database

this.stripe.createCardToken(this.cardinfo).then((response)=>{
   // I get my token here response.id
   const Payment = skygear.Record.extend('payment');
          skygear.publicDB.save(new Payer({
           'token': response,
           'description':"decri[tion payment",
           'prix':100,
         })).then((record) => {
           console.log(record);
         }, (error) => {
           console.error(error);
         });
 }); 

Skygear Cloud function side

You will need a server to create a charge

8. Create a Skygear cloud code project and install stripe sdk, npm i --save stripe

Your package.json should have included "stripe" dependency, e.g. below:

{
  "name": "example_project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "arsene",
  "license": "ISC",
  "dependencies": {
    dotenv": "^5.0.1",
    "stripe": "^5.5.0"
  }
}

9. Then create a cloud function

const skygear =require ('skygear');
const skygearCloud = require('skygear/cloud');
var stripe = require("stripe")("sk_test********************");
skygearCloud.afterSave('payment', function(record, original, pool, options) {
    // write your code
    stripe.charges.create({
      amount: record.prix,
      currency: "usd",
      source: record.token.id, 
      description: record.description
    }, function(err, charge) {
      if (err) {
        console.log("Erreur" + JSON.stringify(err))
      }
      // asynchronously called
    });
}, {
    async: false
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.