-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
d9c8474
commit 77fcfc3
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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' | ||
) | ||
} | ||
}) | ||
) | ||
} |