Skip to content

Commit

Permalink
feat: return access code and authorization url as first class citizens
Browse files Browse the repository at this point in the history
  • Loading branch information
a11rew committed Nov 10, 2024
1 parent ebac85b commit c6a7691
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/plugin/src/lib/__mocks__/paystack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const handlers = [
data: {
reference: `ref-${Math.random() * 1000}`,
authorization_url: "https://paystack.com/123",
access_code: "123",
},
});
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("Provider Service Initialization", () => {
});

describe("initiatePayment", () => {
it("returns a payment session with a transaction reference", async () => {
it("returns a payment session with Paystack transaction data", async () => {
const service = createPaystackProviderService();
const { data } = checkForPaymentProcessorError(
await service.initiatePayment({
Expand All @@ -89,8 +89,9 @@ describe("initiatePayment", () => {
}),
);

expect(data.paystackTxRef).toBeTruthy();
expect(data.paystackTxRef).toEqual(expect.any(String));
expect(data.paystackTxAccessCode).toEqual(expect.any(String));
expect(data.paystackTxAuthorizationUrl).toEqual(expect.any(String));
});

it("errors out if email is not provided", async () => {
Expand Down Expand Up @@ -157,7 +158,6 @@ describe("authorizePayment", () => {
const payment = checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-failed",
cartId: "cart-123",
}),
);
expect(payment.status).toEqual(PaymentSessionStatus.ERROR);
Expand All @@ -169,7 +169,6 @@ describe("authorizePayment", () => {
const payment = checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-passed",
cartId: "cart-123",
}),
);

Expand All @@ -181,7 +180,6 @@ describe("authorizePayment", () => {
const payment = checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-false",
cartId: "cart-123",
}),
);

Expand All @@ -194,7 +192,6 @@ describe("authorizePayment", () => {
const payment = checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-pending",
cartId: "cart-123",
}),
);

Expand Down Expand Up @@ -407,7 +404,6 @@ describe("Retriable error handling", () => {
const payment = checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-throw",
cartId: "cart-123",
}),
);

Expand All @@ -426,7 +422,6 @@ describe("Retriable error handling", () => {
checkForPaymentProcessorError(
await service.authorizePayment({
paystackTxRef: "123-throw",
cartId: "cart-123",
}),
);
}).rejects.toThrow();
Expand Down
11 changes: 7 additions & 4 deletions packages/plugin/src/services/paystack-payment-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface PaystackPaymentProcessorConfig
type PaystackPaymentProviderSessionResponse = PaymentProviderSessionResponse & {
data: {
paystackTxRef: string;
paystackTxAccessCode: string;
paystackTxAuthorizationUrl: string;
};
};

Expand Down Expand Up @@ -126,7 +128,8 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
return {
data: {
paystackTxRef: data.reference,
paystackTxAuthData: data,
paystackTxAccessCode: data.access_code,
paystackTxAuthorizationUrl: data.authorization_url,
},
};
} catch (error) {
Expand Down Expand Up @@ -160,9 +163,9 @@ class PaystackPaymentProcessor extends AbstractPaymentProvider {
* Called when a cart is completed
* We validate the payment and return a status
*/
async authorizePayment(
paymentSessionData: PaystackPaymentProviderSessionResponse["data"],
): Promise<
async authorizePayment(paymentSessionData: {
paystackTxRef: string;
}): Promise<
| PaymentProviderError
| {
status: PaymentSessionStatus;
Expand Down

0 comments on commit c6a7691

Please sign in to comment.