You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中Spreedly与Stripe对接及支付token问题求助

解决方案:Spreedly Token 对接 Stripe(Node.js)

核心逻辑:Spreedly 生成的支付 token 仅在其自身体系内有效,无法直接调用 Stripe API。必须通过 Spreedly 的网关转发接口,将支付请求委托给已配置好的 Stripe 网关完成交易。

步骤 1:在 Spreedly 后台配置 Stripe 网关

  1. 登录 Spreedly 控制台,添加 Stripe 作为支付网关
  2. 填入你的 Stripe API 密钥(测试/生产环境对应),完成配置后获取该网关的 gateway_token(一串类似 "GTxxxxxx" 的字符串)

步骤 2:Node.js 中发起支付请求

可以直接调用 Spreedly 的 REST API,或使用官方 Node.js SDK。以下是两种实现方式:

方式一:使用原生 HTTP 请求(Axios 示例)

const axios = require('axios');

async function processPayment() {
  const spreedlyAuth = Buffer.from(`${SPREEDLY_ENV_KEY}:${SPREEDLY_ACCESS_SECRET}`).toString('base64');
  const gatewayToken = "你的Stripe网关gateway_token";
  const paymentMethodToken = "Spreedly生成的支付token";

  try {
    const response = await axios.post(
      `https://core.spreedly.com/v1/gateways/${gatewayToken}/purchase.json`,
      {
        transaction: {
          amount: "10.00", // 支付金额,字符串格式
          currency_code: "USD", // 币种
          payment_method_token: paymentMethodToken,
          retain_on_success: true // 可选:如果需要保留支付方式供后续使用
        }
      },
      {
        headers: {
          'Authorization': `Basic ${spreedlyAuth}`,
          'Content-Type': 'application/json'
        }
      }
    );

    // 处理响应结果
    if (response.data.transaction.succeeded) {
      console.log("支付成功,Stripe交易ID:", response.data.transaction.gateway_specific_fields.stripe['charge_id']);
    } else {
      console.error("支付失败:", response.data.transaction.message);
    }
  } catch (error) {
    console.error("请求错误:", error.response?.data || error.message);
  }
}

processPayment();

方式二:使用 Spreedly 官方 Node.js SDK

先安装依赖:

npm install spreedly

然后编写代码:

const Spreedly = require('spreedly');

const client = new Spreedly.Client(SPREEDLY_ENV_KEY, SPREEDLY_ACCESS_SECRET);

async function processPayment() {
  const gatewayToken = "你的Stripe网关gateway_token";
  const paymentMethodToken = "Spreedly生成的支付token";

  try {
    const result = await client.purchase(gatewayToken, {
      amount: 10.00,
      currencyCode: 'USD',
      paymentMethodToken: paymentMethodToken
    });

    if (result.succeeded) {
      console.log("支付成功,Stripe交易ID:", result.gatewaySpecificFields.stripe.charge_id);
    } else {
      console.error("支付失败:", result.message);
    }
  } catch (error) {
    console.error("请求错误:", error.message);
  }
}

processPayment();

关键注意事项

  • 确保 Spreedly 环境密钥(SPREEDLY_ENV_KEY)和访问密钥(SPREEDLY_ACCESS_SECRET)正确,对应测试/生产环境
  • 支付金额需符合 Stripe 的币种精度要求(比如美元是两位小数)
  • 若支付失败,可从响应的 transaction.message 或 errors 字段排查原因(比如网关配置错误、银行卡信息无效等)

内容的提问来源于stack exchange,提问作者Jenil Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:32:16