Skip to content

Commit

Permalink
Introduce demo server with payment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorasl committed Apr 5, 2021
1 parent 250fcac commit d19f5a2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ buck-out/
*.keystore

demo/ios/Pods/**
demo/secrets.js
36 changes: 31 additions & 5 deletions demo/App.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { StyleSheet, View, Button } from 'react-native';

export default function App() {

initStripe = async () => {
const { STRIPE_PAYMENTS_APP_KEY } = require('./secrets');
const { default: stripe } = await import('react-native-stripe-payments');
stripe.setOptions({ publishingKey: 'STRIPE_PUBLISHING_KEY' });
stripe.setOptions({ publishingKey: STRIPE_PAYMENTS_APP_KEY });
}

makePayment = async (isCardNeedConfirm) => {
const { default: stripe } = await import('react-native-stripe-payments');

const response = await fetch("http://localhost:8000/pay", { method: 'get' })
let res = await response.json();
console.log(res);

const cardDetails = {
number: isCardNeedConfirm ? '4000002500003155' : '4242424242424242',
expMonth: 12,
expYear: 25,
cvc: '888',
};
let result = await stripe.confirmPayment(res.client_secret, cardDetails);
console.log(result);
}

this.initStripe();
return (
<View style={styles.container}>
<StatusBar style="auto" />
<TouchableOpacity onPress={() => this.initStripe()}>
<Text>Init Stripe</Text>
</TouchableOpacity>
<Button
onPress={() => this.makePayment(false)}
title="Make payment"
color="#42409E"
/>
<Button
onPress={() => this.makePayment(true)}
title="Make payment with card confirmation"
color="#42409E"
/>
</View>
);
}
Expand Down
4 changes: 3 additions & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"main": "index.js",
"scripts": {
"server": "node server.js",
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "expo start --web",
Expand All @@ -25,7 +26,8 @@
"@babel/core": "^7.9.0",
"babel-jest": "~25.2.6",
"jest": "~25.2.6",
"react-test-renderer": "~16.13.1"
"react-test-renderer": "~16.13.1",
"stripe": "^8.142.0"
},
"jest": {
"preset": "react-native"
Expand Down
4 changes: 4 additions & 0 deletions demo/secrets.js_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
STRIPE_PAYMENTS_SERVER_KEY: 'sk_test_STRIPE_PAYMENTS_SERVER_KEY',
STRIPE_PAYMENTS_APP_KEY: 'pk_test_STRIPE_PAYMENTS_APP_KEY'
};
26 changes: 26 additions & 0 deletions demo/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const http = require('http');
const { STRIPE_PAYMENTS_SERVER_KEY } = require('./secrets');
const stripe = require('stripe')(STRIPE_PAYMENTS_SERVER_KEY);

const hostname = '127.0.0.1';
const port = 8000;

const server = http.createServer(async (req, res) => {
switch (req.url) {
case "/pay":
const intent = await stripe.paymentIntents.create({
amount: 200,
currency: 'usd',
payment_method_types: ['card'],
});
res.writeHead(200, {'Content-Type': 'application/json'});
return res.end(JSON.stringify(intent));
default:
res.writeHead(404, { 'Content-Type': 'application/json' });
return res.end();
}
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
12 changes: 10 additions & 2 deletions demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/node@*":
"@types/node@*", "@types/node@>=8.1.0":
version "14.14.37"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==
Expand Down Expand Up @@ -6662,7 +6662,7 @@ punycode@^2.1.0, punycode@^2.1.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

qs@^6.5.0:
qs@^6.5.0, qs@^6.6.0:
version "6.10.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.10.1.tgz#4931482fa8d647a5aab799c5271d2133b981fb6a"
integrity sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==
Expand Down Expand Up @@ -7609,6 +7609,14 @@ strip-final-newline@^2.0.0:
resolved "https://registry.yarnpkg.com/strip-final-newline/-/strip-final-newline-2.0.0.tgz#89b852fb2fcbe936f6f4b3187afb0a12c1ab58ad"
integrity sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==

stripe@^8.142.0:
version "8.142.0"
resolved "https://registry.yarnpkg.com/stripe/-/stripe-8.142.0.tgz#8a55e92837ea5c8bc4f65754df01c004a2beebed"
integrity sha512-0CimG7f5lsqD+qf+Vv8QgBPuYT0hqn/gfXBNEdeQxIIA+WUrxqXEyS5C0r5YIxa/Et5WiByfCpXJdMsxh4G35Q==
dependencies:
"@types/node" ">=8.1.0"
qs "^6.6.0"

sudo-prompt@^9.0.0:
version "9.2.1"
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-9.2.1.tgz#77efb84309c9ca489527a4e749f287e6bdd52afd"
Expand Down

0 comments on commit d19f5a2

Please sign in to comment.