From 17ada60023ec5d4a969baeae78c4ce4a57e26aa6 Mon Sep 17 00:00:00 2001 From: Daveed Date: Sat, 4 Jan 2025 17:19:38 -0500 Subject: [PATCH 1/2] Retrieve session id info --- src/api/markket/controllers/markket.ts | 34 ++++++++++++++++++++------ src/api/markket/services/stripe.ts | 32 ++++++++++++++++++++---- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/api/markket/controllers/markket.ts b/src/api/markket/controllers/markket.ts index 6700ec6..771bc13 100644 --- a/src/api/markket/controllers/markket.ts +++ b/src/api/markket/controllers/markket.ts @@ -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'; @@ -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; @@ -36,21 +38,37 @@ 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: "", } }); @@ -58,7 +76,7 @@ module.exports = createCoreController(modelId, ({ strapi }) => ({ message: `action ${body?.action} completed`, data: { info: message, - link, + link: link || '', }, }); }, diff --git a/src/api/markket/services/stripe.ts b/src/api/markket/services/stripe.ts index 3769456..c98c05c 100644 --- a/src/api/markket/services/stripe.ts +++ b/src/api/markket/services/stripe.ts @@ -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); @@ -45,10 +42,9 @@ 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', @@ -56,6 +52,11 @@ export const createPaymentLinkWithPriceIds = async (prices: { id: string, quanti 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', @@ -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; +}; From 7525b9c0f7b17603254a5b7b0a75946421c1245c Mon Sep 17 00:00:00 2001 From: Daveed Date: Sat, 4 Jan 2025 17:33:23 -0500 Subject: [PATCH 2/2] Shippping options optional --- src/api/markket/controllers/markket.ts | 2 +- src/api/markket/services/stripe.ts | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/api/markket/controllers/markket.ts b/src/api/markket/controllers/markket.ts index 771bc13..8e69234 100644 --- a/src/api/markket/controllers/markket.ts +++ b/src/api/markket/controllers/markket.ts @@ -34,7 +34,7 @@ module.exports = createCoreController(modelId, ({ strapi }) => ({ let link = null; if (body?.action === 'stripe.link') { - link = await createPaymentLinkWithPriceIds(body?.prices || []); + link = await createPaymentLinkWithPriceIds(body?.prices || [], true); message = 'stripe link created'; } diff --git a/src/api/markket/services/stripe.ts b/src/api/markket/services/stripe.ts index c98c05c..624fad2 100644 --- a/src/api/markket/services/stripe.ts +++ b/src/api/markket/services/stripe.ts @@ -9,7 +9,7 @@ const stripeTest = require('stripe')(STRIPE_SECRET_TEST_KEY); * @param prices * @returns */ -export const createPaymentLinkWithPriceIds = async (prices: { id: string, quantity: number }[]) => { +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); @@ -44,7 +44,7 @@ export const createPaymentLinkWithPriceIds = async (prices: { id: string, quanti console.log('create.stripe.payment.link', { line_items }); - const paymentLink = await stripeTest.paymentLinks.create({ + const stripe_options = { line_items: line_items.splice(0, 20) || [], after_completion: { type: 'redirect', @@ -52,16 +52,21 @@ export const createPaymentLinkWithPriceIds = async (prices: { id: string, quanti 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', // }, - }); + }; + + if (include_shipping) { + // @ts-expect-error + stripe_options.shipping_address_collection = { + allowed_countries: ['US'], + }; + } + + const paymentLink = await stripeTest.paymentLinks.create(stripe_options); return paymentLink; };