From 77fcfc3e66800de7f8eb497ded0ca880624b9e8d Mon Sep 17 00:00:00 2001 From: Marcus Pasell Date: Tue, 20 Sep 2022 16:13:08 -0700 Subject: [PATCH] [PAY-568] Add route to identity to create a Stripe session (#3879) * Add route to identity to create stripe session * Add config for stripe secret key * Lint fix * Add stripeSecretKey to config schema --- identity-service/default-config.json | 5 ++- identity-service/src/config.js | 6 +++ identity-service/src/routes/stripe.js | 64 +++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 identity-service/src/routes/stripe.js diff --git a/identity-service/default-config.json b/identity-service/default-config.json index d6a04aa4667..610aa3cd283 100644 --- a/identity-service/default-config.json +++ b/identity-service/default-config.json @@ -4,7 +4,7 @@ "redisHost": "identity-redis", "redisPort": 6379, "logLevel": "debug", - "ipdataAPIKey":"", + "ipdataAPIKey": "", "twitterAPIKey": "", "twitterAPISecret": "", "tikTokAPIKey": "", @@ -70,5 +70,6 @@ "hCaptchaSecret": "", "solanaFeePayerWallets": "[]", "sentryDSN": "", - "solanaEndpoint": "https://api.mainnet-beta.solana.com" + "solanaEndpoint": "https://api.mainnet-beta.solana.com", + "stripeSecretKey": "" } diff --git a/identity-service/src/config.js b/identity-service/src/config.js index 45b6cff12af..5921ae7a5c2 100644 --- a/identity-service/src/config.js +++ b/identity-service/src/config.js @@ -809,6 +809,12 @@ const config = convict({ format: String, env: 'cognitoRetrySecret', default: '' + }, + stripeSecretKey: { + doc: 'Secret key for Stripe Crypto On-Ramp Integration', + format: String, + env: 'stripeSecretKey', + default: '' } }) diff --git a/identity-service/src/routes/stripe.js b/identity-service/src/routes/stripe.js new file mode 100644 index 00000000000..f19d662078c --- /dev/null +++ b/identity-service/src/routes/stripe.js @@ -0,0 +1,64 @@ +const { + handleResponse, + successResponse, + errorResponse, + errorResponseBadRequest +} = require('../apiHelpers') +const config = require('../config') +const axios = require('axios') +const axiosHttpAdapter = require('axios/lib/adapters/http') +const { logger } = require('../logging') + +const createAuthHeader = () => { + const secretKey = config.get('stripeSecretKey') + return { + Authorization: `Basic ${Buffer.from(secretKey + ':', 'utf-8').toString( + 'base64' + )}` + } +} + +module.exports = function (app) { + app.post( + '/stripe/session', + handleResponse(async (req) => { + const { destinationWallet, amount } = req.body + + if (!destinationWallet || !amount) { + return errorResponseBadRequest('Missing destinationWallet or amount') + } + + const urlEncodedData = new URLSearchParams({ + customer_wallet_address: destinationWallet, + 'transaction_details[wallet_address]': destinationWallet, + 'transaction_details[supported_destination_networks][]': 'solana', + 'transaction_details[supported_destination_currencies][]': 'sol', + 'transaction_details[destination_network]': 'solana', + 'transaction_details[destination_currency]': 'sol', + 'transaction_details[destination_exchange_amount]': amount + }) + + try { + const req = { + adapter: axiosHttpAdapter, + url: 'https://api.stripe.com/v1/crypto/onramp_sessions', + method: 'POST', + headers: { + // Required form-urlencoded for the Stripe API + 'Content-Type': 'application/x-www-form-urlencoded', + ...createAuthHeader() + }, + data: urlEncodedData + } + const response = await axios(req) + return successResponse(response.data) + } catch (e) { + logger.error('Failed to create Stripe session:', e.response.data) + return errorResponse( + e.response.status, + 'Failed to create Stripe session' + ) + } + }) + ) +}