Created
December 31, 2021 10:26
-
-
Save vivekvardhanadepu/3807cf7fc248fc9029aa44bc734e0ef2 to your computer and use it in GitHub Desktop.
Payment functions in react-native-stripe-terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StripeTerminal from "react-native-stripe-terminal"; | |
// Creating payment intent in backend | |
axios.post("https://your.backend.com/create_payment_intent", {}) | |
.then((paymentIntent) => { | |
const clientSecret = paymentIntent.client_secret; | |
return StripeTerminal.retrievePaymentIntent(clientSecret); | |
}) | |
//--------or---------// | |
// Creating paymentIntent in the frontend | |
StripeTerminal.createPaymentIntent() | |
// Collecting Payment Method(from the Card Reader) | |
.then((paymentIntent) => { | |
return StripeTerminal.collectPaymentMethod(); | |
}) | |
// Processing Payment | |
.then((res) => { | |
if (res.error) { | |
throw res.error; | |
} else { | |
return StripeTerminal.processPayment(); | |
} | |
}) | |
// Capturing Payment Intent | |
.then((paymentIntent) => { | |
if (paymentIntent.error) { | |
throw paymentIntent.error; | |
} else { | |
return axios.post("https://your.backend.com/capture_payment_intent", | |
{ | |
paymentIntentId: paymentIntent, | |
} | |
); | |
} | |
}) | |
.then((paymentresponse) => { | |
// handle response | |
}) | |
.catch((error) => { | |
// handle error | |
}); | |
// createPayment abstracts createPaymentIntent, collectPaymentMethod, and | |
// confirmPaymentIntent into a single method. If any of them fail, | |
// the Promise will be rejected. A resolved promise means that the | |
// payment was authorized & posted to Stripe and awaits capture. | |
StripeTerminal.createPayment({ amount: 1200, currency: "usd" }) | |
.then(intent => { | |
console.log('Payment intent created', intent); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
// Abort creating a payment | |
StripeTerminal.abortCreatePayment() | |
.then(intent => { | |
console.log('Payment aborted', intent); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment