@stacksjs/payments 0.70.369 → 0.70.371
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{db}from"@stacksjs/database";import{PaymentMethod}from"@stacksjs/orm";import{stripe}from"../drivers/stripe";export const managePaymentMethod=(()=>{async function addPaymentMethod(user,paymentMethod){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");let stripePaymentMethod;if(typeof paymentMethod==="string")stripePaymentMethod=await stripe.paymentMethods.retrieve(paymentMethod);else stripePaymentMethod=paymentMethod;if(stripePaymentMethod.customer!==user.stripe_id)stripePaymentMethod=await stripe.paymentMethods.attach(stripePaymentMethod.id,{customer:user.stripe_id});await storePaymentMethod(user,stripePaymentMethod);return stripePaymentMethod}async function setUserDefaultPayment(user,paymentMethodId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");const paymentMethod=await stripe.paymentMethods.retrieve(paymentMethodId),paymentMethodModel=await db.selectFrom("payment_methods").where("provider_id","=",paymentMethodId).selectAll().executeTakeFirst();if(!user.stripe_id)throw Error("User has no Stripe ID");if(paymentMethod.customer!==user.stripe_id)await stripe.paymentMethods.attach(paymentMethod.id,{customer:user.stripe_id});const updatedCustomer=await stripe.customers.update(user.stripe_id,{invoice_settings:{default_payment_method:paymentMethodId}});await db.updateTable("payment_methods").set({is_default:!1}).where("user_id","=",user.id).executeTakeFirst();if(paymentMethodModel)await updateDefault(paymentMethodModel);return updatedCustomer}async function setDefaultPaymentMethod(user,paymentId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");const pm=await db.selectFrom("payment_methods").where("id","=",paymentId).selectAll().executeTakeFirst();if(!pm?.provider_id)throw Error(`Payment method with id ${paymentId} not found`);const paymentMethod=await stripe.paymentMethods.retrieve(String(pm.provider_id));if(paymentMethod.customer!==user.stripe_id)await stripe.paymentMethods.attach(paymentMethod.id,{customer:user.stripe_id});if(!user.stripe_id)throw Error("User has no Stripe ID");const updatedCustomer=await stripe.customers.update(user.stripe_id,{invoice_settings:{default_payment_method:String(pm.provider_id)}});await db.updateTable("payment_methods").set({is_default:!1}).where("user_id","=",user.id).executeTakeFirst();await db.updateTable("payment_methods").set({is_default:!0}).where("id","=",paymentId).executeTakeFirst();return updatedCustomer}async function storePaymentMethod(user,paymentMethod){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");if(!paymentMethod.card?.brand||!paymentMethod.card?.exp_year||!paymentMethod.card?.exp_month)throw Error("Invalid payment method: missing required card details");const method={type:"card",
|
|
1
|
+
import{db}from"@stacksjs/database";import{PaymentMethod}from"@stacksjs/orm";import{stripe}from"../drivers/stripe";export const managePaymentMethod=(()=>{async function addPaymentMethod(user,paymentMethod){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");let stripePaymentMethod;if(typeof paymentMethod==="string")stripePaymentMethod=await stripe.paymentMethods.retrieve(paymentMethod);else stripePaymentMethod=paymentMethod;if(stripePaymentMethod.customer!==user.stripe_id)stripePaymentMethod=await stripe.paymentMethods.attach(stripePaymentMethod.id,{customer:user.stripe_id});await storePaymentMethod(user,stripePaymentMethod);return stripePaymentMethod}async function setUserDefaultPayment(user,paymentMethodId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");const paymentMethod=await stripe.paymentMethods.retrieve(paymentMethodId),paymentMethodModel=await db.selectFrom("payment_methods").where("provider_id","=",paymentMethodId).selectAll().executeTakeFirst();if(!user.stripe_id)throw Error("User has no Stripe ID");if(paymentMethod.customer!==user.stripe_id)await stripe.paymentMethods.attach(paymentMethod.id,{customer:user.stripe_id});const updatedCustomer=await stripe.customers.update(user.stripe_id,{invoice_settings:{default_payment_method:paymentMethodId}});await db.updateTable("payment_methods").set({is_default:!1}).where("user_id","=",user.id).executeTakeFirst();if(paymentMethodModel)await updateDefault(paymentMethodModel);return updatedCustomer}async function setDefaultPaymentMethod(user,paymentId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");const pm=await db.selectFrom("payment_methods").where("id","=",paymentId).selectAll().executeTakeFirst();if(!pm?.provider_id)throw Error(`Payment method with id ${paymentId} not found`);const paymentMethod=await stripe.paymentMethods.retrieve(String(pm.provider_id));if(paymentMethod.customer!==user.stripe_id)await stripe.paymentMethods.attach(paymentMethod.id,{customer:user.stripe_id});if(!user.stripe_id)throw Error("User has no Stripe ID");const updatedCustomer=await stripe.customers.update(user.stripe_id,{invoice_settings:{default_payment_method:String(pm.provider_id)}});await db.updateTable("payment_methods").set({is_default:!1}).where("user_id","=",user.id).executeTakeFirst();await db.updateTable("payment_methods").set({is_default:!0}).where("id","=",paymentId).executeTakeFirst();return updatedCustomer}async function storePaymentMethod(user,paymentMethod){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");if(!paymentMethod.card?.brand||!paymentMethod.card?.exp_year||!paymentMethod.card?.exp_month)throw Error("Invalid payment method: missing required card details");const method={type:"card",lastFour:String(paymentMethod.card.last4),brand:paymentMethod.card.brand,expYear:paymentMethod.card.exp_year,expMonth:paymentMethod.card.exp_month,user_id:user.id,providerId:paymentMethod.id};if(paymentMethod.customer!==user.stripe_id)await stripe.paymentMethods.attach(paymentMethod.id,{customer:user.stripe_id});return await PaymentMethod.create(method)}async function updateDefault(paymentMethodModel){return await paymentMethodModel.update({is_default:!0})}async function deletePaymentMethod(user,paymentMethodId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");const pm=typeof paymentMethodId==="string"?await db.selectFrom("payment_methods").where("provider_id","=",paymentMethodId).selectAll().executeTakeFirst():await PaymentMethod.find(paymentMethodId);if(!pm?.provider_id)throw Error(`Payment method with id ${paymentMethodId} not found`);if((await stripe.paymentMethods.retrieve(String(pm.provider_id))).customer!==user.stripe_id)throw Error("Payment method does not belong to this customer");await pm.delete();return await stripe.paymentMethods.detach(String(pm.provider_id))}async function updatePaymentMethod(user,paymentMethodId,updateParams){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");if((await stripe.paymentMethods.retrieve(paymentMethodId)).customer!==user.stripe_id)throw Error("Payment method does not belong to this customer");return await stripe.paymentMethods.update(paymentMethodId,updateParams)}async function listPaymentMethods(user){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");return await db.selectFrom("payment_methods").selectAll().where("user_id","=",user.id).execute()}async function retrievePaymentMethod(user,paymentMethodId){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");return await db.selectFrom("payment_methods").where("id","=",paymentMethodId).selectAll().executeTakeFirst()}async function retrieveDefaultPaymentMethod(user){if(!user.hasStripeId())throw Error("Customer does not exist in Stripe");return await PaymentMethod.where("user_id",user.id).where("is_default",!0).first()}return{addPaymentMethod,deletePaymentMethod,retrieveDefaultPaymentMethod,updatePaymentMethod,listPaymentMethods,setDefaultPaymentMethod,storePaymentMethod,retrievePaymentMethod,setUserDefaultPayment}})();
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/payments",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.70.
|
|
5
|
+
"version": "0.70.371",
|
|
6
6
|
"description": "The Stacks payments package. Currently supporting Stripe.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
}
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@stacksjs/config": "0.70.
|
|
75
|
+
"@stacksjs/config": "0.70.371",
|
|
76
76
|
"better-dx": "^0.2.17",
|
|
77
|
-
"@stacksjs/utils": "0.70.
|
|
77
|
+
"@stacksjs/utils": "0.70.371",
|
|
78
78
|
"@stripe/stripe-js": "^9.10.0",
|
|
79
79
|
"stripe": "22.3.2"
|
|
80
80
|
}
|