-
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.
gei-stripe 0.3.0 with collecting payment method and stripe connect
- Loading branch information
Showing
23 changed files
with
1,064 additions
and
350 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
18 changes: 18 additions & 0 deletions
18
packages/integrations/gei-stripe/src/Mutation/attachPaymentMethod.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,18 @@ | ||
import { FieldResolveInput } from 'stucco-js'; | ||
import { resolverFor } from '../zeus/index.js'; | ||
import { newStripe } from '../utils/utils.js'; | ||
|
||
export const handler = async (input: FieldResolveInput) => | ||
resolverFor( | ||
'Mutation', | ||
'attachPaymentMethod', | ||
async ({ payload: { customerId, paymentMethodId } }) => { | ||
const stripe = newStripe(); | ||
|
||
const attachedPaymentMethod = await stripe.paymentMethods.attach(paymentMethodId, { | ||
customer: customerId, | ||
}); | ||
|
||
return true; | ||
}, | ||
)(input.arguments, input.source); |
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
35 changes: 35 additions & 0 deletions
35
packages/integrations/gei-stripe/src/Mutation/createConnectAccount.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,35 @@ | ||
import { FieldResolveInput } from 'stucco-js'; | ||
import { resolverFor } from '../zeus/index.js'; | ||
import { newStripe } from '../utils/utils.js'; | ||
|
||
export const handler = async (input: FieldResolveInput) => | ||
resolverFor( | ||
'Mutation', | ||
'createConnectAccount', | ||
async ({ payload: { type, country, email, bankAccount, business_type } }) => { | ||
const stripe = newStripe(); | ||
|
||
const account = await stripe.accounts.create({ | ||
type: type, | ||
country, | ||
email, | ||
business_type, | ||
}); | ||
|
||
const accountToken = await stripe.tokens.create({ | ||
bank_account: { | ||
country, | ||
currency: bankAccount.currency, | ||
account_holder_name: bankAccount.account_holder_name, | ||
account_holder_type: bankAccount.account_holder_type, | ||
account_number: bankAccount.account_number, | ||
}, | ||
}); | ||
|
||
const stripeBankAccount = await stripe.accounts.createExternalAccount(account.id, { | ||
external_account: accountToken.id, | ||
}); | ||
|
||
return true; | ||
}, | ||
)(input.arguments, input.source); |
85 changes: 85 additions & 0 deletions
85
packages/integrations/gei-stripe/src/Mutation/createNewUserCheckoutSession.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,85 @@ | ||
import Stripe from 'stripe'; | ||
import { newStripe } from '../utils/utils.js'; | ||
import { resolverFor } from '../zeus/index.js'; | ||
import { FieldResolveInput } from 'stucco-js'; | ||
type item = { | ||
price: string; | ||
quantity?: number; | ||
}; | ||
export const handler = async (input: FieldResolveInput) => | ||
resolverFor( | ||
'Mutation', | ||
'createNewUserCheckoutSession', | ||
async ({ payload: { successUrl, cancelUrl, products, applicationFee } }) => { | ||
const stripe = newStripe(); | ||
const subscriptionItems: item[] = []; | ||
const oneTimePaymentItems: item[] = []; | ||
|
||
let totalAmount = 0; | ||
|
||
await Promise.all( | ||
products.map(async (product) => { | ||
let price; | ||
if (product.productId.startsWith('price_')) { | ||
price = await stripe.prices.retrieve(product.productId); | ||
} else if (product.productId.startsWith('prod_')) { | ||
const { default_price } = await stripe.products.retrieve(product.productId); | ||
if (!default_price) { | ||
throw new Error('Cannot find product ' + product.productId + ' default price'); | ||
} | ||
price = | ||
typeof default_price === 'string' | ||
? await stripe.prices.retrieve(default_price) | ||
: await stripe.prices.retrieve(default_price.id); | ||
} else { | ||
throw new Error('Invalid product ID: ' + product.productId); | ||
} | ||
|
||
const quantity = price.type === 'recurring' || price.recurring?.usage_type === 'metered' ? 1 : product.quantity; | ||
const item = { price: price.id, ...(quantity && { quantity }) }; | ||
|
||
totalAmount += (price.unit_amount || 0) * (quantity || 0); | ||
|
||
if (price.type === 'recurring') { | ||
subscriptionItems.push(item); | ||
} else { | ||
oneTimePaymentItems.push(item); | ||
} | ||
}), | ||
); | ||
|
||
const applicationFeeAmount = applicationFee ? Math.round((totalAmount * applicationFee.feePercentage) / 100) : 0; | ||
|
||
if (subscriptionItems.length > 0 && oneTimePaymentItems.length > 0) { | ||
throw new Error('Cannot handle subscription items and one-time payment items in the same request'); | ||
} | ||
|
||
if (subscriptionItems.length > 0 || oneTimePaymentItems.length > 0) { | ||
const lineItems = subscriptionItems.length > 0 ? subscriptionItems : oneTimePaymentItems; | ||
const mode = subscriptionItems.length > 0 ? 'subscription' : 'payment'; | ||
|
||
const sessionData: Stripe.Checkout.SessionCreateParams = { | ||
success_url: successUrl, | ||
cancel_url: cancelUrl, | ||
line_items: lineItems, | ||
mode: mode, | ||
tax_id_collection: { enabled: true }, | ||
automatic_tax: { enabled: true }, | ||
billing_address_collection: 'required', | ||
}; | ||
|
||
if (applicationFeeAmount > 0 && applicationFee) { | ||
sessionData.payment_intent_data = { | ||
application_fee_amount: applicationFeeAmount, | ||
transfer_data: { | ||
destination: applicationFee.connectAccountId, | ||
}, | ||
}; | ||
} | ||
|
||
const session = await stripe.checkout.sessions.create(sessionData); | ||
return session.url; | ||
} | ||
}, | ||
)(input.arguments, input.source); | ||
|
79 changes: 0 additions & 79 deletions
79
packages/integrations/gei-stripe/src/Mutation/createNewUserPaymentSession.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.