coinbase / coinbase-commerce-node

Coinbase Commerce Node
MIT License
148 stars 54 forks source link

Added cancel charge method #65

Open SlayerAnsh opened 2 years ago

SlayerAnsh commented 2 years ago

This pull request targets on adding Cancel Charge feature.

Important use case:

  1. Cancel a charge before the default timeout
  2. In some manual integration, to disable user from completing payment, canceling charge can be used.

Changes:

  1. Added cancel crud method
  2. Added cancel test

Note: Verification with actual api call is left. If someone can verify it, it would be really helpful.

SlayerAnsh commented 2 years ago

Targets issues: #64 #51 #38 #12

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);
  }
};
SlayerAnsh commented 2 years ago

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

const cancelCoinbaseCharge = 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("cancelCoinbaseCharge error:", error);
  }
};

Yes, this works. I am using same using node-fetch in express backend. You can remove X-CC-Api-Key header as it is not required. (It should be required though)

hippolyte42 commented 2 years ago

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

const cancelCoinbaseCharge = 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("cancelCoinbaseCharge error:", error);
  }
};

Yes, this works. I am using same using node-fetch in express backend. You can remove X-CC-Api-Key header as it is not required. (It should be required though)

real weird the api key isn't required, I shall keep it tho, just in case since it should be