支付网关集成 payment-gateway-integration

支付网关集成技能用于整合多种支付处理系统,如Stripe、PayPal、Square,实现订阅支付、退款处理、webhooks回调等功能,确保PCI合规,适用于电商平台、订阅服务等场景。关键词:支付集成、Stripe、PayPal、Square、webhook、PCI合规、订阅支付、退款处理。

支付系统 0 次安装 0 次浏览 更新于 3/7/2026

name: payment-gateway-integration description: 集成支付处理,支持Stripe、PayPal或Square,包括订阅、webhooks和PCI合规。适用于实现结账流程、定期账单、或处理退款和争议。

支付网关集成

集成安全的支付处理,具有适当的错误处理和合规性。

Stripe集成 (Node.js)

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

class PaymentService {
  async createPaymentIntent(amount, currency, customerId) {
    return stripe.paymentIntents.create({
      amount: Math.round(amount * 100), // Convert to cents
      currency,
      customer: customerId,
      automatic_payment_methods: { enabled: true }
    });
  }

  async createSubscription(customerId, priceId) {
    return stripe.subscriptions.create({
      customer: customerId,
      items: [{ price: priceId }],
      payment_behavior: 'default_incomplete',
      expand: ['latest_invoice.payment_intent']
    });
  }

  async refund(paymentIntentId, amount = null) {
    const params = { payment_intent: paymentIntentId };
    if (amount) params.amount = Math.round(amount * 100);
    return stripe.refunds.create(params);
  }
}

Webhook处理

app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
  const sig = req.headers['stripe-signature'];

  let event;
  try {
    event = stripe.webhooks.constructEvent(req.body, sig, process.env.STRIPE_WEBHOOK_SECRET);
  } catch (err) {
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  switch (event.type) {
    case 'payment_intent.succeeded':
      await handlePaymentSuccess(event.data.object);
      break;
    case 'invoice.payment_failed':
      await handlePaymentFailed(event.data.object);
      break;
  }

  res.json({ received: true });
});

PayPal集成

参见 references/paypal-integration.md 获取完整的PayPal实现,包括:

  • 订单创建和捕获
  • 退款处理
  • Webhook处理
  • 前端SDK集成
  • 成功/取消回调

安全清单

  • [ ] 仅使用官方SDK
  • [ ] 验证webhook签名
  • [ ] 永不记录完整卡号
  • [ ] 存储最小化支付数据
  • [ ] 首先在沙盒中测试
  • [ ] 所有支付路由使用HTTPS
  • [ ] 处理所有错误情况
  • [ ] 使用幂等性键
  • [ ] 实现重试逻辑

最佳实践

做:

  • 使用官方SDK库
  • 验证所有webhook签名
  • 记录交易ID(而非卡数据)
  • 在沙盒环境中测试
  • 处理所有支付状态
  • 实现适当的错误消息

不做:

  • 直接处理原始卡数据
  • 存储敏感支付信息
  • 硬编码API密钥
  • 跳过webhook签名验证
  • 忽略失败的支付事件
  • 在生产中使用测试密钥