-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripePayment.ts
31 lines (29 loc) · 960 Bytes
/
stripePayment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { GraphQLError } from "graphql";
import Product from "../../../../Schema/Product/Product.model";
import { MutationStripePaymentArgs } from "../../../Types/types";
const stripePayment = async (
_: any,
{ productIDs }: MutationStripePaymentArgs,
{ stripe }
) => {
try {
if (productIDs.length === 0)
throw new Error("order must have at least one product");
var amount = 0;
for (let i = 0; i < productIDs.length; i++) {
var product = await Product.findById(productIDs[i], { price: 1 });
amount += product.price;
}
if (amount === 0) throw new Error("amount must be >= 0");
const paymentIntent = await stripe.paymentIntents.create({
amount: amount * 100,
currency: "eur",
});
return paymentIntent.client_secret;
} catch (e: any) {
console.log("error while executing the stripe payment");
throw new GraphQLError(e.message);
return null;
}
};
export default stripePayment;