-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sourcefuse/gh-stripe
feat(provider): add stripe billing integration
- Loading branch information
Showing
16 changed files
with
14,515 additions
and
14,138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './chargebee'; | ||
export * from './stripe'; |
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 @@ | ||
import {AnyObject} from '@loopback/repository'; | ||
import {IAdapter} from '../../../../types'; | ||
import {IStripeCustomer} from '../type'; | ||
export class StripeCustomerAdapter implements IAdapter<IStripeCustomer> { | ||
constructor() {} | ||
|
||
adaptToModel(resp: AnyObject): IStripeCustomer { | ||
const {firstName, lastName} = splitName(resp.name); | ||
const res: IStripeCustomer = { | ||
id: resp.id, | ||
firstName: firstName, | ||
lastName: lastName, | ||
email: resp.email, | ||
phone: resp.phone, | ||
billingAddress: { | ||
firstName: firstName, | ||
lastName: lastName, | ||
email: resp.email, | ||
phone: resp.phone, | ||
line1: resp.address?.line1, | ||
line2: resp.address?.line2, | ||
city: resp.address?.city, | ||
state: resp.address?.state, | ||
zip: resp.address?.postal_code, | ||
country: resp.address?.country, | ||
}, | ||
}; | ||
return res; | ||
} | ||
adaptFromModel(data: IStripeCustomer): AnyObject { | ||
return { | ||
name: data.firstName + ' ' + data.lastName, | ||
email: data.email, | ||
phone: data.phone, | ||
address: { | ||
line1: data.billingAddress?.line1, | ||
line2: data.billingAddress?.line2, | ||
city: data.billingAddress?.city, | ||
state: data.billingAddress?.state, | ||
country: data.billingAddress?.country, | ||
/* eslint-disable-next-line @typescript-eslint/naming-convention */ | ||
postal_code: data.billingAddress?.zip, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
function splitName(fullName: string): {firstName: string; lastName: string} { | ||
const nameParts = fullName.trim().split(' '); | ||
|
||
if (nameParts.length === 1) { | ||
// If only first name is provided | ||
return { | ||
firstName: nameParts[0], | ||
lastName: '', | ||
}; | ||
} else { | ||
// If both first and last names are provided | ||
return { | ||
firstName: nameParts[0], | ||
lastName: nameParts.slice(1).join(' '), // Handles multiple middle/last names | ||
}; | ||
} | ||
} |
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,3 @@ | ||
export * from './customer.adapter'; | ||
export * from './invoice.adapter'; | ||
export * from './payment-source.adapter'; |
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,72 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import {AnyObject} from '@loopback/repository'; | ||
import {IAdapter} from '../../../../types'; | ||
import {IStripeInvoice} from '../type'; | ||
export class StripeInvoiceAdapter implements IAdapter<IStripeInvoice> { | ||
constructor() {} | ||
|
||
adaptToModel(resp: AnyObject): IStripeInvoice { | ||
return { | ||
id: resp.id, | ||
customerId: resp.customer, | ||
status: resp.status, | ||
currencyCode: resp.currency, | ||
shippingAddress: resp.shipping_details?.address | ||
? { | ||
firstName: resp.customer_name?.split(' ')[0] || '', | ||
lastName: resp.customer_name?.split(' ')[1] || '', | ||
line1: resp.shipping_details.address.line1, | ||
line2: resp.shipping_details.address.line2, | ||
city: resp.shipping_details.address.city, | ||
state: resp.shipping_details.address.state, | ||
zip: resp.shipping_details.address.postal_code, | ||
country: resp.shipping_details.address.country, | ||
phone: resp.shipping_details.phone, | ||
email: resp.customer_email, | ||
} | ||
: undefined, | ||
charges: resp.lines?.data.map( | ||
( | ||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
lineItem: any, // NOSONAR | ||
) => ({ | ||
amount: lineItem.amount / 100, // divided by 100 because the lineItem.amount is coming in cents | ||
description: lineItem.description, | ||
}), | ||
), | ||
options: { | ||
autoAdvnace: resp.auto_advance || false, | ||
}, | ||
}; | ||
} | ||
adaptFromModel(data: IStripeInvoice): AnyObject { | ||
const shippingDetails: AnyObject = data.shippingAddress | ||
? { | ||
shipping_details: { | ||
name: [ | ||
data.shippingAddress.firstName, | ||
data.shippingAddress.lastName, | ||
] | ||
.join(' ') | ||
.trim(), | ||
address: { | ||
line1: data.shippingAddress.line1, | ||
line2: data.shippingAddress.line2, | ||
city: data.shippingAddress.city, | ||
state: data.shippingAddress.state, | ||
postal_code: data.shippingAddress.zip, | ||
country: data.shippingAddress.country, | ||
}, | ||
phone: data.shippingAddress.phone, | ||
}, | ||
} | ||
: {}; | ||
|
||
return { | ||
customer: data.customerId, | ||
currency: data.currencyCode, | ||
...shippingDetails, | ||
auto_advance: data.options?.autoAdvnace ?? false, | ||
}; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/providers/sdk/stripe/adapter/payment-source.adapter.ts
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,28 @@ | ||
import {AnyObject} from '@loopback/repository'; | ||
import {IAdapter} from '../../../../types'; | ||
import {IStripePaymentSource} from '../type'; | ||
export class StripePaymentAdapter implements IAdapter<IStripePaymentSource> { | ||
constructor() {} | ||
|
||
adaptToModel(resp: AnyObject): IStripePaymentSource { | ||
return { | ||
id: resp.id, | ||
customerId: resp.customer, | ||
card: resp.card | ||
? { | ||
gatewayAccountId: resp.card.fingerprint, // Using fingerprint as an identifier | ||
number: '**** **** **** ' + resp.card.last4, // Masked card number | ||
expiryMonth: resp.card.exp_month, | ||
expiryYear: resp.card.exp_year, | ||
cvv: '***', // CVV is not returned by Stripe, so it should be masked | ||
} | ||
: undefined, | ||
options: { | ||
token: resp.id, // Use the payment method id as the token if needed | ||
}, | ||
}; | ||
} | ||
adaptFromModel(data: IStripePaymentSource): AnyObject { | ||
return {}; // This is intentional | ||
} | ||
} |
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,5 @@ | ||
export * from './adapter'; | ||
export * from './key'; | ||
export * from './stripe.provider'; | ||
export * from './stripe.service'; | ||
export * from './type'; |
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,12 @@ | ||
// Copyright (c) 2023 Sourcefuse Technologies | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
import {BindingKey} from '@loopback/core'; | ||
import {StripeConfig} from './type'; | ||
|
||
export namespace StripeBindings { | ||
export const config = BindingKey.create<StripeConfig>( | ||
'sf.provider.stripe.config', | ||
); | ||
} |
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,15 @@ | ||
import {Provider, inject} from '@loopback/core'; | ||
import {StripeBindings} from './key'; | ||
import {StripeService} from './stripe.service'; | ||
import {IStripeService, StripeConfig} from './type'; | ||
|
||
export class StripeServiceProvider implements Provider<IStripeService> { | ||
constructor( | ||
@inject(StripeBindings.config, {optional: true}) | ||
private readonly stripeConfig: StripeConfig, | ||
) {} | ||
|
||
value(): IStripeService { | ||
return new StripeService(this.stripeConfig); | ||
} | ||
} |
Oops, something went wrong.