Ship the page-focused follow reply with the news and shop updates.
Replace the subscribe welcome with page-generation examples, add the trial news003 WeChat share card, and let OneShop checkout return to an order or recharge page. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,6 +3,22 @@ import { createHmac, timingSafeEqual } from 'node:crypto';
|
||||
const lifetimeMs = 5 * 60_000;
|
||||
const signature = (body, secret) => createHmac('sha256', secret)
|
||||
.update('one-shop-checkout-v1:' + body).digest('base64url');
|
||||
const returnPathPattern = /^(\/[a-z0-9-]{1,80})?\/(my\/orders\/[1-9]\d*\/pay|recharge)$/;
|
||||
|
||||
export function resolveReturnUrl(shopOrigin, { orderId, returnPath } = {}) {
|
||||
let path = returnPath;
|
||||
if (path == null || path === '') {
|
||||
if (!Number.isSafeInteger(orderId) || orderId <= 0) return null;
|
||||
path = `/my/orders/${orderId}/pay`;
|
||||
}
|
||||
const pattern = Number.isSafeInteger(orderId) && orderId > 0
|
||||
? new RegExp(`^(?:/[a-z0-9-]{1,80})?/my/orders/${orderId}/pay$`)
|
||||
: /^(?:\/[a-z0-9-]{1,80})?\/recharge$/;
|
||||
if (typeof path !== 'string' || path.length > 120 || !pattern.test(path)) return null;
|
||||
const target = new URL(path, shopOrigin);
|
||||
if (target.origin !== shopOrigin || target.pathname !== path || target.search || target.hash) return null;
|
||||
return target.href;
|
||||
}
|
||||
|
||||
export function createCheckoutToken(payload, secret, now = Date.now()) {
|
||||
const body = Buffer.from(JSON.stringify({ ...payload, expires: now + lifetimeMs })).toString('base64url');
|
||||
@@ -19,7 +35,7 @@ export function readCheckoutToken(token, secret, shopOrigin, now = Date.now()) {
|
||||
const payload = JSON.parse(Buffer.from(body, 'base64url').toString());
|
||||
const target = new URL(payload.return_url);
|
||||
if (!Number.isSafeInteger(payload.expires) || payload.expires <= now || payload.expires > now + lifetimeMs ||
|
||||
target.origin !== shopOrigin || !/^\/my\/orders\/[1-9]\d*\/pay$/.test(target.pathname) || target.search || target.hash ||
|
||||
target.origin !== shopOrigin || !returnPathPattern.test(target.pathname) || target.search || target.hash ||
|
||||
!Number.isSafeInteger(payload.amount_cents) || payload.amount_cents <= 0 || !payload.jsapi_params?.package) {
|
||||
throw new Error('Invalid checkout');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { checkoutHtml, createCheckoutToken, readCheckoutToken } from './one-shop-checkout.mjs';
|
||||
import { checkoutHtml, createCheckoutToken, readCheckoutToken, resolveReturnUrl } from './one-shop-checkout.mjs';
|
||||
import {
|
||||
createWechatPayClient,
|
||||
loadWechatPayConfig,
|
||||
@@ -123,15 +123,19 @@ export function attachPortalOneShopRoutes({
|
||||
out_trade_no: outTradeNo, amount_cents: amountCents,
|
||||
memind_user_id: userId, order_id: orderId,
|
||||
openid: directOpenid, openid_app_id: openidAppId,
|
||||
return_path: returnPath,
|
||||
} = req.body || {};
|
||||
if (typeof outTradeNo !== 'string' || !orderPattern.test(outTradeNo) ||
|
||||
!Number.isSafeInteger(amountCents) || amountCents <= 0 || amountCents > 100_000_000 ||
|
||||
(userId != null && (typeof userId !== 'string' || !userId || userId.length > 128)) ||
|
||||
(directOpenid != null && (typeof directOpenid !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(directOpenid) ||
|
||||
typeof openidAppId !== 'string' || openidAppId.length > 64)) ||
|
||||
(orderId != null && (!Number.isSafeInteger(orderId) || orderId <= 0 || !outTradeNo.startsWith('OC')))) {
|
||||
(orderId != null && (!Number.isSafeInteger(orderId) || orderId <= 0 || !outTradeNo.startsWith('OC'))) ||
|
||||
(returnPath != null && (typeof returnPath !== 'string' || returnPath.length > 120))) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
const returnUrl = resolveReturnUrl(shopOrigin, { orderId, returnPath: returnPath || null });
|
||||
if (returnPath && !returnUrl) return res.sendStatus(400);
|
||||
try {
|
||||
const pay = getPayClient();
|
||||
if (!pay?.enabled) return res.sendStatus(503);
|
||||
@@ -140,22 +144,26 @@ export function attachPortalOneShopRoutes({
|
||||
if (userId) {
|
||||
await userAuthReady;
|
||||
openid = await getUserAuth()?.getWechatOpenidForUser(userId, pay.appId);
|
||||
if (!openid) return res.sendStatus(409);
|
||||
} else if (directOpenid) {
|
||||
}
|
||||
if (!openid && directOpenid) {
|
||||
if (openidAppId !== pay.appId) return res.sendStatus(409);
|
||||
openid = directOpenid;
|
||||
} else if (userId && !openid) {
|
||||
return res.sendStatus(409);
|
||||
}
|
||||
if (openid) {
|
||||
const { jsapiParams } = await pay.createJsapiOrder({ ...order, openid });
|
||||
if (orderId) {
|
||||
if (returnUrl) {
|
||||
const token = createCheckoutToken({
|
||||
jsapi_params: jsapiParams, amount_cents: amountCents,
|
||||
return_url: new URL(`/my/orders/${orderId}/pay`, shopOrigin).href,
|
||||
jsapi_params: jsapiParams, amount_cents: amountCents, return_url: returnUrl,
|
||||
}, sharedSecret);
|
||||
// Use the configured public payment origin, never an untrusted Host header.
|
||||
const publicOrigin = new URL(paymentPublicUrl).origin;
|
||||
if (!['https:', 'http:'].includes(new URL(paymentPublicUrl).protocol)) throw new Error('Invalid payment origin');
|
||||
return res.json({ checkout_url: publicOrigin + '/one-shop-pay#checkout=' + token });
|
||||
return res.json({
|
||||
checkout_url: publicOrigin + '/one-shop-pay#checkout=' + token,
|
||||
jsapi_params: jsapiParams,
|
||||
});
|
||||
}
|
||||
return res.json({ jsapi_params: jsapiParams });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user