opennodedev / opennode-node

Node.js library for the OpenNode API. https://opennode.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding InitiateExchange Endpoint to client lib

brandonrobinson5060 opened this issue Β· comments

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch opennode@1.3.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/opennode/src/lib.js b/node_modules/opennode/src/lib.js
index 6a2bcba..b8fb86d 100644
--- a/node_modules/opennode/src/lib.js
+++ b/node_modules/opennode/src/lib.js
@@ -24,6 +24,10 @@ async function initiateWithdrawal(withdrawal) {
   return await instance.initiateWithdrawal(withdrawal);
 }
 
+async function initiateExchange(exchange) {
+  return await instance.initiateExchange(exchange);
+}
+
 async function withdrawalInfo(id) {
   return await instance.withdrawalInfo(id);
 }
@@ -74,6 +78,7 @@ module.exports = {
   chargeInfo: chargeInfo,
   listCharges: listCharges,
   initiateWithdrawal: initiateWithdrawal,
+  initiateExchange: initiateExchange,
   initiateWithdrawalAsync: initiateWithdrawalAsync,
   withdrawalInfo: withdrawalInfo,
   listWithdrawals: listWithdrawals,
diff --git a/node_modules/opennode/submodules/client.js b/node_modules/opennode/submodules/client.js
index bb2fab6..b7ea879 100644
--- a/node_modules/opennode/submodules/client.js
+++ b/node_modules/opennode/submodules/client.js
@@ -126,6 +126,21 @@ class OpenNodeClient {
     }
   }
 
+  async initiateExchange(exchange) {
+    try {
+      let new_instance = axios.create();
+      new_instance.defaults.baseURL = (this.env === 'live') ? 'https://api.opennode.com/v2' : 'https://dev-api.opennode.com/v2';
+      new_instance.defaults.timeout = 15000;
+      new_instance.defaults.headers = { 'Authorization': this.api_key, 'user_agent': version };
+
+      const response = await new_instance.post('/exchanges', exchange);
+      return response.data;
+    }
+    catch (error) {
+      throw Exception(error.response.status, error.response.statusText, error.response.data.message);
+    }
+  }
+
   async verifySignature(charge) {
     const hash = crypto.createHmac('sha256', this.api_key).update(charge.id).digest('hex');
     return hash === charge.hashed_order;

This issue body was partially generated by patch-package.

Stopped being lazy and created a PR

#22