Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve session id info #10

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/api/markket/controllers/markket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { version } from '../../../../package.json';
const { createCoreController } = require('@strapi/strapi').factories;
const modelId = "api::markket.markket";

import { createPaymentLinkWithPriceIds } from '../services/stripe';
import { createPaymentLinkWithPriceIds, getSessionById } from '../services/stripe';

const NODE_ENV = process.env.NODE_ENV || 'development';
const STRIPE_PUBLIC_KEY = process.env.STRIPE_PUBLIC_KEY || 'n/a';
Expand All @@ -26,39 +26,57 @@ module.exports = createCoreController(modelId, ({ strapi }) => ({
});
},
async create(ctx: any) {
const body = JSON.parse(ctx.request.body);
let message = 'action completed';
console.info('markket.create');
const body = ctx.request?.body || {};
let message = 'action started';

console.log(`markket.create:${body.action}`, { body });

let link = null;
if (body?.action === 'stripe.link') {
link = await createPaymentLinkWithPriceIds(body?.prices || []);
link = await createPaymentLinkWithPriceIds(body?.prices || [], true);
message = 'stripe link created';
}

if (body?.action === 'stripe.receipt' && body?.session_id) {
link = await getSessionById(body?.session_id);
message = 'stripe session retrieved';
}

if (body?.action === 'stripe.webhook') {
// @TODO Store transaction record & send pertinent notifications
// await strapi.plugins['email'].services.email.send({
// to: 'valid email address',
// from: 'your verified email address', //e.g. single sender verification in SendGrid
// cc: 'valid email address',
// bcc: 'valid email address',
// replyTo: 'valid email address',
// subject: 'The Strapi Email plugin worked successfully',
// text: 'Hello world!',
// html: 'Hello world!',
// });
}

// Storing record of transaction
// Create a markket transaction record
await strapi.service(modelId).create({
locale: 'en',
data: {
Key: `markket.create.${body?.action || 'default'}`,
Content: {
link,
produt: body?.product,
link: link || '',
product: body?.product,
total: body?.total,
},
user_key_or_id: "", // @TODO: Review authorization, token or related user
// @TODO: Review authorization, token or related user
user_key_or_id: "",
}
});

ctx.send({
message: `action ${body?.action} completed`,
data: {
info: message,
link,
link: link || '',
},
});
},
Expand Down
43 changes: 35 additions & 8 deletions src/api/markket/services/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ const stripeTest = require('stripe')(STRIPE_SECRET_TEST_KEY);
* @param prices
* @returns
*/
export const createPaymentLinkWithPriceIds = async (prices: { id: string, quantity: number }[]) => {
console.log({ prices });

export const createPaymentLinkWithPriceIds = async (prices: { id: string, quantity: number }[], include_shipping?: boolean) => {
const line_items = [];

const custom_price: any = prices.find((price: any) => price.product);
const set_price: any = prices.find((price: any) => price.price);

Expand Down Expand Up @@ -45,22 +42,52 @@ export const createPaymentLinkWithPriceIds = async (prices: { id: string, quanti
return null;
}

console.log({ line_items });
console.log('create.stripe.payment.link', { line_items });

const paymentLink = await stripeTest.paymentLinks.create({
// @TODO: maximum 20 items
const stripe_options = {
line_items: line_items.splice(0, 20) || [],
after_completion: {
type: 'redirect',
redirect: {
url: 'https://markket.place/receipt?session_id={CHECKOUT_SESSION_ID}',
},
},
// @TODO: toggle automatic payouts & charging on behalf of connected account
// on_behalf_of: 'connected_acct_id',
// transfer_data: {
// destination: 'connected_acct_id',
// },
});
};

if (include_shipping) {
// @ts-expect-error
stripe_options.shipping_address_collection = {
allowed_countries: ['US'],
};
}

const paymentLink = await stripeTest.paymentLinks.create(stripe_options);

return paymentLink;
};


export const getSessionById = async (session_id: string) => {
if (!session_id) {
return null;
}

let session;
console.log('session.receipt', { session_id });
if (session_id.includes('cs_test')) {
session = await stripeTest.checkout.sessions.retrieve(
session_id,
);
} else {
// session = await stripe.checkout.sessions.retrieve(
// session_id,
// );
}

return session;
};
Loading