-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_request.php
174 lines (140 loc) · 4.5 KB
/
api_request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Mavlyan;
#Coupon
$cardNumber = '0123456789';
$wsdlUrl = 'http://stage.magento.shop/api/v2_soap?wsdl=1';
$apiUser = 'userapi';
$apiKey = 'apikey';
$arData['login'] = 'BAA_login';
$arData['password'] = 'BAA_password';
$arData['trace'] = 'true';
$proxy = new \SoapClient($wsdlUrl, $arData);
//common object to store all data for order creation
$total = new \stdClass();
//Check your store ID
$total->storeId = 1;
try {
$sessionId = connect();
createCart($proxy, $sessionId);
addAddress();
addPayment();
$couponResult = applyCoupon();
$orderId = placeOrder();
echo '<pre>';
var_dump($orderId);
var_dump($couponResult);
var_dump($total);
echo '<pre>';
} catch (\Exception $e) {
echo '<h1>Fail</h1>';
echo '<p>' . $e->getMessage() . '</p>';
echo '<p>' . $e->getTraceAsString() . '</p>';
var_dump($total);
}
function connect()
{
global $apiUser, $apiKey, $proxy;
$sessionId = $proxy->login((object)
[
'username' => $apiUser,
'apiKey' => $apiKey
]
);
$sessionId->sessionId = $sessionId->result;
return $sessionId;
}
function createCart($proxy, $sessionId)
{
global $total;
$cartId = $proxy->shoppingCartCreate($sessionId);
$total->quoteId = $cartId->result;
$total->sessionId = $sessionId->sessionId;
//add product to cart
$total->productId = 365733;//371034;
$total->filters = [
['sku' => 'N252010000', 'id' => 365733]
];
$product = $proxy->catalogProductInfo($total);
$product = (array)$product->result;
$product['qty'] = 1;
$total->productsData = [$product];
$proxy->shoppingCartProductAdd($total);
return $total;
}
function addAddress() {
global $total, $proxy;
//load existing customer with id = 370
$total->customerId = 370;
$customer = $proxy->customerCustomerInfo($total);
$customer->result->mode = 'customer';
$customer->mode = 'customer';
$total->customerData = (array)$customer->result;
$proxy->shoppingCartCustomerSet($total);
$address = [
[
'mode' => 'shipping',
'firstname' => $customer->result->firstname,
'lastname' => $customer->result->lastname,
'street' => 'street address',
'city' => 'Moscow',
'region' => 'region',
'telephone' => '123123123123',
'postcode' => '123123',
'country_id' => '6',
'is_default_shipping' => 0,
'is_default_billing' => 0
],
[
'mode' => 'billing',
'firstname' => $customer->result->firstname,
'lastname' => $customer->result->lastname,
'street' => 'street address',
'city' => 'city',
'region' => 'region',
'telephone' => 'phone number',
'postcode' => 'postcode',
'country_id' => 'country ID',
'is_default_shipping' => 0,
'is_default_billing' => 0
],
];
$total->customerAddressData = $address;
// add customer address
$proxy->shoppingCartCustomerAddresses($total);
//TODO: Load here all shipping methods and select 1st one
$total->shippingMethod = 'flatrate_flatrate';
// $total->shippingMethod = 'cdek_1';
// $total->shippingMethod = 'dpd_error';
// $total->shippingMethod = 'flatrate_error';
// add shipping method
$proxy->shoppingCartShippingMethod($total);
}
function addPayment() {
global $total, $proxy;
$paymentMethod = [
'po_number' => null,
'method' => 'checkmo',
'title' => 'Payment',
'cc_cid' => null,
'cc_owner' => null,
'cc_number' => null,
'cc_type' => null,
'cc_exp_year' => null,
'cc_exp_month' => null
];
$total->paymentData = $paymentMethod;
// add payment method
$proxy->shoppingCartPaymentMethod($total);
}
function placeOrder() {
global $total, $proxy;
// place the order
$orderId = $proxy->shoppingCartOrder($total);
return $orderId;
}
function applyCoupon() {
global $cardNumber, $total, $proxy;
$total->couponCode = $cardNumber;
$couponResult = $proxy->shoppingCartCouponAdd($total);
return $couponResult;
}