|
16 | 16 | use Webkul\Product\Repositories\ProductRepository;
|
17 | 17 | use NexaMerchant\Apis\Http\Resources\Api\V1\Shop\Checkout\CartResource;
|
18 | 18 | use Webkul\Sales\Repositories\OrderRepository;
|
| 19 | +use Illuminate\Support\Facades\Redis; |
19 | 20 |
|
20 | 21 | class CartController extends CustomerController
|
21 | 22 | {
|
@@ -331,8 +332,6 @@ public function moveToWishlist(int $cartItemId)
|
331 | 332 |
|
332 | 333 | public function OrderAddrAfter(Request $request) {
|
333 | 334 |
|
334 |
| - |
335 |
| - |
336 | 335 | $input = $request->all();
|
337 | 336 |
|
338 | 337 | //$last_order_id = $request->session()->get('last_order_id'); // check the laster order id
|
@@ -741,4 +740,245 @@ public function OrderConfirm(Request $request) {
|
741 | 740 | $data['order_id'] = $order_id;
|
742 | 741 | return response()->json($data);
|
743 | 742 | }
|
| 743 | + |
| 744 | + // airwallex payment |
| 745 | + public function OrderAddSync(Request $request) { |
| 746 | + $input = $request->all(); |
| 747 | + $addressData = []; |
| 748 | + |
| 749 | + $payment_method_input = $request->input('payment_method'); |
| 750 | + $refer = isset($input['refer']) ? trim($input['refer']) : ""; |
| 751 | + |
| 752 | + $addressData['billing'] = []; |
| 753 | + $address1 = []; |
| 754 | + array_push($address1, $input['address']); |
| 755 | + $addressData['billing']['city'] = $input['city']; |
| 756 | + $addressData['billing']['country'] = $input['country']; |
| 757 | + $addressData['billing']['email'] = $input['email']; |
| 758 | + $addressData['billing']['first_name'] = $input['first_name']; |
| 759 | + $addressData['billing']['last_name'] = $input['second_name']; |
| 760 | + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); |
| 761 | + $addressData['billing']['phone'] = $input['phone_full']; |
| 762 | + $addressData['billing']['postcode'] = $input['code']; |
| 763 | + $addressData['billing']['state'] = $input['province']; |
| 764 | + $addressData['billing']['use_for_shipping'] = true; |
| 765 | + $addressData['billing']['address1'] = $address1; |
| 766 | + |
| 767 | + $addressData['billing']['address1'] = implode(PHP_EOL, $addressData['billing']['address1']); |
| 768 | + |
| 769 | + $shipping = []; |
| 770 | + $address1 = []; |
| 771 | + array_push($address1, $input['address']); |
| 772 | + $shipping['city'] = $input['city']; |
| 773 | + $shipping['country'] = $input['country']; |
| 774 | + $shipping['email'] = $input['email']; |
| 775 | + $shipping['first_name'] = $input['first_name']; |
| 776 | + $shipping['last_name'] = $input['second_name']; |
| 777 | + //undefined+ |
| 778 | + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); |
| 779 | + $shipping['phone'] = $input['phone_full']; |
| 780 | + $shipping['postcode'] = $input['code']; |
| 781 | + $shipping['state'] = $input['province']; |
| 782 | + $shipping['use_for_shipping'] = true; |
| 783 | + $shipping['address1'] = $address1; |
| 784 | + $shipping['address1'] = implode(PHP_EOL, $shipping['address1']); |
| 785 | + |
| 786 | + |
| 787 | + $addressData['shipping'] = $shipping; |
| 788 | + $addressData['shipping']['isSaved'] = false; |
| 789 | + $address1 = []; |
| 790 | + array_push($address1, $input['address']); |
| 791 | + $addressData['shipping']['address1'] = $address1; |
| 792 | + $addressData['shipping']['address1'] = implode(PHP_EOL, $addressData['shipping']['address1']); |
| 793 | + |
| 794 | + // customer bill address info |
| 795 | + if(@$input['shipping_address']=="other") { |
| 796 | + $address1 = []; |
| 797 | + array_push($address1, $input['bill_address']); |
| 798 | + $billing = []; |
| 799 | + $billing['city'] = $input['bill_city']; |
| 800 | + $billing['country'] = $input['bill_country']; |
| 801 | + $billing['email'] = $input['email']; |
| 802 | + $billing['first_name'] = $input['bill_first_name']; |
| 803 | + $billing['last_name'] = $input['bill_second_name']; |
| 804 | + //undefined+ |
| 805 | + $input['phone_full'] = str_replace('undefined+','', $input['phone_full']); |
| 806 | + $billing['phone'] = $input['phone_full']; |
| 807 | + $billing['postcode'] = $input['bill_code']; |
| 808 | + $billing['state'] = $input['bill_province']; |
| 809 | + //$billing['use_for_shipping'] = true; |
| 810 | + $billing['address1'] = $address1; |
| 811 | + $billing['address1'] = implode(PHP_EOL, $billing['address1']); |
| 812 | + |
| 813 | + // $billing['address1'] = implode(PHP_EOL, $billing['address1']); |
| 814 | + |
| 815 | + $addressData['billing'] = $billing; |
| 816 | + } |
| 817 | + |
| 818 | + |
| 819 | + Log::info("address" . json_encode($addressData)); |
| 820 | + |
| 821 | + if ( |
| 822 | + Cart::hasError() |
| 823 | + || ! Cart::saveCustomerAddress($addressData) |
| 824 | + ) { |
| 825 | + return new JsonResource([ |
| 826 | + 'redirect' => false, |
| 827 | + 'data' => Cart::getCart(), |
| 828 | + ]); |
| 829 | + } |
| 830 | + |
| 831 | + |
| 832 | + |
| 833 | + // |
| 834 | + $shippingMethod = "free_free"; // free shipping |
| 835 | + $shippingMethod = "flatrate_flatrate"; |
| 836 | + |
| 837 | + if ( |
| 838 | + Cart::hasError() |
| 839 | + || ! $shippingMethod |
| 840 | + || ! Cart::saveShippingMethod($shippingMethod) |
| 841 | + ) { |
| 842 | + return response()->json([ |
| 843 | + 'redirect_url' => route('shop.checkout.cart.index'), |
| 844 | + ], Response::HTTP_FORBIDDEN); |
| 845 | + } |
| 846 | + |
| 847 | + Cart::collectTotals(); |
| 848 | + |
| 849 | + |
| 850 | + if($payment_method_input=="airwallex_klarna") $payment_method = "airwallex"; |
| 851 | + if($payment_method_input=="airwallex_dropin") $payment_method = "airwallex"; |
| 852 | + if($payment_method_input=="airwallex_google") $payment_method = "airwallex"; |
| 853 | + if($payment_method_input=="airwallex_apple") $payment_method = "airwallex"; |
| 854 | + if($payment_method_input=="airwallex") $payment_method = "airwallex"; |
| 855 | + |
| 856 | + // when enable the upselling and can config the upselling rule for carts |
| 857 | + if($payment_method=='airwallex') { |
| 858 | + // |
| 859 | + $payment = []; |
| 860 | + $payment['description'] = $payment_method."-".$refer; |
| 861 | + $payment['method'] = $payment_method; |
| 862 | + $payment['method_title'] = $payment_method."-".$refer; |
| 863 | + $payment['sort'] = "2"; |
| 864 | + // Cart::savePaymentMethod($payment); |
| 865 | + |
| 866 | + if ( |
| 867 | + Cart::hasError() |
| 868 | + || ! $payment |
| 869 | + || ! Cart::savePaymentMethod($payment) |
| 870 | + ) { |
| 871 | + return response()->json([ |
| 872 | + 'redirect_url' => route('shop.checkout.cart.index'), |
| 873 | + ], Response::HTTP_FORBIDDEN); |
| 874 | + } |
| 875 | + |
| 876 | + |
| 877 | + Cart::collectTotals(); |
| 878 | + $this->validateOrder(); |
| 879 | + $cart = Cart::getCart(); |
| 880 | + |
| 881 | + $order = $this->orderRepository->create(Cart::prepareDataForOrder()); |
| 882 | + // Cart::deActivateCart(); |
| 883 | + // Cart::activateCartIfSessionHasDeactivatedCartId(); |
| 884 | + $data['result'] = 200; |
| 885 | + $data['order'] = $order; |
| 886 | + if ($order) { |
| 887 | + $orderId = $order->id; |
| 888 | + |
| 889 | + //customer id |
| 890 | + $cus_id = isset($input['cus_id']) ? trim($input['cus_id']) : null; |
| 891 | + |
| 892 | + $airwallex_customer = []; |
| 893 | + if(is_null($cus_id)) { |
| 894 | + //Step 1: Create a Customer |
| 895 | + //var_dump($order->id); |
| 896 | + try { |
| 897 | + $airwallex_customer = $this->airwallex->createCustomer($cart, $order->id); |
| 898 | + $cus_id = $airwallex_customer->id; |
| 899 | + } catch (\Exception $e) { |
| 900 | + return response()->json(['error' => $e->getMessage(),'code'=>'203'], 400); |
| 901 | + } |
| 902 | + }else{ |
| 903 | + $airwallex_customer['id'] = $cus_id; |
| 904 | + } |
| 905 | + |
| 906 | + //create a airwallex payment order |
| 907 | + $transactionManager = $this->airwallex->createPaymentOrder($cart, $order->id, $cus_id); |
| 908 | + //Step 2: Generate a client secret for the Customer |
| 909 | + $customerClientSecret = $this->airwallex->createCustomerClientSecret($cus_id); |
| 910 | + if(!isset($transactionManager->client_secret)) { |
| 911 | + response()->json(['error' => $transactionManager->body->message,'code'=>'203'], 400); |
| 912 | + } |
| 913 | + |
| 914 | + //$transactionManager = $this->airwallex->createPaymentOrder($cart, $order->id); |
| 915 | + Log::info("airwallex-".$order->id."--".json_encode($transactionManager)); |
| 916 | + $data['client_secret'] = $transactionManager->client_secret; |
| 917 | + $data['payment_intent_id'] = $transactionManager->id; |
| 918 | + $data['currency'] = $transactionManager->currency; |
| 919 | + $data['transaction'] = $transactionManager; |
| 920 | + $data['customer'] = $airwallex_customer; |
| 921 | + $data['customer_client_secret'] = $customerClientSecret; |
| 922 | + $data['country'] = $input['country']; |
| 923 | + $data['billing'] = $addressData['billing']; |
| 924 | + $data['airwallex'] = $this->airwallex; |
| 925 | + |
| 926 | + // redis save the customer id from airwallex |
| 927 | + Redis::set("airwallex_customer_".$order->id, $cus_id); |
| 928 | + } |
| 929 | + |
| 930 | + return response()->json($data); |
| 931 | + } |
| 932 | + |
| 933 | + } |
| 934 | + |
| 935 | + /** |
| 936 | + * Validate order before creation. |
| 937 | + * |
| 938 | + * @return void|\Exception |
| 939 | + */ |
| 940 | + public function validateOrder() |
| 941 | + { |
| 942 | + $cart = Cart::getCart(); |
| 943 | + |
| 944 | + $minimumOrderAmount = core()->getConfigData('sales.order_settings.minimum_order.minimum_order_amount') ?: 0; |
| 945 | + |
| 946 | + if ( |
| 947 | + auth()->guard('customer')->check() |
| 948 | + && auth()->guard('customer')->user()->is_suspended |
| 949 | + ) { |
| 950 | + throw new \Exception(trans('shop::app.checkout.cart.suspended-account-message')); |
| 951 | + } |
| 952 | + |
| 953 | + if ( |
| 954 | + auth()->guard('customer')->user() |
| 955 | + && ! auth()->guard('customer')->user()->status |
| 956 | + ) { |
| 957 | + throw new \Exception(trans('shop::app.checkout.cart.inactive-account-message')); |
| 958 | + } |
| 959 | + |
| 960 | + if (! $cart->checkMinimumOrder()) { |
| 961 | + throw new \Exception(trans('shop::app.checkout.cart.minimum-order-message', ['amount' => core()->currency($minimumOrderAmount)])); |
| 962 | + } |
| 963 | + |
| 964 | + if ($cart->haveStockableItems() && ! $cart->shipping_address) { |
| 965 | + throw new \Exception(trans('shop::app.checkout.cart.check-shipping-address')); |
| 966 | + } |
| 967 | + |
| 968 | + if (! $cart->billing_address) { |
| 969 | + throw new \Exception(trans('shop::app.checkout.cart.check-billing-address')); |
| 970 | + } |
| 971 | + |
| 972 | + if ( |
| 973 | + $cart->haveStockableItems() |
| 974 | + && ! $cart->selected_shipping_rate |
| 975 | + ) { |
| 976 | + throw new \Exception(trans('shop::app.checkout.cart.specify-shipping-method')); |
| 977 | + } |
| 978 | + |
| 979 | + if (! $cart->payment) { |
| 980 | + throw new \Exception(trans('shop::app.checkout.cart.specify-payment-method')); |
| 981 | + } |
| 982 | + } |
| 983 | + |
744 | 984 | }
|
0 commit comments