├── views
├── cancel.ejs
├── success.ejs
└── index.ejs
├── README.md
├── package.json
└── app.js
/views/cancel.ejs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | cancel
8 |
9 |
10 | Transaction canceled!
11 |
12 |
13 |
--------------------------------------------------------------------------------
/views/success.ejs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | success
8 |
9 |
10 | Transaction successfull!
11 |
12 |
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Documentation
2 |
3 | [Integrating Paypal in your react native app](https://medium.com/@adityasingh_32512/integrating-paypal-in-your-react-native-app-4dcf89e11dd)
4 |
5 | # Introduction
6 |
7 | Make sure to change client_id and client_secret values with your own values inside app.js
8 |
9 | Once done, follow up [React Native Paypal](https://github.com/morfsys/react-native-paypal) for the react native code.
10 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "paypal-rn",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "author": "",
10 | "license": "ISC",
11 | "dependencies": {
12 | "body-parser": "^1.18.3",
13 | "consolidate": "^0.15.1",
14 | "ejs": "^2.6.1",
15 | "express": "^4.16.4",
16 | "paypal-rest-sdk": "^1.8.1"
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/views/index.ejs:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Document
8 |
9 |
10 |
11 | Please wait...
12 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const bodyParser = require("body-parser");
3 | const engines = require("consolidate");
4 | const paypal = require("paypal-rest-sdk");
5 |
6 | const app = express();
7 |
8 | app.engine("ejs", engines.ejs);
9 | app.set("views", "./views");
10 | app.set("view engine", "ejs");
11 |
12 | app.use(bodyParser.json());
13 | app.use(bodyParser.urlencoded({ extended: true }));
14 |
15 | paypal.configure({
16 | mode: "sandbox", //sandbox or live
17 | client_id:
18 | "AarUi7DfcZaVwTyuHOE2qMPGP1Gy65T8KNHicseSgSXB-gm_2upRM74fU-MKmslNaHKqNOUsxMrNv9I-",
19 | client_secret:
20 | "EI0ea3hVzYFwuts33i0RUVhxF48woSUSg7lwNbkImLrHpcEYpUmJPRXdf4CXn4kFacsRoZ-62cn9Xe6h"
21 | });
22 |
23 | app.get("/", (req, res) => {
24 | res.render("index");
25 | });
26 |
27 | app.get("/paypal", (req, res) => {
28 | var create_payment_json = {
29 | intent: "sale",
30 | payer: {
31 | payment_method: "paypal"
32 | },
33 | redirect_urls: {
34 | return_url: "http://localhost:3000/success",
35 | cancel_url: "http://localhost:3000/cancel"
36 | },
37 | transactions: [
38 | {
39 | item_list: {
40 | items: [
41 | {
42 | name: "item",
43 | sku: "item",
44 | price: "1.00",
45 | currency: "USD",
46 | quantity: 1
47 | }
48 | ]
49 | },
50 | amount: {
51 | currency: "USD",
52 | total: "1.00"
53 | },
54 | description: "This is the payment description."
55 | }
56 | ]
57 | };
58 |
59 | paypal.payment.create(create_payment_json, function(error, payment) {
60 | if (error) {
61 | throw error;
62 | } else {
63 | console.log("Create Payment Response");
64 | console.log(payment);
65 | res.redirect(payment.links[1].href);
66 | }
67 | });
68 | });
69 |
70 | app.get("/success", (req, res) => {
71 | // res.send("Success");
72 | var PayerID = req.query.PayerID;
73 | var paymentId = req.query.paymentId;
74 | var execute_payment_json = {
75 | payer_id: PayerID,
76 | transactions: [
77 | {
78 | amount: {
79 | currency: "USD",
80 | total: "1.00"
81 | }
82 | }
83 | ]
84 | };
85 |
86 | paypal.payment.execute(paymentId, execute_payment_json, function(
87 | error,
88 | payment
89 | ) {
90 | if (error) {
91 | console.log(error.response);
92 | throw error;
93 | } else {
94 | console.log("Get Payment Response");
95 | console.log(JSON.stringify(payment));
96 | res.render("success");
97 | }
98 | });
99 | });
100 |
101 | app.get("cancel", (req, res) => {
102 | res.render("cancel");
103 | });
104 |
105 | app.listen(3000, () => {
106 | console.log("Server is running");
107 | });
108 |
--------------------------------------------------------------------------------