Credits Cards

Payment 入账

PaymentService 拥有支付订单,确认 paid 后向 Primary Card 发放额度。

PaymentService 管理 pending、paid、canceled 等订单状态。CreditsService.topup() 只接收已经确认的额度。

const credits = new CreditsService();
const payment = new PaymentService({
  providers: [stripePaymentProvider()],
  resolve_topup: ({ topup_amount_minor }) => ({
    credits: topup_amount_minor * 10_000,
  }),
  on_paid: async (record) => {
    await credits.topup({
      card: { kind: "primary", user_id: record.user_id },
      credits: record.credits,
      source: "payment",
      ref: record.payment_id,
      idempotency_key: `payment:${record.payment_id}`,
      note: record.note,
    });
  },
});

用户直接创建支付订单,无需先在 Credits 中创建 pending Topup:

await city.payment.method("stripe").invoke({
  topup_amount_minor: 500,
  idempotency_key: "order_123",
  note: "购买 Credits",
});

用户只能提交真实支付金额。resolve_topup 在 Federation 服务端计算并冻结本单 Credits,客户端不能指定兑换比例。