I'm making payment function for my static e-commerce Next.js app.
For payment I've several stages:
- Making cart page with shipping information form and button "Pay" which redirect to
/payment
page; - On
/payment
page I connect with my payment service and need to get cart info from Context API, but I can't use Context API ingetStaticProps
it's my problem. Payment page needs just get cart data and redirects on external service payment form.
Code for page /payment
is below:
import { useEffect, useContext } from "react"
import QiwiBillPaymentsAPI from "@qiwi/bill-payments-node-js-sdk"
import { CartContext } from "@/context/GlobalState"
export default function Payment ({ payUrl }) {
useEffect(() => window.location.assign(payUrl))
return (
<span>Redirect</span>
)
}
export async function getStaticProps() {
const qiwiApi = new QiwiBillPaymentsAPI(process.env.QIWI_SECRET_KEY)
const { state, dispatch } = useContext(CartContext)
const { cart } = state
const billId = qiwiApi.generateId()
const lifetime = qiwiApi.getLifetimeByDay(1);
const fields = {
amount: 1.00,
currency: "RUB",
expirationDateTime: lifetime,
}
const payment_data = await qiwiApi.createBill( billId, fields )
const payUrl = payment_data.payUrl
return { props: { payUrl }}
}
Please, help me with any ideas.