|
5 | 5 | use GuzzleHttp\Client;
|
6 | 6 | use Sykez\Betterpay\Exceptions\BetterpayException;
|
7 | 7 | use GuzzleHttp\Exception\ClientException;
|
8 |
| -use Sykez\Betterpay\Hashing; |
| 8 | +use Sykez\Betterpay\HashVerification; |
| 9 | +use Sykez\Betterpay\Tokenization; |
9 | 10 |
|
10 | 11 | class Betterpay
|
11 | 12 | {
|
| 13 | + use HashVerification, Tokenization; |
| 14 | + |
| 15 | + /** |
| 16 | + * API endpoints |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $production_url = 'https://www.betterpay.me/merchant/api/'; |
| 20 | + protected $sandbox_url = 'https://www.demo.betterpay.me/merchant/api/'; |
| 21 | + |
12 | 22 | protected $client;
|
| 23 | + protected $api_url; |
13 | 24 | protected $api_key;
|
14 | 25 | protected $merchant_id;
|
15 | 26 | protected $callback_url;
|
16 | 27 | protected $success_url;
|
17 | 28 | protected $fail_url;
|
18 |
| - |
19 |
| - public function __construct(?string $api_key, ?string $merchant_id, string $api_url, ?string $callback_url, ?string $success_url, ?string $fail_url) |
| 29 | + protected $sandbox; |
| 30 | + protected $is_3ds; |
| 31 | + |
| 32 | + /** |
| 33 | + * __construct |
| 34 | + * |
| 35 | + * @param mixed $api_key |
| 36 | + * @param mixed $merchant_id |
| 37 | + * @param mixed $callback_url |
| 38 | + * @param mixed $success_url |
| 39 | + * @param mixed $fail_url |
| 40 | + * @param mixed $sandbox |
| 41 | + * @param mixed $is_3ds |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function __construct(?string $api_key, ?int $merchant_id, ?string $callback_url, ?string $success_url, ?string $fail_url, bool $sandbox = false, bool $is_3ds = true) |
20 | 45 | {
|
21 |
| - if (!$api_key || !$merchant_id || !$api_url || !$callback_url || !$success_url || !$fail_url) { |
| 46 | + if (!$api_key || !$merchant_id || !$callback_url || !$success_url || !$fail_url) { |
22 | 47 | throw BetterpayException::configException();
|
23 | 48 | }
|
24 | 49 |
|
| 50 | + if ($sandbox && $is_3ds) { |
| 51 | + throw BetterpayException::sandbox3dsException(); |
| 52 | + } |
| 53 | + |
25 | 54 | $this->api_key = $api_key;
|
26 | 55 | $this->merchant_id = $merchant_id;
|
27 |
| - $this->api_url = $api_url; |
28 | 56 | $this->callback_url = $callback_url;
|
29 | 57 | $this->success_url = $success_url;
|
30 | 58 | $this->fail_url = $fail_url;
|
| 59 | + $this->sandbox = $sandbox; |
| 60 | + $this->is_3ds = $is_3ds; |
| 61 | + $this->api_url = $this->sandbox ? $this->sandbox_url : $this->production_url; |
31 | 62 | }
|
32 |
| - |
33 |
| - public function createTokenizationUrl(string $reference_id, int $skip_receipt = 0, bool $_3ds = true) |
| 63 | + |
| 64 | + /** |
| 65 | + * Set 3D Secure mode. Not supported in sandbox mode. |
| 66 | + * |
| 67 | + * @param mixed $is_3ds |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function set3DS(bool $is_3ds) |
34 | 71 | {
|
35 |
| - $hash = Hashing::reference($this->api_key, $this->merchant_id, $reference_id); |
36 |
| - $payload = [ |
37 |
| - 'merchant_id' => $this->merchant_id, |
38 |
| - 'reference_id' => $reference_id, |
39 |
| - 'callback_url_be' => $this->callback_url, |
40 |
| - 'callback_url_fe_succ' => $this->success_url, |
41 |
| - 'callback_url_fe_fail' => $this->fail_url, |
42 |
| - 'skip_receipt' => $skip_receipt, |
43 |
| - 'hash' => $hash, |
44 |
| - ]; |
45 |
| - |
46 |
| - $response = $this->http_request('cards/token/v1/' . ($_3ds ? '3ds/' : '') . 'create', $payload); |
47 |
| - |
48 |
| - return $response; |
| 72 | + $this->is_3ds = $is_3ds; |
49 | 73 | }
|
50 |
| - |
51 |
| - public function charge(string $token, string $invoice, int $amount, int $sandbox_charge_status = 0) |
| 74 | + |
| 75 | + /** |
| 76 | + * Set API URL manually, in case you need to. |
| 77 | + * |
| 78 | + * @param mixed $api_url |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function setApiUrl(string $api_url) |
52 | 82 | {
|
53 |
| - $hash = Hashing::tokenizationCharge($this->api_key, $this->merchant_id, $token, $invoice, $amount); |
54 |
| - $payload = [ |
55 |
| - 'merchant_id' => $this->merchant_id, |
56 |
| - 'invoice' => $invoice, |
57 |
| - 'amount' => $amount, |
58 |
| - 'sandbox_charge_status' => $sandbox_charge_status, |
59 |
| - 'hash' => $hash, |
60 |
| - ]; |
61 |
| - $response = $this->http_request('cards/token/v1/pay/' . $token, $payload); |
62 |
| - return $response; |
| 83 | + $this->api_url = $api_url; |
63 | 84 | }
|
64 |
| - |
65 |
| - public function delete_card(string $token) |
| 85 | + |
| 86 | + /** |
| 87 | + * Hash (API Key + Merchant ID + ...) |
| 88 | + * |
| 89 | + * @param mixed $args |
| 90 | + * @return string |
| 91 | + */ |
| 92 | + public function makeHash(string ...$args): string |
66 | 93 | {
|
67 |
| - $hash = Hashing::reference($this->api_key, $this->merchant_id, $token); |
68 |
| - $payload = [ |
69 |
| - 'merchant_id' => $this->merchant_id, |
70 |
| - 'hash' => $hash, |
71 |
| - ]; |
72 |
| - $response = $this->http_request('cards/token/v1/delete/' . $token, $payload); |
73 |
| - return $response; |
| 94 | + return md5(implode('|', [$this->api_key, $this->merchant_id, ...$args])); |
74 | 95 | }
|
75 |
| - |
76 |
| - public function http_request(string $url, array $payload) |
| 96 | + |
| 97 | + /** |
| 98 | + * Hash2 - reverse of hash() (Merchant ID + API Key + ...) |
| 99 | + * |
| 100 | + * @param mixed $args |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + public function makeHash2(string ...$args): string |
| 104 | + { |
| 105 | + return md5(implode('|', [$this->merchant_id, $this->api_key, ...$args])); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Filter payload; remove null or '' array |
| 110 | + * |
| 111 | + * @param mixed $payload |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + public function filterPayload(array $payload): array |
| 115 | + { |
| 116 | + return array_filter($payload, fn ($v) => $v !== null && $v !== ''); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * httpRequest |
| 121 | + * |
| 122 | + * @param mixed $url |
| 123 | + * @param mixed $payload |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + public function httpRequest(string $url, array $payload): array |
77 | 127 | {
|
78 | 128 | $client = new Client();
|
79 | 129 | try {
|
|
0 commit comments