-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.php
80 lines (66 loc) · 2.08 KB
/
api.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
<?php
class MagentoClient {
public $bearer_token = '';
public $base_url = '';
public function __construct($token, $base_url) {
$this->base_url = $base_url;
$this->bearer_token = $token;
}
public function request($endpoint, $method = 'GET', $body = FALSE) {
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->base_url . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers = array();
$headers[] = "Authorization: Bearer " . $this->bearer_token;
if ($body) {
$headers[] = "Content-Type: application/json";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
return $result;
}
public function getProduct($product_id) {
return $this->request('/products/' . $product_id . '/', 'GET');
}
public function createCart() {
return $this->request('/guest-carts/', 'POST');
}
public function addToCart($cart_id, $product_sku, $quantity = 1) {
$order = array(
'cartItem' => array(
'quote_id' => $cart_id,
'sku' => $product_sku,
'qty' => $quantity,
)
);
return $this->request('/guest-carts/' . $cart_id . '/items',
'POST',
json_encode($order)
);
}
public function setShipping($cart_id, $shipping) {
return $this->request('/guest-carts/' . $cart_id . '/shipping-information',
'POST',
json_encode($shipping)
);
}
public function placeOrder($cart_id, $payment_method = 'cashondelivery') {
$payment = array(
'paymentMethod' => array('method' => $payment_method)
);
return $this->request('/guest-carts/' . $cart_id . '/order',
'PUT',
json_encode($payment)
);
}
public function getPaymentMethods($cart_id) {
return $this->request('/guest-carts/' . $cart_id . '/payment-information', 'GET');
}
}