fasenderos
fasenderos

Reputation: 424

Stripe auth and capture charge with NODE.js

I'm having trouble with the pre-authorization described here https://stripe.com/docs/charges#auth-capture.

The auth goes successful (capture params to false), I can store the ID of the carghe on my databse and I can see the uncaptured charge from my Stripe Dashboard.

Problem comes when I try to capture the charge, because it fails with Error: No such charge: <CHARGE_ID>. Here is the code:

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);
  }
}

Even if I try with a POST to https://api.stripe.com/v1/charges/<CHARGE_ID>/capture with auth: Bearer <sk_test_mysecretkey> i get the same error:

{
    "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"
    }
}

The uncaptured charge still exist on Stripe.

Thanks in advance.

UPDATE

I forgot to say that the charge isn't a simple charge, but it is a split payment with a shared customer. Stripe supports three approaches for processing split payment, I have choosed the 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);
  }
}

Upvotes: 1

Views: 793

Answers (1)

fasenderos
fasenderos

Reputation: 424

Finally I understood the reason, I did not indicate the connected account (see the updated question).

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

Upvotes: 0

Related Questions