|
|
@@ -1,7 +1,7 @@
|
|
|
"use client";
|
|
|
|
|
|
import Image from "next/image";
|
|
|
-import { use,useState,useEffect,useCallback } from "react";
|
|
|
+import { use,useState,useCallback } from "react";
|
|
|
import { useForm, get, useWatch } from "react-hook-form";
|
|
|
import { useRouter } from 'next/navigation'
|
|
|
import { useCustomToast } from "@/utils/hooks/useToast";
|
|
|
@@ -14,10 +14,11 @@ import { usePlaceOrder } from "@/utils/hooks/usePlaceOrder";
|
|
|
import PaypalButton from "../PaymentButton/PaypalButton";
|
|
|
import { overlayLoading } from "@/components/theme/ui/kernel/loading/api";
|
|
|
import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
|
|
|
-import {loadAfterpayScript} from "@/lib/Afterpay/afterpay";
|
|
|
import PaypalApplepayButton from "../PaymentButton/PaypalApplepayButton";
|
|
|
+import KlarnaButton from "../PaymentButton/KlarnaButton";
|
|
|
import {getAddressFromOrderDetailAddressList} from "@/utils/orderDetailTools";
|
|
|
import { useConfig } from '@utils/hooks/useConfig';
|
|
|
+import {usePaymentSDKContext} from "@/providers/PaymentSDKProvider";
|
|
|
|
|
|
/**@todo 优化目标:不使用 react-hook-form */
|
|
|
export function PaymentMethodContinueTpPay({
|
|
|
@@ -38,6 +39,7 @@ export function PaymentMethodContinueTpPay({
|
|
|
|
|
|
const { paymentRepay, createPaymentCallback } = usePlaceOrder();
|
|
|
|
|
|
+ const {loadSdk} = usePaymentSDKContext();
|
|
|
const {store} = useConfig();
|
|
|
const currentCurrencyCode = store.currentCurrency;
|
|
|
|
|
|
@@ -275,6 +277,14 @@ export function PaymentMethodContinueTpPay({
|
|
|
}
|
|
|
}
|
|
|
window.AfterPay.transfer({token: afterpayToken});
|
|
|
+ } else {
|
|
|
+ //创建订单出错了
|
|
|
+ overlayLoading.stop();
|
|
|
+ confirmDialog({
|
|
|
+ title: "Warning",
|
|
|
+ content: res.msg + " Create order failed. Please try again.",
|
|
|
+ noCancel: true,
|
|
|
+ }).then(() => {});
|
|
|
}
|
|
|
} else {
|
|
|
overlayLoading.stop();
|
|
|
@@ -304,15 +314,131 @@ export function PaymentMethodContinueTpPay({
|
|
|
return true;
|
|
|
}, []);
|
|
|
|
|
|
- const airwallexCardElementShow = selectedPaymentMethod === 'airwallex' && airwallexCardState.cardNumberReady && airwallexCardState.expiryReady && airwallexCardState.cvcReady;
|
|
|
+ const klarnaPlaceOrder = async () => {
|
|
|
+ overlayLoading.start();
|
|
|
+
|
|
|
+ const resRepay = await paymentRepay({
|
|
|
+ orderId:Number(orderId),
|
|
|
+ paymentMethod: selectedPaymentMethod,
|
|
|
+ paymentSuccessUrl: window.location.origin + '/paymentresult/result',
|
|
|
+ paymentCancelUrl: window.location.origin + '/paymentresult/result'
|
|
|
+ });
|
|
|
+
|
|
|
+ if(!resRepay.error && resRepay.data !== null) {
|
|
|
+ const klarnaToken = resRepay.data.gatewayOrderId;
|
|
|
+ const webOrderId = resRepay.data.orderId;
|
|
|
+ const billingAddress = billingContact;
|
|
|
+ const shippingAddress = shippingContact;
|
|
|
+
|
|
|
+ const klarnaClientToken = klarnaToken.split("|")[0];
|
|
|
+ const klarnaVerifyToken = klarnaToken.split("|")[1];
|
|
|
+ window.Klarna.Payments.init({
|
|
|
+ client_token: klarnaClientToken,
|
|
|
+ });
|
|
|
+ //klarna-widget-container
|
|
|
+ window.Klarna.Payments.load({
|
|
|
+ container: '#klarna-widget-container',
|
|
|
+ }, (response) => {
|
|
|
+ if(!response.show_form) { // klarna不可用
|
|
|
+ // 跳转结果页
|
|
|
+ router.replace('/paymentresult/result?orderId=' + webOrderId + '&return_result=failure');
|
|
|
+ } else {
|
|
|
+
|
|
|
+
|
|
|
+ window.Klarna.Payments.authorize(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ billing_address: {
|
|
|
+ given_name: billingAddress.firstName,
|
|
|
+ family_name: billingAddress.lastName,
|
|
|
+ email: billingAddress.email,
|
|
|
+ phone: billingAddress.phone,
|
|
|
+ street_address: billingAddress.address,
|
|
|
+ postal_code: billingAddress.postcode,
|
|
|
+ city: billingAddress.city,
|
|
|
+ country: billingAddress.country
|
|
|
+ },
|
|
|
+ shipping_address: {
|
|
|
+ given_name: shippingAddress.firstName,
|
|
|
+ family_name: shippingAddress.lastName,
|
|
|
+ email: shippingAddress.email,
|
|
|
+ street_address: shippingAddress.address,
|
|
|
+ postal_code: shippingAddress.postcode,
|
|
|
+ city: shippingAddress.city,
|
|
|
+ phone: shippingAddress.phone,
|
|
|
+ region: shippingAddress.state,
|
|
|
+ country: shippingAddress.country
|
|
|
+ },
|
|
|
+ // customer: {
|
|
|
+ // date_of_birth: "1941-03-21",
|
|
|
+ // },
|
|
|
+ },
|
|
|
+ async function(res) {
|
|
|
+ console.log('klarna authorize res ---- ',res);
|
|
|
+ if(res.approved && res.authorization_token) { // 可以支付
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- const hasAfterpay = paymentMethods.some(x=>x.method==="afterpay");
|
|
|
+ const resCallback = await createPaymentCallback({
|
|
|
+ orderId: Number(webOrderId),
|
|
|
+ gatewayOrderId: res.authorization_token,
|
|
|
+ verifyToken: klarnaVerifyToken,
|
|
|
+ status: "success",
|
|
|
+ });
|
|
|
+ // 跳转结果页
|
|
|
+ overlayLoading.stop();
|
|
|
+ if(!resCallback.error) {
|
|
|
+ router.replace('/paymentresult/result?orderId=' + webOrderId + '&return_result=success');
|
|
|
+ } else {
|
|
|
+ // callback 失败,提示错误
|
|
|
+ confirmDialog({
|
|
|
+ title: "Somthing Wrong",
|
|
|
+ content: resCallback.msg + " Please contact customer service." + " OrderId: "+ webOrderId,
|
|
|
+ noCancel: true,
|
|
|
+ }).then(() => {
|
|
|
+ router.replace('/');
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- if(hasAfterpay){
|
|
|
- loadAfterpayScript();
|
|
|
+ } else {
|
|
|
+ // if(res.show_form) { // klarna可用,但是有报错
|
|
|
+ // } else {
|
|
|
+ // // klarna 不可用 跳转结果页
|
|
|
+ // }
|
|
|
+ overlayLoading.stop();
|
|
|
+ router.replace('/paymentresult/result?orderId=' + webOrderId + '&return_result=failure');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //创建订单出错了
|
|
|
+ overlayLoading.stop();
|
|
|
+ confirmDialog({
|
|
|
+ title: "Warning",
|
|
|
+ content: resRepay.msg + " Create order failed. Please try again.",
|
|
|
+ noCancel: true,
|
|
|
+ }).then(() => {});
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ const changePaymentMethod = async (method: string) => {
|
|
|
+ if(method === 'klarna' ||
|
|
|
+ method === 'afterpay' ||
|
|
|
+ method === 'clearpay' ||
|
|
|
+ method === 'applepay'
|
|
|
+ ) {
|
|
|
+ loadSdk(method);
|
|
|
}
|
|
|
- }, [paymentMethods]);
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ const airwallexCardElementShow = selectedPaymentMethod === 'airwallex' && airwallexCardState.cardNumberReady && airwallexCardState.expiryReady && airwallexCardState.cvcReady;
|
|
|
+
|
|
|
return (
|
|
|
<>
|
|
|
<form name="shipping method" className="w-full box-border px-4 mt-4">
|
|
|
@@ -344,6 +470,9 @@ export function PaymentMethodContinueTpPay({
|
|
|
value={item.method}
|
|
|
{...register('paymentMethod', {
|
|
|
required: 'Please select a shipping method.',
|
|
|
+ onChange: (event) => {
|
|
|
+ changePaymentMethod(event.target.value);
|
|
|
+ },
|
|
|
})}
|
|
|
/>
|
|
|
</label>
|
|
|
@@ -412,6 +541,11 @@ export function PaymentMethodContinueTpPay({
|
|
|
isRePay={true}
|
|
|
/>
|
|
|
}
|
|
|
+ {selectedPaymentMethod === 'klarna' &&
|
|
|
+ <KlarnaButton
|
|
|
+ clickHandler={klarnaPlaceOrder}
|
|
|
+ />
|
|
|
+ }
|
|
|
|
|
|
</div>
|
|
|
</div>
|