Skip to content

Commit

Permalink
Retrieve session id info
Browse files Browse the repository at this point in the history
  • Loading branch information
Daveed committed Jan 4, 2025
1 parent cea45aa commit 17ada60
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
34 changes: 26 additions & 8 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,8 +26,10 @@ 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;
Expand All @@ -36,29 +38,45 @@ module.exports = createCoreController(modelId, ({ strapi }) => ({
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
32 changes: 27 additions & 5 deletions src/api/markket/services/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ const stripeTest = require('stripe')(STRIPE_SECRET_TEST_KEY);
* @returns
*/
export const createPaymentLinkWithPriceIds = async (prices: { id: string, quantity: number }[]) => {
console.log({ prices });

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,17 +42,21 @@ 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
line_items: line_items.splice(0, 20) || [],
after_completion: {
type: 'redirect',
redirect: {
url: 'https://markket.place/receipt?session_id={CHECKOUT_SESSION_ID}',
},
},
// @TODO: toggle asking for address
shipping_address_collection: {
allowed_countries: ['US'],
},
// @TODO: toggle automatic payouts & charging on behalf of connected account
// on_behalf_of: 'connected_acct_id',
// transfer_data: {
// destination: 'connected_acct_id',
Expand All @@ -64,3 +65,24 @@ export const createPaymentLinkWithPriceIds = async (prices: { id: string, quanti

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;
};

0 comments on commit 17ada60

Please sign in to comment.