2022-01-18 17:56:47 +02:00
const logging = require ( '@tryghost/logging' ) ;
const tpl = require ( '@tryghost/tpl' ) ;
2021-10-04 13:18:22 +02:00
2022-01-18 17:56:47 +02:00
const messages = {
remoteWebhooksInDevelopment : 'Cannot use remote webhooks in development. See https://ghost.org/docs/webhooks/#stripe-webhooks for developing with Stripe.'
} ;
// @TODO Refactor to a class w/ constructor
2022-02-15 11:47:00 +02:00
/ * *
* @ typedef { object } StripeURLConfig
* @ prop { string } checkoutSessionSuccessUrl
* @ prop { string } checkoutSessionCancelUrl
* @ prop { string } checkoutSetupSessionSuccessUrl
* @ prop { string } checkoutSetupSessionCancelUrl
* /
2021-10-04 13:18:22 +02:00
module . exports = {
2022-01-18 17:56:47 +02:00
getConfig ( settings , config , urlUtils ) {
2022-02-15 11:47:00 +02:00
/ * *
* @ returns { StripeURLConfig }
* /
function getStripeUrlConfig ( ) {
const siteUrl = urlUtils . getSiteUrl ( ) ;
const checkoutSuccessUrl = new URL ( siteUrl ) ;
checkoutSuccessUrl . searchParams . set ( 'stripe' , 'success' ) ;
const checkoutCancelUrl = new URL ( siteUrl ) ;
checkoutCancelUrl . searchParams . set ( 'stripe' , 'cancel' ) ;
const billingSuccessUrl = new URL ( siteUrl ) ;
billingSuccessUrl . searchParams . set ( 'stripe' , 'billing-update-success' ) ;
const billingCancelUrl = new URL ( siteUrl ) ;
billingCancelUrl . searchParams . set ( 'stripe' , 'billing-update-cancel' ) ;
return {
checkoutSessionSuccessUrl : checkoutSuccessUrl . href ,
checkoutSessionCancelUrl : checkoutCancelUrl . href ,
checkoutSetupSessionSuccessUrl : billingSuccessUrl . href ,
checkoutSetupSessionCancelUrl : billingCancelUrl . href
} ;
}
2021-10-04 13:18:22 +02:00
/ * *
* @ param { 'direct' | 'connect' } type - The "type" of keys to fetch from settings
* @ returns { { publicKey : string , secretKey : string } | null }
* /
function getStripeKeys ( type ) {
const secretKey = settings . get ( ` stripe_ ${ type === 'connect' ? 'connect_' : '' } secret_key ` ) ;
const publicKey = settings . get ( ` stripe_ ${ type === 'connect' ? 'connect_' : '' } publishable_key ` ) ;
if ( ! secretKey || ! publicKey ) {
return null ;
}
return {
secretKey ,
publicKey
} ;
}
/ * *
* @ returns { { publicKey : string , secretKey : string } | null }
* /
function getActiveStripeKeys ( ) {
const stripeDirect = config . get ( 'stripeDirect' ) ;
if ( stripeDirect ) {
return getStripeKeys ( 'direct' ) ;
}
const connectKeys = getStripeKeys ( 'connect' ) ;
if ( ! connectKeys ) {
return getStripeKeys ( 'direct' ) ;
}
return connectKeys ;
}
const keys = getActiveStripeKeys ( ) ;
if ( ! keys ) {
return null ;
}
2022-01-18 17:56:47 +02:00
const env = config . get ( 'env' ) ;
let webhookSecret = process . env . WEBHOOK _SECRET ;
if ( env !== 'production' ) {
if ( ! webhookSecret ) {
webhookSecret = 'DEFAULT_WEBHOOK_SECRET' ;
logging . warn ( tpl ( messages . remoteWebhooksInDevelopment ) ) ;
}
}
const webhookHandlerUrl = new URL ( 'members/webhooks/stripe/' , urlUtils . getSiteUrl ( ) ) ;
2022-02-15 11:47:00 +02:00
const urls = getStripeUrlConfig ( ) ;
2021-10-04 13:18:22 +02:00
return {
2022-02-15 11:47:00 +02:00
... keys ,
... urls ,
2022-01-18 17:56:47 +02:00
enablePromoCodes : config . get ( 'enableStripePromoCodes' ) ,
webhookSecret : webhookSecret ,
webhookHandlerUrl : webhookHandlerUrl . href
2021-10-04 13:18:22 +02:00
} ;
}
} ;