Skip to content

Commit cea45aa

Browse files
author
Daveed
committedJan 4, 2025·
Creates prices & test stripe payment links correctly
1 parent bedff94 commit cea45aa

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed
 

‎src/api/markket/controllers/markket.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { version } from '../../../../package.json';
55
const { createCoreController } = require('@strapi/strapi').factories;
66
const modelId = "api::markket.markket";
77

8+
import { createPaymentLinkWithPriceIds } from '../services/stripe';
9+
810
const NODE_ENV = process.env.NODE_ENV || 'development';
911
const STRIPE_PUBLIC_KEY = process.env.STRIPE_PUBLIC_KEY || 'n/a';
1012
const SENDGRID_REPLY_TO_EMAIL = process.env.SENDGRID_REPLY_TO_EMAIL || 'n/a';
@@ -24,22 +26,40 @@ module.exports = createCoreController(modelId, ({ strapi }) => ({
2426
});
2527
},
2628
async create(ctx: any) {
27-
console.info('markket.create');
29+
const body = JSON.parse(ctx.request.body);
30+
let message = 'action completed';
31+
console.log(`markket.create:${body.action}`, { body });
32+
33+
let link = null;
34+
if (body?.action === 'stripe.link') {
35+
link = await createPaymentLinkWithPriceIds(body?.prices || []);
36+
message = 'stripe link created';
37+
}
38+
39+
if (body?.action === 'stripe.webhook') {
40+
// @TODO Store transaction record & send pertinent notifications
41+
}
42+
43+
// Storing record of transaction
2844
await strapi.service(modelId).create({
2945
locale: 'en',
3046
data: {
31-
Key: "markket.create",
32-
Content: ctx.request.body,
47+
Key: `markket.create.${body?.action || 'default'}`,
48+
Content: {
49+
link,
50+
produt: body?.product,
51+
total: body?.total,
52+
},
3353
user_key_or_id: "", // @TODO: Review authorization, token or related user
3454
}
3555
});
36-
// @TODO: send notification emails, perform STRIPE actions, etc, here
56+
3757
ctx.send({
38-
message: 'This is the create endpoint',
58+
message: `action ${body?.action} completed`,
3959
data: {
40-
info: 'Markket.place is an international commercial community',
41-
version,
60+
info: message,
61+
link,
4262
},
4363
});
44-
}
64+
},
4565
}));

‎src/api/markket/routes/markket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ module.exports = {
2121
policies: [],
2222
middlewares: [],
2323
},
24-
}
24+
},
2525
],
2626
};

‎src/api/markket/services/stripe.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
2+
const STRIPE_SECRET_TEST_KEY = process.env.STRIPE_SECRET_TEST_KEY;
3+
4+
// const stripe = require('stripe')(STRIPE_SECRET_KEY);
5+
const stripeTest = require('stripe')(STRIPE_SECRET_TEST_KEY);
6+
7+
/**
8+
* Create a payment link with price ids, or create new prices for the custom amounts
9+
* @param prices
10+
* @returns
11+
*/
12+
export const createPaymentLinkWithPriceIds = async (prices: { id: string, quantity: number }[]) => {
13+
console.log({ prices });
14+
15+
const line_items = [];
16+
17+
const custom_price: any = prices.find((price: any) => price.product);
18+
const set_price: any = prices.find((price: any) => price.price);
19+
20+
if (custom_price?.product) {
21+
const new_price = await stripeTest.prices.create({
22+
currency: 'usd',
23+
unit_amount: (custom_price?.unit_amount * 100) || 0,
24+
product: custom_price?.product,
25+
});
26+
27+
console.log({ new_price });
28+
29+
if (new_price?.id) {
30+
line_items.push({
31+
price: new_price.id,
32+
quantity: 1,
33+
});
34+
}
35+
}
36+
37+
if (set_price?.price) {
38+
line_items.push({
39+
price: set_price.price,
40+
quantity: set_price.quantity || 1,
41+
});
42+
}
43+
44+
if (line_items?.length < 1) {
45+
return null;
46+
}
47+
48+
console.log({ line_items });
49+
50+
const paymentLink = await stripeTest.paymentLinks.create({
51+
// @TODO: maximum 20 items
52+
line_items: line_items.splice(0, 20) || [],
53+
after_completion: {
54+
type: 'redirect',
55+
redirect: {
56+
url: 'https://markket.place/receipt?session_id={CHECKOUT_SESSION_ID}',
57+
},
58+
},
59+
// on_behalf_of: 'connected_acct_id',
60+
// transfer_data: {
61+
// destination: 'connected_acct_id',
62+
// },
63+
});
64+
65+
return paymentLink;
66+
};

0 commit comments

Comments
 (0)
Please sign in to comment.