Skip to content

Prompt as Code | GPT Image 2 / 2.5 提示词与案例库,530+ 个案例、20+ 套工业级模板与可复用 Skills,新增 2.5 同提示词对比专区,附完整提示词与生成记录,持续更新。

Reconcile paid community orders with Alipay

苍何fd0f1b5d37be
diff --git a/api/_lib/community-alipay.js b/api/_lib/community-alipay.js
index f5154a8..684303a 100644
--- a/api/_lib/community-alipay.js
+++ b/api/_lib/community-alipay.js
@@ -10,6 +10,10 @@ export function isAlipayTradeNotFound(result) {
return code === 'ACQ.TRADE_NOT_EXIST' || code.includes('TRADE_NOT_EXIST');
}
+export function shouldQueryCommunityOrderAtAlipay(status) {
+ return status === 'PENDING' || status === 'PAID';
+}
+
export async function queryCommunityOrderAtAlipay(client, sdk, order) {
const result = await sdk.exec('alipay.trade.query', {
bizContent: { out_trade_no: order.id }
diff --git a/api/_lib/community.test.js b/api/_lib/community.test.js
index 6bfdb5c..d92845f 100644
--- a/api/_lib/community.test.js
+++ b/api/_lib/community.test.js
@@ -18,6 +18,10 @@ import {
validateCommunityQrUpload,
validateSameOrigin
} from './community.js';
+import {
+ queryCommunityOrderAtAlipay,
+ shouldQueryCommunityOrderAtAlipay
+} from './community-alipay.js';
const migrationPath = fileURLToPath(new URL(
'../../supabase/migrations/20260722090000_paid_community.sql',
@@ -147,6 +151,42 @@ test('payment kill switch defaults closed and changes only on explicit true', ()
assert.equal(isCommunityPaymentEnabled({ COMMUNITY_PAYMENT_ENABLED: 'true' }), true);
});
+test('active reconciliation queries Alipay for both pending and already-paid orders', async () => {
+ assert.equal(shouldQueryCommunityOrderAtAlipay('PENDING'), true);
+ assert.equal(shouldQueryCommunityOrderAtAlipay('PAID'), true);
+ for (const status of ['CLOSED', 'REFUNDED', 'REVOKED']) {
+ assert.equal(shouldQueryCommunityOrderAtAlipay(status), false);
+ }
+
+ const calls = [];
+ const sdk = {
+ async exec(method, payload, options) {
+ calls.push({ method, payload, options });
+ return {
+ code: '10000',
+ out_trade_no: order.id,
+ trade_no: 'trade-query-1',
+ trade_status: 'TRADE_SUCCESS',
+ total_amount: '9.90',
+ currency: 'CNY'
+ };
+ }
+ };
+ const client = {
+ async rpc(name, payload) {
+ calls.push({ name, payload });
+ return { data: [{ current_status: 'PAID', transitioned: false }], error: null };
+ }
+ };
+
+ const result = await queryCommunityOrderAtAlipay(client, sdk, { ...order, status: 'PAID' });
+ assert.equal(result.state, 'PAID');
+ assert.equal(calls[0].method, 'alipay.trade.query');
+ assert.equal(calls[0].payload.bizContent.out_trade_no, order.id);
+ assert.equal(calls[0].options.validateSign, true);
+ assert.equal(calls[1].name, 'mark_community_order_paid');
+});
+
test('community actions use bounded per-user rate limits', () => {
const key = `test-rate-${Date.now()}`;
assert.equal(takeCommunityRateLimit(key, { limit: 2, windowMs: 1000, now: 100 }).allowed, true);
diff --git a/api/community/alipay/query.js b/api/community/alipay/query.js
index 06d8c7f..7bab667 100644
--- a/api/community/alipay/query.js
+++ b/api/community/alipay/query.js
@@ -1,5 +1,8 @@
import { getAlipayClient, isAlipayConfigured } from '../../_lib/alipay.js';
-import { queryCommunityOrderAtAlipay } from '../../_lib/community-alipay.js';
+import {
+ queryCommunityOrderAtAlipay,
+ shouldQueryCommunityOrderAtAlipay
+} from '../../_lib/community-alipay.js';
import {
applyCommunityRateLimit,
communityRequestRateKey,
@@ -44,10 +47,10 @@ export default async function handler(req, res) {
.maybeSingle();
if (error) throw error;
if (!order) return res.status(404).json({ ok: false, error: 'COMMUNITY_ORDER_NOT_FOUND' });
- if (order.status !== 'PENDING') {
+ if (!shouldQueryCommunityOrderAtAlipay(order.status)) {
return res.status(200).json({
ok: true,
- paid: order.status === 'PAID',
+ paid: false,
order: serializeCommunityOrder(order)
});
}
@@ -57,6 +60,9 @@ export default async function handler(req, res) {
const { sdk } = getAlipayClient();
const reconciliation = await queryCommunityOrderAtAlipay(auth.client, sdk, order);
+ if (order.status === 'PAID' && reconciliation.state !== 'PAID') {
+ throw new Error('ALIPAY_COMMUNITY_PAID_ORDER_NOT_PAID');
+ }
const { data: refreshed, error: refreshError } = await auth.client
.from('community_orders')
.select('*')
@@ -67,8 +73,9 @@ export default async function handler(req, res) {
return res.status(200).json({
ok: true,
- paid: reconciliation.state === 'PAID',
- pending: ['PENDING', 'NOT_FOUND'].includes(reconciliation.state),
+ paid: refreshed.status === 'PAID',
+ pending: refreshed.status === 'PENDING'
+ && ['PENDING', 'NOT_FOUND'].includes(reconciliation.state),
order: serializeCommunityOrder(refreshed)
});
} catch (error) {