Skip to content

Instantly share code, notes, and snippets.

@ofyzero
Last active July 13, 2022 11:49
Show Gist options
  • Save ofyzero/43f4e7400982e9c427dbedd696de587e to your computer and use it in GitHub Desktop.
Save ofyzero/43f4e7400982e9c427dbedd696de587e to your computer and use it in GitHub Desktop.
IonicApplePay with Stripe Example
const stripe = require('stripe')('stripe_secret_key');
myExpress.post('/applepay', function (req, res) {
logRequest(req.headers['x-real-ip'], "/applepay");
if (req.body.token === undefined) {
return res.status(501).send("wrong token");
} else if (req.body.currencyCode === undefined) {
return res.status(501).send("currency code is missing");
} else if (req.body.amount === undefined || isNaN(req.body.amount)) {
return res.status(501).send("amount should be numeric");
}
else{
stripe.charges.create({
amount: req.body.amount * 100, // stripe wants it
currency: req.body.currencyCode,
source: req.body.token,
}).then((charge) => {
// asynchronously called
return res.status(200);
//res.send(charge);
})
.catch(err =>{
return res.status(err.statusCode).send("stripe error");
});
//return res.status(200);
}
});
//This is the function in ionic to use applePay :
async ApplePay() {
const opts = {
merchantId: "your merchantIdentifier ",
country: "countryCode",
currency: "currencyCode",
items: [
{
label: "description",
amount: "amount"
}
],
};
const success = (token, cb) => {
// response of token object
// {"object":"token",
// "livemode":true,
// "id":"tok_1FYZakFGoMkOv5wfawGSGZC2",
// "client_ip":"24.0.105.129",
// "card":{ "object":"card","exp_month":11,"country":"US","funding":"credit","dynamic_last4":"2428",
// "id":"card_1FYZakFGoMkOv5wfkad5a96o","tokenization_method":"apple_pay",
// "last4":"4817","brand":"MasterCard","metadata":{},"exp_year":2022},
// "created":1572274390,"used":false,"type":"card"}
// token is the payment authorization token
// cb is a callback function that accepts a boolean. Pass `true` to capture payment or `false` to discard it.
// this is an example for charging with stripe token
// send charge request to app server
this.http.post(this.urlApplePay, {
amount: "amount",
currencyCode: "currencyCode" ,
token: token.id // stripe token
},'').then(result => {
// response of server
if ( result.status == 200){
cb("true"); // make successfull tic at apple pay view
}else{
cb("false"); // make payment error at apple pay view
}
}).catch(err => {
// handle error
});
};
const error = (err) => {
// handle error
};
const result = cordova.plugins.stripe.payWithApplePay(opts, success, error);
}
There is two text : ionic-applepay-stripe usage and charge at NodeJS server.
@sandy1198
Copy link

sandy1198 commented Jul 13, 2022

apple pay working ?
I got an error from You're using your Stripe testmode key. Make sure to use your livemode key when submitting to the App Store!

Callback ID is CordovaStripe851949724
THREAD WARNING: ['CordovaStripe'] took '17.274902' ms. Plugin should use a background thread.
[General] Connection to remote alert view service failedxcode
if you have a any idea bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment