1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AllDigitalRewards\WeGift; |
4
|
|
|
|
5
|
|
|
use AllDigitalRewards\WeGift\Entity\Balance; |
6
|
|
|
use AllDigitalRewards\WeGift\Entity\OrderDetails; |
7
|
|
|
use AllDigitalRewards\WeGift\Entity\OrderRequest; |
8
|
|
|
use AllDigitalRewards\WeGift\Entity\OrderResponse; |
9
|
|
|
use AllDigitalRewards\WeGift\Entity\Product; |
10
|
|
|
use AllDigitalRewards\WeGift\Entity\AuthResponse; |
11
|
|
|
use Exception; |
12
|
|
|
use Generator; |
13
|
|
|
use GuzzleHttp\Client as HttpClient; |
14
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
public const PLAYGROUND_URL = 'https://playground.wegift.io/api/b2b-sync/v1'; |
19
|
|
|
public const PRODUCTION_URL = 'https://api.wegift.io/api/b2b-sync/v1'; |
20
|
|
|
|
21
|
|
|
private HttpClient $http_client; |
22
|
|
|
|
23
|
|
|
public function __construct(protected string $user_id, protected string $api_key) |
24
|
|
|
{ |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function getEndpoint(): string |
28
|
|
|
{ |
29
|
|
|
if (getenv('ENVIRONMENT') === 'production') { |
30
|
|
|
return self::PRODUCTION_URL; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return self::PLAYGROUND_URL; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return HttpClient |
38
|
|
|
*/ |
39
|
|
|
public function getHttpClient(): HttpClient |
40
|
|
|
{ |
41
|
|
|
if (!isset($this->http_client)) { |
42
|
|
|
$this->http_client = new HttpClient(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->http_client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param HttpClient $http_client |
50
|
|
|
*/ |
51
|
|
|
public function setHttpClient(HttpClient $http_client): void |
52
|
|
|
{ |
53
|
|
|
$this->http_client = $http_client; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Authentication |
58
|
|
|
* |
59
|
|
|
* https://docs.wegift.io/#0583bf3c-1524-4618-86f2-c2e95805b4db |
60
|
|
|
* |
61
|
|
|
* @return AuthResponse |
62
|
|
|
* @throws GuzzleException |
63
|
|
|
*/ |
64
|
|
|
public function authentication(): AuthResponse |
65
|
|
|
{ |
66
|
|
|
$response = $this->getHttpClient()->get($this->getEndpoint() . '/auth', [ |
67
|
|
|
'auth' => [$this->user_id, $this->api_key] |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return new AuthResponse(json_decode($response->getBody(), true)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getBalanceByCurrency(string $currency_code): Balance |
74
|
|
|
{ |
75
|
|
|
if ( |
76
|
|
|
!in_array( |
77
|
|
|
$currency_code, |
78
|
|
|
['AUD', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HUF', 'NOK', 'NZD', 'PLN', 'SEK', 'USD'] |
79
|
|
|
) |
80
|
|
|
) { |
81
|
|
|
// Murder a kitten... |
82
|
|
|
throw new Exception('Unsupported currency code: ' . $currency_code); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$response = $this->getHttpClient()->get($this->getEndpoint() . '/balance/' . $currency_code, [ |
86
|
|
|
'auth' => [$this->user_id, $this->api_key] |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return new Balance(json_decode($response->getBody(), true)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* List all Products |
94
|
|
|
* |
95
|
|
|
* https://docs.wegift.io/#81ddc631-8525-4464-ae9b-85aaf99b031a |
96
|
|
|
* |
97
|
|
|
* @return Generator |
98
|
|
|
* @throws GuzzleException |
99
|
|
|
*/ |
100
|
|
|
public function listAllProducts(): Generator |
101
|
|
|
{ |
102
|
|
|
$response = $this->getHttpClient()->get($this->getEndpoint() . '/products', [ |
103
|
|
|
'auth' => [$this->user_id, $this->api_key] |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$json = json_decode($response->getBody(), true); |
107
|
|
|
foreach ($json['products'] as $product) { |
108
|
|
|
yield new Product($product); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function findAProduct($product_code): Product |
113
|
|
|
{ |
114
|
|
|
$response = $this->getHttpClient()->get($this->getEndpoint() . '/products/' . $product_code, [ |
115
|
|
|
'auth' => [$this->user_id, $this->api_key] |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
return new Product(json_decode($response->getBody(), true)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function createAnOrder(OrderRequest $order_request): OrderResponse |
122
|
|
|
{ |
123
|
|
|
$response = $this->getHttpClient()->post($this->getEndpoint() . '/order-digital-card', [ |
124
|
|
|
'headers' => [ |
125
|
|
|
'content-type' => 'application/json', |
126
|
|
|
], |
127
|
|
|
'auth' => [$this->user_id, $this->api_key], |
128
|
|
|
'body' => json_encode($order_request) |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
return new OrderResponse(json_decode($response->getBody(), true)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function findOrderDetails($order_id) |
135
|
|
|
{ |
136
|
|
|
$response = $this->getHttpClient()->get($this->getEndpoint() . '/order-details/' . $order_id, [ |
137
|
|
|
'auth' => [$this->user_id, $this->api_key] |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
return new OrderDetails(json_decode($response->getBody(), true)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|