coinbase-commerce-node icon indicating copy to clipboard operation
coinbase-commerce-node copied to clipboard

coinbase.resources.Charge.cancel

Open joekim opened this issue 3 years ago • 1 comments

The coinbase commerce API has an affordance to cancel charges this node library should support this API call.

https://commerce.coinbase.com/docs/api/#cancel-a-charge

This issue was previously brought up with: https://github.com/coinbase/coinbase-commerce-node/issues/38

Why would someone need to cancel a charge if it's just going to expire? One scenario is if the user is given less than the expiration time to complete the charge and the system wants to explicitly close the charge to prevent payment if it hasn't been done yet.

joekim avatar Jul 06 '21 16:07 joekim

wrote this small function that does the trick, let me know

import fetch from "node-fetch";

const cancelCharge = async (chargeCode: string, apiKey: string): Promise<void> => {
  try {
    await fetch(
      `https://api.commerce.coinbase.com/charges/${chargeCode}/cancel`,
      {
        method: "POST",
        headers: {
          "X-CC-Api-Key": apiKey,
          "X-CC-Version": "2018-03-22",
        },
      }
    );
  } catch (error) {
    console.log("cancelCharge error:", error);
  }
};

hippolyte42 avatar Jun 30 '22 14:06 hippolyte42