2021-09-26 22:08:02 +00:00
import { getConfig } from "blitz" ;
import got from "got" ;
2021-10-03 16:38:39 +00:00
import type {
PaddleSdkSubscriptionCreatedEvent ,
PaddleSdkCancelSubscriptionRequest ,
PaddleSdkUpdateSubscriptionRequest ,
} from "@devoxa/paddle-sdk" ;
2021-10-01 21:04:12 +00:00
import { PaddleSdk , PaddleSdkSubscriptionStatus , stringifyMetadata } from "@devoxa/paddle-sdk" ;
import { SubscriptionStatus } from "db" ;
2021-09-26 22:08:02 +00:00
const { publicRuntimeConfig , serverRuntimeConfig } = getConfig ( ) ;
2021-10-03 16:38:39 +00:00
const vendorId = publicRuntimeConfig . paddle . vendorId ;
const vendorAuthCode = serverRuntimeConfig . paddle . apiKey ;
2021-09-26 22:08:02 +00:00
2021-10-01 21:04:12 +00:00
export const paddleSdk = new PaddleSdk ( {
2021-10-25 09:44:43 +00:00
publicKey :
"-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwUABB5uOIIAyJwrJfGIj\nOwueWs4itGIm/5om5HZhbgjGM0f7dnZVBQCCfE+1Dvi9IPkrQaPR83M84JYxRDOA\nbExyg79Lo5nrueinuKtkTOVOCKWmXgtBiuRHGYY90lMhg2I3qY3/DnZDyIyHuprT\n/WU5nVSXP2LLPbOqUjZJysOWIJ2IPbz9no5QdJyge5SudCBa58xa24Us3PiVpmsk\nJ8y0AGxF9AfxFJv/vcUJ0SMnzB6ddWOMcf/fHJwF2urOqU6c66IzJFKf7aytny4n\nqPpNPOkttazUV8ycCXjJAer9peIoTYYcSFqs2pWxg+YSP7EqSSOsqGCuux98q1m7\nBq+eojt9HFMG9ZNaE258gO7G9FhdnLPyMGZC0Q2EeTTohhRNzdzo0iI9BEbVVmLp\nefUZ33IuyDCxtYIiw5vhj/CUBsYhod16GAWLuuCoIfyaGPbaLGGWsecmIsbJx/je\n2MJuHD8PB1f5T9NSLC9MPlptoYlBJjhz7+qBPR8E13aKnRnu7OyDtQ4wi74zPxIr\n0DNcwlbWT3HL7CUDr40glLGx5OxFw4wdIUm6hLy1iC+8vHnY+fyQv+JtaDChP49i\nlSzPrswGaiU/rTkOB6HtGlOci5Skgr1ue1j/lO7y2NcLZMbwDRldGhYXpo0aLQvI\nPzcoXd4HrmEM2friEPLwcn8CAwEAAQ==\n-----END PUBLIC KEY-----" ,
2021-10-03 16:38:39 +00:00
vendorId ,
vendorAuthCode ,
2021-10-01 21:04:12 +00:00
metadataCodec : stringifyMetadata ( ) ,
} ) ;
export type Metadata = { organizationId : string } ;
export function translateSubscriptionStatus (
2021-10-03 18:56:31 +00:00
status : PaddleSdkSubscriptionCreatedEvent < Metadata > [ "status" ] ,
2021-10-01 21:04:12 +00:00
) : SubscriptionStatus {
switch ( status ) {
case PaddleSdkSubscriptionStatus . ACTIVE :
return SubscriptionStatus . active ;
case PaddleSdkSubscriptionStatus . CANCELLED :
return SubscriptionStatus . deleted ;
case PaddleSdkSubscriptionStatus . PAUSED :
return SubscriptionStatus . paused ;
case PaddleSdkSubscriptionStatus . PAST_DUE :
return SubscriptionStatus . past_due ;
case PaddleSdkSubscriptionStatus . TRIALING :
return SubscriptionStatus . trialing ;
default :
throw new Error ( "unreachable" ) ;
}
}
2021-09-30 22:59:35 +00:00
type GetPaymentsParams = {
2021-10-01 21:04:12 +00:00
subscriptionId : number ;
2021-09-30 22:59:35 +00:00
} ;
export async function getPayments ( { subscriptionId } : GetPaymentsParams ) {
type Payment = {
id : number ;
subscription_id : number ;
amount : number ;
currency : string ;
payout_date : string ;
is_paid : number ;
is_one_off_charge : boolean ;
receipt_url? : string ;
} ;
type PaymentsSuccessResponse = {
success : true ;
response : Payment [ ] ;
} ;
type PaymentsErrorResponse = {
success : false ;
error : {
code : number ;
message : string ;
} ;
} ;
type PaymentsResponse = PaymentsSuccessResponse | PaymentsErrorResponse ;
2021-10-03 16:38:39 +00:00
const { body } = await got . post < PaymentsResponse > ( "https://vendors.paddle.com/api/2.0/subscription/payments" , {
json : {
subscription_id : subscriptionId ,
vendor_id : vendorId ,
vendor_auth_code : vendorAuthCode ,
} ,
responseType : "json" ,
} ) ;
2021-09-30 22:59:35 +00:00
if ( ! body . success ) {
throw new Error ( body . error . message ) ;
}
return body . response ;
}
2021-10-03 16:38:39 +00:00
export async function updateSubscriptionPlan ( {
subscriptionId ,
productId ,
shouldProrate = true ,
2021-10-03 16:41:43 +00:00
shouldKeepModifiers = true ,
2021-10-24 22:32:33 +00:00
shouldMakeImmediatePayment = true ,
2021-10-03 16:38:39 +00:00
} : PaddleSdkUpdateSubscriptionRequest < Metadata > ) {
return paddleSdk . updateSubscription ( {
subscriptionId ,
productId ,
shouldProrate ,
2021-10-03 16:41:43 +00:00
shouldKeepModifiers ,
2021-10-24 22:32:33 +00:00
shouldMakeImmediatePayment ,
2021-09-26 22:08:02 +00:00
} ) ;
}
2021-10-03 16:38:39 +00:00
export async function cancelPaddleSubscription ( { subscriptionId } : PaddleSdkCancelSubscriptionRequest ) {
return paddleSdk . cancelSubscription ( { subscriptionId } ) ;
2021-09-26 22:08:02 +00:00
}