Skip to content

Commit

Permalink
Merge pull request #54 from a11rew/use-axios
Browse files Browse the repository at this point in the history
  • Loading branch information
a11rew authored Oct 21, 2023
2 parents 5a0938a + f489787 commit 447d065
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
7 changes: 7 additions & 0 deletions .changeset/friendly-candles-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"medusa-payment-paystack": patch
---

Use Axios for Paystack API requests to resolve header issues on some platforms.

Credits: [JaeKralj](https://github.com/JaeKralj)
3 changes: 2 additions & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@medusajs/medusa": "^1.16.1"
},
"dependencies": {
"@medusajs/utils": "^1.9.4"
"@medusajs/utils": "^1.9.4",
"axios": "^1.5.1"
},
"devDependencies": {
"@medusajs/medusa": "^1.16.1",
Expand Down
61 changes: 21 additions & 40 deletions packages/plugin/src/lib/paystack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import https from "https";
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";

import { SupportedCurrency } from "../utils/currencyCode";

Expand Down Expand Up @@ -36,54 +36,35 @@ export interface PaystackTransactionAuthorisation {
export default class Paystack {
apiKey: string;

protected readonly axiosInstance: AxiosInstance;

constructor(apiKey: string) {
this.apiKey = apiKey;
}

protected async requestPaystackAPI<T>(request: Request): Promise<T> {
const path =
request.path.replace(/\/$/, "") +
"/?" +
new URLSearchParams(request.query).toString();

const options = {
method: request.method,
path,
this.axiosInstance = axios.create({
baseURL: PAYSTACK_API_PATH,
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
};

return new Promise((resolve, reject) => {
const req = https.request(PAYSTACK_API_PATH, options, res => {
let data: Uint8Array[] = [];

res.on("data", chunk => {
data.push(chunk);
});

res.on("end", () => {
try {
resolve(JSON.parse(Buffer.concat(data).toString()) as T);
} catch (e) {
reject(e);
}
});
});

req.on("error", e => {
reject(e);
});

if (request.body && Object.values(request.body).length > 0) {
req.write(JSON.stringify(request.body));
}

req.end();
});
}

protected async requestPaystackAPI<T>(request: Request): Promise<T> {
const options = {
method: request.method,
url: request.path,
params: request.query,
data: request.body,
} satisfies AxiosRequestConfig;

try {
const res = await this.axiosInstance(options);
return res.data;
} catch (error) {
throw "Error from Paystack API: " + error.message;
}
}

transaction = {
verify: ({ reference }: { reference: string }) =>
this.requestPaystackAPI<
Expand Down
29 changes: 26 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 447d065

Please sign in to comment.