Last active
October 10, 2022 13:32
-
-
Save domtalbot/cad3d8d445d20bc86ee34a5b3779beb2 to your computer and use it in GitHub Desktop.
payment-flow
This file contains 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
/* API */ | |
/** | |
* Fetch the available payment methods from Adyen | |
* | |
* GET: /api/payment-methods | |
*/ | |
app.get('/api/payment-methods', async (req, res) => { | |
try { | |
const response = await checkout.paymentMethods({ | |
shopperReference: req.query.userGuid, | |
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT, | |
}); | |
res.json({ | |
paymentMethodsResponse: response.paymentMethods, | |
clientKey: process.env.ADYEN_CLIENT_KEY, | |
}); | |
} catch (err) { | |
console.error(`Error: ${err.message}, error code: ${err.errorCode}`); | |
res.status(err.statusCode).json(err.message); | |
} | |
}); | |
/** | |
* Make a payment on Adyen, called by the drop-in component | |
* after collecting encrypted card details from the customer | |
* | |
* POST: /api/make-payment | |
*/ | |
app.post('/api/make-payment', async (req, res) => { | |
console.log('body', req.body); | |
try { | |
const response = await checkout.payments({ | |
merchantAccount: process.env.ADYEN_MERCHANT_ACCOUNT, | |
amount: req.body.amount, | |
// amount: { | |
// value: 100, | |
// currency: 'GBP', | |
// }, | |
reference: uuid(), | |
paymentMethod: req.body.paymentMethod, | |
additionalData: { | |
allow3DS2: true, | |
}, | |
channel: 'ios', | |
returnUrl: 'rnapoc://adyen', | |
}); | |
res.json(response); | |
} catch (err) { | |
console.error(err); | |
res.status(500).json(err); | |
} | |
}); | |
/** | |
* Post additional payment details to Adyen | |
* This is used for 3DS details | |
* | |
* POST: /api/details | |
*/ | |
app.post('/api/details', async (req, res) => { | |
try { | |
const response = await checkout.paymentsDetails({ | |
details: req.body.details, | |
paymentData: req.body.paymentData, | |
}); | |
res.json(response); | |
} catch (err) { | |
console.error(err); | |
res.status(500).json(err); | |
} | |
}); | |
/* INAPP */ | |
async function pay(data: any) { | |
if (!loading) { | |
setLoading(true); | |
PaymentService.getPaymentMethods() | |
.then((res: any) => { | |
console.log(res.data); | |
const adyenDropIn = AdyenDropIn.setModuleConfig({ | |
baseUrl: baseUrl, | |
debug: true, | |
endpoints: { | |
makePayment: '/make-payment', | |
makeDetailsCall: '/details', | |
}, | |
}).setDropInConfig({ | |
clientKey: res.data.clientKey, | |
environment: 'test', // [test/live] | |
countryCode: 'GB', | |
amount: { | |
value: +data.amount * 100, | |
currencyCode: 'GBP', | |
}, | |
applePay: { | |
label: 'AppToPay Purchase', | |
amount: { | |
value: +data.amount, | |
currencyCode: 'GBP', | |
}, | |
configuration: { | |
merchantName: 'V12 Retail Finance Limited', | |
merchantId: | |
'merchant.com.adyen.V12RetailFinanceLimitedECOM.test', | |
}, | |
}, | |
}); | |
/** | |
* Start the drop-in flow | |
*/ | |
adyenDropIn | |
.start(res.data.paymentMethodsResponse) | |
.then((paymentRes: any) => { | |
if (isSuccessResult(paymentRes)) { | |
Alert.alert( | |
'Success', | |
`Payment success: ${paymentRes.resultCode}`, | |
); | |
} else { | |
Alert.alert( | |
'Refused', | |
`Payment refused: ${paymentRes.refusalReason}`, | |
); | |
} | |
}) | |
.catch((err: any) => { | |
if (isCancelledError(err)) { | |
console.log('Cancelled'); | |
} else { | |
console.log('err', err); | |
Alert.alert('Error', `Payment error: ${err}`); | |
} | |
}) | |
.finally(() => { | |
setLoading(false); | |
}); | |
}) | |
.catch(err => { | |
console.log('erro', err); | |
Alert.alert('Error getting payment methods for customer'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment