stripe-paymentsstripe-tax

How can I tell Stripe to calculate the tax for me during credit note creation via their API?


When using Stripe's automatic tax calculation (automatic_tax[enabled]=true) Stripe takes care of calculating the amount of tax that is applied to an invoice. The problem I'm running into is that when creating a "credit note" for a partial refund via Stripe's API, the refund_amount must equal the credit note amount (credit note amount = refund amount + tax), but I don't know what the calculated tax refund amount should be since Stripe handles that calculation on their end.

Consider the following code that will attempt to create a credit note on an invoice in the amount of $10.00:

$stripe->creditNotes->create([
  'invoice' => 'in_xxxxxxxxxxxxx',
  'refund_amount => 1000,
]);

The problem is that Stripe will calculate on their end that the refund_amount should be $10.70 (assuming 7% tax), and will return the following error:

The sum of credit amount, refund amount and out of band amount ($10.00) must equal the credit note amount ($10.70).

So what I'm thinking I need is an additional parameter that tells Stripe that I'm expecting them to determine what the additional amount for tax should be; something like this:

$stripe->creditNotes->create([
  'invoice' => 'in_xxxxxxxxxxxxx',
  'refund_amount => 1000,
  'automatic_tax' => [
    'enabled' => true,
  ],
]);

But that parameter doesn't exist on the API. Does anyone have any suggestions on how to solve this problem?


Solution

  • The solution is to first create a "preview" credit note, which provides the calculated tax amounts. You can then add that tax onto the refund amount when creating the actual credit note:

    // The amount to refund.
    $amount = 1000; // ($10.00)
    
    // First create a preview credit note.
    $preview = $stripe->creditNotes->preview([
        'invoice' => 'in_xxxxxxxxxxxxx',
        'refund_amount' => $amount,
        'lines' => [
            [
                'type' => 'invoice_line_item',
                'invoice_line_item' => 'li_xxxxxxxxxxxxx',
                'amount' => $amount,
            ],
        ],
    ]);
    
    // Collect the tax from the preview.
    $tax = 0;
    foreach ($preview->tax_amounts as $taxAmount) {
        $tax += $taxAmount->amount;
    }
    
    // Add the tax onto the refund amount.
    $totalAmount = $amount + $tax;
    
    // Issue the actual credit note.
    $creditNote = $stripe->creditNotes->create([
        'invoice' => 'in_xxxxxxxxxxxxx',
        'refund_amount' => $totalAmount,
        'lines' => [
            [
                'type' => 'invoice_line_item',
                'invoice_line_item' => 'li_xxxxxxxxxxxxx',
                'amount' => $amount,
            ],
        ],
    ]);