PaymentMethodContinueTpPay.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use client";
  2. import Image from "next/image";
  3. import { use } from "react";
  4. import { useForm, get } from "react-hook-form";
  5. import {RepayOrderPaymentMethodsData} from "@/types/checkout/type";
  6. import {GraphqlRequestResult} from "@/types/graphqlFetch/type";
  7. import { usePlaceOrder } from "@/utils/hooks/usePlaceOrder";
  8. import PaypalButton from "../PaymentButton/PaypalButton";
  9. import { overlayLoading } from "@/components/theme/ui/kernel/loading/api";
  10. import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
  11. interface PaymentReplayResult {
  12. orderId: string;
  13. webOrderId: string;
  14. error: boolean;
  15. msg: string;
  16. }
  17. export function PaymentMethodContinueTpPay({
  18. paymentMethodsDataPromise,
  19. defaultPaymentMethod,
  20. orderId,
  21. }: {
  22. paymentMethodsDataPromise: Promise<GraphqlRequestResult<RepayOrderPaymentMethodsData>>;
  23. defaultPaymentMethod: {method: string; methodTitle: string;}
  24. orderId: string;
  25. }) {
  26. const paymentMethodsData = use(paymentMethodsDataPromise);
  27. const paymentMethods = paymentMethodsData.data?.collectionRepayOrderPaymentMethods || [];
  28. const { paymentRepay } = usePlaceOrder();
  29. const {
  30. register,
  31. watch,
  32. formState: { errors }
  33. } = useForm({
  34. mode: "onChange",
  35. reValidateMode: "onChange",
  36. defaultValues:{
  37. paymentMethod: defaultPaymentMethod.method
  38. }
  39. });
  40. // 参考 https://github.com/react-hook-form/error-message/blob/master/src/ErrorMessage.tsx
  41. const paymentMethodError = get(errors, "paymentMethod");
  42. const selectedPaymentMethod = watch("paymentMethod");
  43. console.log('selectedPaymentMethod -------- ',selectedPaymentMethod);
  44. const handleCreateOrder = async () => {
  45. return await paymentRepay({
  46. orderId: Number(orderId),
  47. paymentMethod: selectedPaymentMethod
  48. });
  49. };
  50. const handleClickPay = async () => {
  51. return true;
  52. };
  53. const airwallexRepay = async () => {
  54. overlayLoading.start();
  55. const res = await paymentRepay({
  56. orderId: Number(orderId),
  57. paymentMethod: selectedPaymentMethod,
  58. paymentSuccessUrl: window.location.origin + '/paymentresult/result'
  59. });
  60. console.log("airwallexPlaceOrder res ---- ", res);
  61. if(!res.error && res.orderId) {
  62. // 创建订单成功了,跳转支付页面
  63. window.location.href = res.orderId;
  64. } else {
  65. //创建订单出错了
  66. overlayLoading.stop();
  67. confirmDialog({
  68. title: "Warning",
  69. content: res.msg + " Create order failed. Please try again.",
  70. noCancel: true,
  71. }).then(() => {});
  72. }
  73. };
  74. return (
  75. <>
  76. <form name="shipping method" className="w-full box-border px-4 mt-4">
  77. <div className="mt-3 w-full">
  78. {paymentMethodsData.error && <p className="text-red-500 text-ly-12">{paymentMethodsData.error}</p>}
  79. {
  80. paymentMethods.map((item) => {
  81. return (
  82. <label htmlFor={"paymentmethod-" + item.method} key={item.method}
  83. className="group box-border flex items-center relative bg-ly-lightgray mt-3 p-3 rounded-sm has-checked:bg-white has-checked:border-1 has-checked:border-ly-green"
  84. >
  85. <p className="pr-2 flex-none relative">
  86. <Image className="block h-8 w-auto"
  87. src={item.icon || ''} alt={"Payment Method " + item.title}
  88. width={32}
  89. height={32}
  90. />
  91. </p>
  92. <p className="text-ly-12 text-[#666666] ml-1">{item.description}</p>
  93. <div className="absolute top-1/2 -translate-y-1/2 right-3 flex items-center">
  94. <svg className="check w-4 h-4 hidden group-has-[:checked]:block" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="1" width="14" height="14" rx="1" fill="#000000" ></rect><path stroke="rgba(255, 255, 255, 1)" strokeWidth="1.5" strokeLinecap="square" d="M5.3335 8L7.3335 10L11.3335 6"></path></svg>
  95. <svg className="uncheck w-4 h-4 block group-has-[:checked]:hidden" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="1" width="14" height="14" rx="1" fill="#FFFFFF" ></rect><path fill="rgba(0, 0, 0, 1)" d="M2 0.5L14 0.5C14.8284 0.5 15.5 1.17157 15.5 2L15.5 14C15.5 14.8284 14.8284 15.5 14 15.5L2 15.5C1.17157 15.5 0.5 14.8284 0.5 14L0.5 2C0.5 1.17157 1.17157 0.5 2 0.5ZM2 1.5C1.72386 1.5 1.5 1.72386 1.5 2L1.5 14C1.5 14.2761 1.72386 14.5 2 14.5L14 14.5C14.2761 14.5 14.5 14.2761 14.5 14L14.5 2C14.5 1.72386 14.2761 1.5 14 1.5L2 1.5Z"></path></svg>
  96. </div>
  97. <input id={"paymentmethod-" + item.method}
  98. className="absolute w-6 h-6 opacity-0"
  99. type="radio"
  100. value={item.method}
  101. {...register('paymentMethod', {
  102. required: 'Please select a shipping method.',
  103. })}
  104. />
  105. </label>
  106. )
  107. })
  108. }
  109. {paymentMethodError && <p className="text-red-500 text-ly-12">{paymentMethodError.message}</p>}
  110. </div>
  111. </form>
  112. <div className="w-full box-border px-4 mt-3">
  113. <p className="text-ly-12 text-[#666666] leading-4.75">
  114. By providing your information, you agree to Wiggins'sPrivacy Policyand Terms of Use.
  115. </p>
  116. <div className="mt-6 w-full">
  117. {selectedPaymentMethod === 'paypal_smart_button' &&
  118. <PaypalButton
  119. createOrder={handleCreateOrder}
  120. onPaymentClick={handleClickPay}
  121. isRePay={true}
  122. />
  123. }
  124. {(selectedPaymentMethod === 'awxklarna' || selectedPaymentMethod === 'awxafterpay') &&
  125. <button className="flex items-center justify-center w-full h-12 bg-ly-green text-white rounded-3xl text-ly-16 font-bold"
  126. onClick={airwallexRepay}
  127. >
  128. Place Order
  129. </button>
  130. }
  131. </div>
  132. </div>
  133. </>
  134. );
  135. }