coinbase / coinbase-commerce-node

Coinbase Commerce Node
MIT License
144 stars 54 forks source link

coinbase.resources.Charge.cancel #51

Open joekim opened 3 years ago

joekim commented 3 years ago

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.

hippolyte42 commented 2 years ago

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