1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace bSecure\UniversalCheckout\Controllers\Orders; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
6
|
|
|
|
7
|
|
|
//Models |
8
|
|
|
use bSecure\UniversalCheckout\Models\Order; |
9
|
|
|
|
10
|
|
|
//Helper |
11
|
|
|
use bSecure\UniversalCheckout\Helpers\AppException; |
|
|
|
|
12
|
|
|
use bSecure\UniversalCheckout\Helpers\ApiResponseHandler; |
13
|
|
|
|
14
|
|
|
//Facade |
15
|
|
|
use Validator; |
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class CreateOrderController extends Controller |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected $validationRule = [ |
22
|
|
|
'order_id' => 'order_id', |
23
|
|
|
'customer' => 'customer', |
24
|
|
|
'products' => 'products', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Author: Sara Hasan |
31
|
|
|
* Date: 10-November-2020 |
32
|
|
|
*/ |
33
|
|
|
public function create($orderData) |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
$validator = Validator::make($orderData, Order::$validationRules['createOrder']); |
37
|
|
|
|
38
|
|
|
if ($validator->fails()) { |
39
|
|
|
return ApiResponseHandler::validationError($validator->errors()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$orderResponse = Order::createMerchantOrder($orderData); |
43
|
|
|
if($orderResponse['error']) |
44
|
|
|
{ |
45
|
|
|
return ApiResponseHandler::failure($orderResponse['message']); |
46
|
|
|
}else{ |
47
|
|
|
$response = $orderResponse['body']; |
48
|
|
|
return ApiResponseHandler::success($response, trans('bSecure::messages.order.success')); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
return ApiResponseHandler::failure(trans('bSecure::messages.order.failure'), $e->getTraceAsString()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function _setProductsDataStructure($products) |
57
|
|
|
{ |
58
|
|
|
$orderData = [ |
59
|
|
|
'products' => null, |
60
|
|
|
'sub_total_amount' => null, |
61
|
|
|
'discount_amount' => null, |
62
|
|
|
'total_amount' => null, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
if(!empty($products)) |
66
|
|
|
{ |
67
|
|
|
|
68
|
|
|
$orderItems = []; |
69
|
|
|
$sub_total_amount = 0; |
70
|
|
|
$total_discount = 0; |
71
|
|
|
$orderItems['products'] = []; |
72
|
|
|
|
73
|
|
|
foreach ($products as $key => $product) { |
74
|
|
|
//Product Price |
75
|
|
|
$price = $product['price'] ; |
76
|
|
|
$sale_price = $product['sale_price']; |
77
|
|
|
$quantity = $product['quantity'] ?? 1; |
78
|
|
|
|
79
|
|
|
//Product options |
80
|
|
|
$product_options = $this->_setProductOptionsDataStructure($product); |
81
|
|
|
|
82
|
|
|
$options_price = $product_options['price']; |
83
|
|
|
$options = $product_options['options']; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
#Product charges |
87
|
|
|
$discount = ( $price - $sale_price ) * $quantity; |
88
|
|
|
$product_price = ( $price + $options_price ) * $quantity; |
89
|
|
|
$product_sub_total = ( $price + $options_price ) * $quantity; |
90
|
|
|
|
91
|
|
|
$orderItems['products'][] = [ |
92
|
|
|
"id" => $product['id'], |
93
|
|
|
"name" => $product['name'], |
94
|
|
|
"sku" => $product['sku'], |
95
|
|
|
"quantity" => $quantity, |
96
|
|
|
"price" => $price, |
97
|
|
|
"sale_price" => $sale_price, |
98
|
|
|
"discount" => $discount, |
99
|
|
|
"sub_total" => $product_price, |
100
|
|
|
"image" => $product['image'], |
101
|
|
|
"short_description" => $product['short_description'], |
102
|
|
|
"description" => $product['description'], |
103
|
|
|
"product_options" => $options |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$total_discount += $discount; |
107
|
|
|
$sub_total_amount += $product_sub_total; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$order_grand_total = $sub_total_amount-$total_discount; |
111
|
|
|
|
112
|
|
|
$orderData['products'] = $orderItems['products']; |
113
|
|
|
$orderData['sub_total_amount'] = $sub_total_amount; |
114
|
|
|
$orderData['discount_amount'] = $total_discount; |
115
|
|
|
$orderData['total_amount'] = $order_grand_total; |
116
|
|
|
} |
117
|
|
|
return $orderData; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function _setProductOptionsDataStructure($product) |
122
|
|
|
{ |
123
|
|
|
$product_options = array_key_exists('product_options',$product) ? $product['product_options'] : []; |
124
|
|
|
|
125
|
|
|
$price = 0; |
126
|
|
|
if( isset($product_options) && !empty($product_options) ) |
127
|
|
|
{ |
128
|
|
|
foreach( $product_options as $productOption ) |
129
|
|
|
{ |
130
|
|
|
$productValue = array_key_exists('value',$productOption) ? $productOption['value'] : []; |
131
|
|
|
foreach( $productValue as $key => $optionValue ) |
132
|
|
|
{ |
133
|
|
|
$optionPrice = array_key_exists('price',$optionValue) ? $optionValue['price'] : []; |
134
|
|
|
if(!empty($optionPrice)) |
135
|
|
|
{ |
136
|
|
|
#Price ++ |
137
|
|
|
$price += $optionPrice; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
'price' => $price, |
145
|
|
|
'options' => $product_options |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
public function _setCustomer($customerData) |
151
|
|
|
{ |
152
|
|
|
$customer = []; |
153
|
|
|
if(!empty($customerData)) |
154
|
|
|
{ |
155
|
|
|
$auth_code = array_key_exists('auth_code',$customerData) ? $customerData['auth_code'] : '' ; |
156
|
|
|
|
157
|
|
|
if( !empty( $auth_code ) ) |
158
|
|
|
{ |
159
|
|
|
$customer = [ |
160
|
|
|
"auth_code" => $auth_code, |
161
|
|
|
];; |
162
|
|
|
} |
163
|
|
|
else{ |
164
|
|
|
$customer = [ |
165
|
|
|
"country_code" => array_key_exists('country_code',$customerData) ? $customerData['country_code'] : '', |
166
|
|
|
"phone_number" => array_key_exists('phone_number',$customerData) ? $customerData['phone_number'] : '', |
167
|
|
|
"name" => array_key_exists('name',$customerData) ? $customerData['name'] : '', |
168
|
|
|
"email" => array_key_exists('email',$customerData) ? $customerData['email'] : '', |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $customer; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
public function _setShipmentDetails($shipmentData) |
179
|
|
|
{ |
180
|
|
|
$shipmentDetail = [ |
181
|
|
|
"charges" => '', |
182
|
|
|
"method_name" => '', |
183
|
|
|
]; |
184
|
|
|
if(!empty($shipmentData)) |
185
|
|
|
{ |
186
|
|
|
$shipmentDetail['charges'] = array_key_exists('charges',$shipmentData) ? $shipmentData['charges'] : ''; |
187
|
|
|
$shipmentDetail['method_name'] = array_key_exists('method_name',$shipmentData) ? $shipmentData['method_name'] : ''; |
188
|
|
|
} |
189
|
|
|
return $shipmentDetail; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths