Skip to content

Commit

Permalink
update logic for initializePaymentGateway
Browse files Browse the repository at this point in the history
  • Loading branch information
pankajmindpath committed Apr 26, 2024
1 parent 59febaa commit 9a36672
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
11 changes: 9 additions & 2 deletions example/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
SALEOR_API_URL=
NEXT_PUBLIC_SALEOR_CHANNEL="default-channel"
SALEOR_API_URL=""
NEXT_PUBLIC_SALEOR_API_URL=""

AUTHORIZE_ENVIRONMENT=""
AUTHORIZE_SALEOR_CHANNEL_SLUG=""
NEXT_PUBLIC_AUTHORIZE_API_LOGIN_ID=""
NEXT_PUBLIC_AUTHORIZE_TRANSACTION_KEY=""
NEXT_PUBLIC_AUTHORIZE_PUBLIC_CLIENT_KEY=""
NEXT_PUBLIC_SALEOR_CHANNEL=""
34 changes: 23 additions & 11 deletions example/src/payment-methods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ export type AcceptHostedData = z.infer<typeof acceptHostedPaymentGatewaySchema>;

// currently, Payment Gateway Initialize doesnt return any config data
const dataSchema = z.object({
acceptHosted: z.unknown().optional(),
acceptHosted: z.object({ type: z.string().optional() }).optional(),
applePay: z.unknown().optional(),
paypal: z.unknown().optional(),
acceptJs: z.object({ enabled: z.boolean().optional() }),
acceptJs: z.object({ type: z.string().optional() }).optional(),
});

type PaymentMethods = z.infer<typeof dataSchema>;

const paymentGatewayInitializeSessionSchema = dataSchema;

const payloadDataSchemaAcceptHosted = z.object({
shouldCreateCustomerProfile: z.boolean(),
iframeUrl: z.string(),
Expand Down Expand Up @@ -89,14 +87,28 @@ export const PaymentMethods = () => {
if (!gateway) {
throw new Error("No payment gateway found");
}

const data = paymentGatewayInitializeSessionSchema?.parse(gateway.data);

if (!data) {
throw new Error("No data found");
const gatewayData = (gateway.data as { data?: { type: string } })?.data;

/**
* This needs to be selectable - if we want type apple pay, paypal, or acceptHosted
*/
switch (gatewayData?.type) {
case "acceptJs":
setPaymentMethods({ acceptJs: gatewayData });
break;
case "acceptHosted":
setPaymentMethods({ acceptHosted: gatewayData });
break;

case "applePay":
setPaymentMethods({ applePay: "" });
break;
case "paypal":
setPaymentMethods({ paypal: "" });
break;
default:
throw new Error("No data found");
}

setPaymentMethods(data);
}, [initializePaymentGateways, checkoutId]);

React.useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class AcceptHostedGateway implements PaymentGateway {
async initializePaymentGateway(
_payload: PaymentGatewayInitializeSessionEventFragment,
): Promise<AcceptHostedPaymentGatewayResponseData> {
return {};
return { type: "acceptHosted" };
}

async initializeTransaction(
Expand Down
2 changes: 1 addition & 1 deletion src/modules/authorize-net/gateways/accept-js-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class AcceptJsGateway implements PaymentGateway {
async initializePaymentGateway(
_payload: PaymentGatewayInitializeSessionEventFragment,
): Promise<AcceptJsPaymentGatewayResponseData> {
return {};
return { type: "acceptJs" };
}

async initializeTransaction(
Expand Down

0 comments on commit 9a36672

Please sign in to comment.