CheckoutPlaceOrder.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use client";
  2. import { usePlaceOrder } from "@/utils/hooks/usePlaceOrder";
  3. import PaypalButton from "./PaymentButton/PaypalButton";
  4. import { overlayLoading } from "@/components/theme/ui/kernel/loading/api";
  5. import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
  6. import { useAppDispatch } from "@/store/hooks";
  7. import { clearCart } from "@/store/slices/cart-slice";
  8. export default function CheckoutPlaceOrder({
  9. paymentMethod,
  10. clickPlaceOrder, // 校验运输方式,地址
  11. }: {
  12. paymentMethod: string;
  13. clickPlaceOrder: () => Promise<boolean>;
  14. }) {
  15. const dispatch = useAppDispatch();
  16. const { createOrder } = usePlaceOrder();
  17. const handleCreateOrder = async () => {
  18. const res = await createOrder();
  19. console.log("createOrder res ---- ", res);
  20. if(!res.error && res.data) {
  21. //创建订单成功后清空redux 购物车
  22. dispatch(clearCart());
  23. return {
  24. error: res.error,
  25. msg: '',
  26. orderId: res.data.gatewayOrderId,
  27. webOrderId: res.data.orderId
  28. };
  29. } else {
  30. return {
  31. error: res.error,
  32. msg: res.msg,
  33. orderId: res.data?.gatewayOrderId ?? '',
  34. webOrderId: res.data?.orderId ?? ''
  35. };
  36. }
  37. };
  38. const airwallexPlaceOrder = async () => {
  39. overlayLoading.start();
  40. const vaild = await clickPlaceOrder();
  41. if(vaild) {
  42. const res = await createOrder({
  43. paymentSuccessUrl: window.location.origin + '/paymentresult/result'
  44. });
  45. console.log("createOrder res ---- ", res);
  46. if(!res.error && res.data) {
  47. // 创建订单成功了,跳转支付页面
  48. //创建订单成功后清空redux 购物车
  49. dispatch(clearCart());
  50. window.location.href = res.data.gatewayOrderId;
  51. } else {
  52. //创建订单出错了
  53. overlayLoading.stop();
  54. confirmDialog({
  55. title: "Warning",
  56. content: res.msg + " Create order failed. Please try again.",
  57. noCancel: true,
  58. }).then(() => {});
  59. }
  60. } else {
  61. overlayLoading.stop();
  62. }
  63. };
  64. return (
  65. <>
  66. <div className="relative w-full">
  67. {paymentMethod === 'paypal_smart_button' &&
  68. <PaypalButton
  69. createOrder={handleCreateOrder}
  70. onPaymentClick={clickPlaceOrder}
  71. />
  72. }
  73. {(paymentMethod === 'awxklarna' || paymentMethod === 'awxafterpay') &&
  74. <button className="flex items-center justify-center w-full h-12 bg-ly-green text-white rounded-3xl text-ly-16 font-bold"
  75. onClick={airwallexPlaceOrder}
  76. >
  77. Place Order
  78. </button>
  79. }
  80. </div>
  81. </>
  82. );
  83. }