Skip to content

Commit 718b28e

Browse files
committed
fix
1 parent 073d1e4 commit 718b28e

File tree

3 files changed

+266
-5
lines changed

3 files changed

+266
-5
lines changed

src/Http/Controllers/Api/V1/Admin/Catalog/ProductController.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function quickCreate(Request $request){
6060
]);
6161
}
6262

63+
64+
65+
6366
$input = [];
6467
$input['sku'] = $req['sku'];
6568
$input['type'] = 'configurable';
@@ -254,6 +257,10 @@ public function quickCreate(Request $request){
254257
$skus = $request->input('tableData');
255258

256259
$categories = $request->input('categories');
260+
261+
262+
263+
257264
$categories[] = 5; // add the default category
258265

259266
$Variants = [];
@@ -267,8 +274,8 @@ public function quickCreate(Request $request){
267274
}
268275

269276
// match the variants to the sku id
270-
271-
277+
278+
// print_r($categories);
272279
$i = 0;
273280
foreach($skus as $key=>$sku) {
274281
$Variant = [];
@@ -329,12 +336,19 @@ public function quickCreate(Request $request){
329336

330337
//Log::info("quick-create-product: ".json_encode($tableData));
331338

339+
340+
341+
332342
$product = $this->getRepositoryInstance()->update($tableData, $id);
333343

334344
Event::dispatch('catalog.product.update.after', $product);
335345

336346
$images = $request->input('images');
337347

348+
349+
350+
351+
338352
// add images to the product
339353
$productImages = [];
340354
foreach($images as $key=>$image) {
@@ -345,6 +359,10 @@ public function quickCreate(Request $request){
345359
];
346360
}
347361

362+
363+
364+
365+
348366
$product->images()->createMany($productImages);
349367

350368
return response([

src/Http/Controllers/Api/V1/Shop/Customer/CartController.php

+242-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Webkul\Product\Repositories\ProductRepository;
1717
use NexaMerchant\Apis\Http\Resources\Api\V1\Shop\Checkout\CartResource;
1818
use Webkul\Sales\Repositories\OrderRepository;
19+
use Illuminate\Support\Facades\Redis;
1920

2021
class CartController extends CustomerController
2122
{
@@ -331,8 +332,6 @@ public function moveToWishlist(int $cartItemId)
331332

332333
public function OrderAddrAfter(Request $request) {
333334

334-
335-
336335
$input = $request->all();
337336

338337
//$last_order_id = $request->session()->get('last_order_id'); // check the laster order id
@@ -741,4 +740,245 @@ public function OrderConfirm(Request $request) {
741740
$data['order_id'] = $order_id;
742741
return response()->json($data);
743742
}
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+
744984
}

src/Routes/V1/Shop/customers-routes.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@
134134

135135
Route::delete('coupon', 'removeCoupon');
136136

137+
// paypal
137138
Route::post('order_addr_after', 'OrderAddrAfter');
138-
139+
// paypal confirm
139140
Route::post('order-status', 'OrderStatus');
141+
// airwallex
142+
Route::post('order-add-sync', 'OrderAddSync');
140143
});
141144

142145
/**

0 commit comments

Comments
 (0)