Skip to content

Commit

Permalink
[PAY-568] Add route to identity to create a Stripe session (#3879)
Browse files Browse the repository at this point in the history
* Add route to identity to create stripe session

* Add config for stripe secret key

* Lint fix

* Add stripeSecretKey to config schema
  • Loading branch information
rickyrombo committed Sep 20, 2022
1 parent d9c8474 commit 77fcfc3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 3 additions & 2 deletions identity-service/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"redisHost": "identity-redis",
"redisPort": 6379,
"logLevel": "debug",
"ipdataAPIKey":"",
"ipdataAPIKey": "",
"twitterAPIKey": "",
"twitterAPISecret": "",
"tikTokAPIKey": "",
Expand Down Expand Up @@ -70,5 +70,6 @@
"hCaptchaSecret": "",
"solanaFeePayerWallets": "[]",
"sentryDSN": "",
"solanaEndpoint": "https://api.mainnet-beta.solana.com"
"solanaEndpoint": "https://api.mainnet-beta.solana.com",
"stripeSecretKey": ""
}
6 changes: 6 additions & 0 deletions identity-service/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
}
})

Expand Down
64 changes: 64 additions & 0 deletions identity-service/src/routes/stripe.js
Original file line number Diff line number Diff line change
@@ -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'
)
}
})
)
}

0 comments on commit 77fcfc3

Please sign in to comment.