Guest User

Untitled

a guest
Mar 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. ## On Ionic side
  2. ### 1. Create a ionic project
  3. * ionic start ionicStripe blank
  4. ### 2. Install the stripe plugin
  5. * `ionic cordova plugin add cordova-plugin-stripe`
  6. * `npm install --save @ionic-native/stripe`
  7. ### 3. add stripe to module.ts
  8.  * `import { Stripe } from '@ionic-native/stripe';`
  9.  
  10. ### 4. On the payment page :
  11. * import stripe again `import { Stripe } from "@ionic-native/stripe";` and add it to constructor
  12.  
  13. ### 5. Get credit card information (for example read it from a form)
  14. ```
  15. cardinfo: any = {
  16. number: 4242424242424242,
  17. expMonth: 8,
  18. expYear: 2020,
  19. cvc: 213
  20. }
  21. ```
  22.  
  23. ### 6. Next, generate token with stripe ans to make payment,
  24. in my case, I record all the orders in my database before starting the payment process with stripe
  25. ```
  26. this.stripe.setPublishableKey('pk_test**************');
  27. this.stripe.createCardToken(this.cardinfo).then((response)=>{
  28. // I get my token here response.id
  29. });
  30. ```
  31. ### 7. i need to save the orders in my database
  32.  
  33. ```
  34. this.stripe.createCardToken(this.cardinfo).then((response)=>{
  35. // I get my token here response.id
  36. const Payment = skygear.Record.extend('payment');
  37. skygear.publicDB.save(new Payer({
  38. 'token': response,
  39. 'description':"decri[tion payment",
  40. 'prix':100,
  41. })).then((record) => {
  42. console.log(record);
  43. }, (error) => {
  44. console.error(error);
  45. });
  46. });
  47. ```
  48.  
  49. ## Skygear Cloud function side
  50. You will need a server to create a charge
  51. ### 8. Create a Skygear cloud code project and install stripe sdk, `npm i --save stripe`
  52.  
  53. Your package.json should have included "stripe" dependency, e.g. below:
  54.  
  55. ```
  56. {
  57. "name": "example_project",
  58. "version": "1.0.0",
  59. "description": "",
  60. "main": "index.js",
  61. "scripts": {
  62. "test": "echo \"Error: no test specified\" && exit 1"
  63. },
  64. "author": "arsene",
  65. "license": "ISC",
  66. "dependencies": {
  67. dotenv": "^5.0.1",
  68. "stripe": "^5.5.0"
  69. }
  70. }
  71. ```
  72.  
  73. ### 9. Then create a cloud function
  74. ```
  75. const skygear =require ('skygear');
  76. const skygearCloud = require('skygear/cloud');
  77. var stripe = require("stripe")("sk_test********************");
  78. skygearCloud.afterSave('payment', function(record, original, pool, options) {
  79. // write your code
  80. stripe.charges.create({
  81. amount: record.prix,
  82. currency: "usd",
  83. source: record.token.id,
  84. description: record.description
  85. }, function(err, charge) {
  86. if (err) {
  87. console.log("Erreur" + JSON.stringify(err))
  88. }
  89. // asynchronously called
  90. });
  91. }, {
  92. async: false
  93. });
  94.  
  95. ```
Add Comment
Please, Sign In to add comment