使用NODE.js进行身份验证并捕获费用

时间:2018-12-01 16:29:02

标签: node.js stripe-payments

我在https://stripe.com/docs/charges#auth-capture中描述的预授权方面遇到了麻烦。

身份验证成功(capture参数为false),我可以将carghe的ID存储在我的数据库中,并且可以从我的Stripe Dashboard中看到未支付的费用

当我尝试收取费用时出现问题,因为它失败并显示Error: No such charge: <CHARGE_ID>。 这是代码:

constructor(){
  this.stripe = require('stripe')('<sk_test_mysecretkey>');
}
async captureCharge(chargeId) {
  try {
    /**
     * https://stripe.com/docs/api/charges/capture
     */
    return this.stripe.charges.capture(chargeId)
      .then((charge) => {
        return {
          error: false,
          success: true,
          message: 'ChargeSuccesful',
          code: 200,
          charge: charge,
        };
      },
        (err) => {
          console.log("CAPTURE CHARGE ERROR: ", err);
          return {
            success: false,
            error: err.type,
            code: err.statusCode,
          };
        }
      );
  } catch (e) {
    console.log('ERROR CAPTURE', e);
  }
}

即使我尝试使用https://api.stripe.com/v1/charges/<CHARGE_ID>/captureauth: Bearer <sk_test_mysecretkey>进行POST,我也会遇到相同的错误:

{
    "error": {
        "code": "resource_missing",
        "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
        "message": "No such charge: <CHARGE_ID>",
        "param": "charge",
        "type": "invalid_request_error"
    }
}

Stripe上仍存在未捕获的费用。

谢谢。

更新

我忘了说这笔费用不是简单的费用,而是一笔shared customer的分期付款。 Stripe支持three approaches处理拆分付款,我选择了direct charges

async authorizationCharge(amount, merchantStripeId, billId, customerId) {
  try {    
    var fee = 0.25;
    /** Pre-Authorization Charge using a shared customer
    * https://stripe.com/docs/charges#auth-capture
    * https://stripe.com/docs/connect/shared-customers
    */
    return this.stripe.tokens
      .create(
        { customer: customerId },
        { stripe_account: merchantStripeId }
      )
      .then((oneTimeToken) => {
        return this.stripe.charges
          .create(
            {
              amount: Math.round(amount),
              currency: 'eur',
              application_fee: fee,
              description: 'Bill ID: ' + billId,
              source: oneTimeToken.id,
              capture: false,
            },
            { stripe_account: merchantStripeId }
          )
          .then((charge) => {
            console.log('CHARGE: ', charge);
            return {
              error: false,
              success: true,
              code: 200,
              charge: charge,
              fee: fee,
            };
          },
            (err) => {
              // ERROR INFO:
              // https://stripe.com/docs/api#error_handling
              console.log('ERROR', err);
              return {
                success: false,
                error: err.type,
                code: err.statusCode,
              };
            }
          );
      },
        (err) => {
          // ERROR INFO:
          // https://stripe.com/docs/api#error_handling
          console.log('ERROR', err);
          return { success: false, error: err.type, code: err.statusCode };
        }
      );
  } catch (e) {
    console.log(e);
  }
}

1 个答案:

答案 0 :(得分:0)

最后,我理解了原因,但没有指出关联的帐户(请参阅更新的问题)。

return this.stripe.charges.capture(chargeId, { stripe_account: merchantStripeId }) <---- CONNECTED ACCOUNT
.then((charge) => {
  ...
}