generated from saleor/saleor-app-payment-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
556 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from "react"; | ||
|
||
export const checkoutIdUtils = { | ||
set: (id: string) => localStorage.setItem("checkoutId", id), | ||
get: () => { | ||
const checkoutId = localStorage.getItem("checkoutId"); | ||
|
||
if (!checkoutId) { | ||
throw new Error("Checkout ID not found"); | ||
} | ||
|
||
return checkoutId; | ||
}, | ||
}; | ||
|
||
export const useGetCheckoutId = () => { | ||
const [checkoutId, setCheckoutId] = React.useState<string | null>(null); | ||
|
||
React.useEffect(() => { | ||
const checkoutId = checkoutIdUtils.get(); | ||
setCheckoutId(checkoutId); | ||
}, []); | ||
|
||
return checkoutId; | ||
}; |
66 changes: 66 additions & 0 deletions
66
example/src/pages/[transactionId]/paypal/continue/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useMutation } from "@apollo/client"; | ||
import gql from "graphql-tag"; | ||
import React from "react"; | ||
import { | ||
TransactionProcessMutation, | ||
TransactionProcessMutationVariables, | ||
TransactionProcessDocument, | ||
} from "../../../../../generated/graphql"; | ||
import { useRouter } from "next/router"; | ||
|
||
type Status = "idle" | "loading" | "success"; | ||
|
||
const PaypalContinuePage = () => { | ||
const [processTransaction] = useMutation<TransactionProcessMutation, TransactionProcessMutationVariables>( | ||
gql(TransactionProcessDocument.toString()), | ||
); | ||
const router = useRouter(); | ||
const isCalled = React.useRef(false); | ||
const [status, setStatus] = React.useState<Status>("idle"); | ||
|
||
const continueTransaction = React.useCallback( | ||
async ({ payerId, transactionId }: { payerId: string; transactionId: string }) => { | ||
setStatus("loading"); | ||
const response = await processTransaction({ | ||
variables: { | ||
transactionId, | ||
data: { | ||
type: "paypal", | ||
data: { | ||
payerId, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
isCalled.current = true; | ||
|
||
if (response.data?.transactionProcess?.transactionEvent?.type !== "AUTHORIZATION_SUCCESS") { | ||
throw new Error("Transaction failed"); | ||
} | ||
|
||
setStatus("success"); | ||
}, | ||
[processTransaction], | ||
); | ||
|
||
React.useEffect(() => { | ||
const payerId = router.query.PayerID?.toString(); | ||
const rawTransactionId = router.query.transactionId?.toString(); | ||
setStatus("idle"); | ||
|
||
if (payerId && rawTransactionId && !isCalled.current) { | ||
const transactionId = atob(rawTransactionId); | ||
continueTransaction({ payerId, transactionId }); | ||
} | ||
}, [continueTransaction, router.query.PayerID, router.query.transactionId]); | ||
|
||
return ( | ||
<div> | ||
{status === "loading" && <div>Processing transaction...</div>} | ||
{status === "success" && <div>You successfully paid with PayPal 🎺</div>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default PaypalContinuePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from "react"; | ||
|
||
const FailurePage = () => { | ||
return ( | ||
<div> | ||
<h1>Something went wrong</h1> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FailurePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { useMutation } from "@apollo/client"; | ||
import gql from "graphql-tag"; | ||
import Script from "next/script"; | ||
import React from "react"; | ||
import { z } from "zod"; | ||
import { | ||
TransactionInitializeDocument, | ||
TransactionInitializeMutation, | ||
TransactionInitializeMutationVariables, | ||
} from "../generated/graphql"; | ||
import { checkoutIdUtils } from "./lib/checkoutIdUtils"; | ||
import { authorizeNetAppId } from "./lib/common"; | ||
|
||
declare global { | ||
interface Window { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
paypal: any; | ||
} | ||
} | ||
|
||
const paypalTransactionResponseDataSchema = z.object({ | ||
secureAcceptanceUrl: z.string().min(1).optional(), | ||
error: z | ||
.object({ | ||
message: z.string().min(1), | ||
}) | ||
.optional(), | ||
}); | ||
|
||
/** | ||
* This form uses PayPal's legacy Express Checkout integration | ||
* https://developer.paypal.com/docs/archive/express-checkout/in-context/javascript-advanced-settings/ */ | ||
|
||
export const PayPalForm = () => { | ||
const [isLoading, setIsLoading] = React.useState(false); | ||
const [initializeTransaction] = useMutation< | ||
TransactionInitializeMutation, | ||
TransactionInitializeMutationVariables | ||
>(gql(TransactionInitializeDocument.toString())); | ||
|
||
function onLoad() { | ||
if (typeof window !== "undefined" && window.paypal) { | ||
window.paypal.checkout.setup(" ", { | ||
environment: "sandbox", | ||
container: "paypalButton", | ||
click: () => { | ||
getPayPalAcceptanceUrl(); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
const getPayPalAcceptanceUrl = async () => { | ||
setIsLoading(true); | ||
const checkoutId = checkoutIdUtils.get(); | ||
|
||
const initializeTransactionResponse = await initializeTransaction({ | ||
variables: { | ||
checkoutId, | ||
paymentGateway: authorizeNetAppId, | ||
data: { | ||
type: "paypal", | ||
data: {}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (initializeTransactionResponse.data?.transactionInitialize?.errors?.length) { | ||
throw new Error("Failed to initialize transaction"); | ||
} | ||
|
||
const data = initializeTransactionResponse.data?.transactionInitialize?.data; | ||
|
||
if (!data) { | ||
throw new Error("Data not found on transaction initialize response"); | ||
} | ||
|
||
const { secureAcceptanceUrl, error } = paypalTransactionResponseDataSchema.parse(data); | ||
|
||
if (error) { | ||
throw new Error(error.message); | ||
} | ||
|
||
if (!secureAcceptanceUrl) { | ||
throw new Error("Secure acceptance url not found"); | ||
} | ||
|
||
setIsLoading(false); | ||
window.open(secureAcceptanceUrl, "_self"); | ||
}; | ||
|
||
return ( | ||
<> | ||
{/* We need to load this before we execute any code */} | ||
<Script src="https://www.paypalobjects.com/api/checkout.js" onLoad={onLoad} /> | ||
|
||
{isLoading ? <p>Loading...</p> : <button id="paypalButton"></button>} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.