Skip to content

Commit

Permalink
Fix crash in paydunya error handler when response is HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
saalihou committed Jul 25, 2024
1 parent 17e2967 commit 2d72997
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/sdk/providers/paydunya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from "../payment-events";
import { createHash } from "crypto";
import { PaymentError, PaymentErrorType } from "../payment-error";
import { isBuffer, isString } from "lodash";
import { isBuffer, isObject, isString } from "lodash";

class PaydunyaPaymentProvider implements PaymentProvider {
private api: ApisauceInstance;
Expand Down Expand Up @@ -65,12 +65,13 @@ class PaydunyaPaymentProvider implements PaymentProvider {
response.status +
". Data: " +
JSON.stringify(response.data);
console.error(response);
throw new PaymentError(
response.data
response.data && isObject(response.data)
? "message" in response.data
? response.data.message
? String(response.data.message)
: "response_text" in response.data
? response.data.response_text
? String(response.data.response_text)
: defaultErrorMessage
: defaultErrorMessage
);
Expand Down Expand Up @@ -279,7 +280,9 @@ class PaydunyaPaymentProvider implements PaymentProvider {
return this.checkout(options);
}

async checkoutRedirect(options: RedirectCheckoutOptions): Promise<CheckoutResult> {
async checkoutRedirect(
options: RedirectCheckoutOptions
): Promise<CheckoutResult> {
throw new PaymentError(
"Paydunya redirect checkout not yet implemented",
PaymentErrorType.UNSUPPORTED_PAYMENT_METHOD
Expand Down Expand Up @@ -373,12 +376,17 @@ class PaydunyaPaymentProvider implements PaymentProvider {
};
}

async payoutMobileMoney(options: MobileMoneyPayoutOptions): Promise<PayoutResult> {
const paymentMethodToWithdrawMode: Record<MobileMoneyPayoutOptions['paymentMethod'], string> = {
async payoutMobileMoney(
options: MobileMoneyPayoutOptions
): Promise<PayoutResult> {
const paymentMethodToWithdrawMode: Record<
MobileMoneyPayoutOptions["paymentMethod"],
string
> = {
[PaymentMethod.WAVE]: "wave-senegal",
[PaymentMethod.ORANGE_MONEY]: "orange-money-senegal",
}
};

const parsedRecipientPhoneNumber = parsePhoneNumber(
options.recipient.phoneNumber,
"SN"
Expand Down Expand Up @@ -453,9 +461,13 @@ class PaydunyaPaymentProvider implements PaymentProvider {
};
}

async handleWebhook(rawBody: Buffer | string | Record<string, unknown>): Promise<void> {
async handleWebhook(
rawBody: Buffer | string | Record<string, unknown>
): Promise<void> {
if (isBuffer(rawBody) || isString(rawBody)) {
console.error("Paydunya webhook body must be a parsed object, not the raw body");
console.error(
"Paydunya webhook body must be a parsed object, not the raw body"
);
return;
}
const body = rawBody as PaydunyaPaymentWebhookBody;
Expand Down

0 comments on commit 2d72997

Please sign in to comment.