CurrencyService.php
2 years ago
SquareMiddlewareService.php
1 year ago
SquareService.php
1 year ago
StarterPaymentService.php
2 years ago
SquareMiddlewareService.php
160 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Infrastructure\Services\Payment; |
| 8 | |
| 9 | /** |
| 10 | * Class SquareMiddlewareService |
| 11 | */ |
| 12 | class SquareMiddlewareService |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * |
| 17 | * @return boolean |
| 18 | * |
| 19 | * @throws \Exception |
| 20 | */ |
| 21 | public function decrypt($savedAccessToken) |
| 22 | { |
| 23 | $ch = curl_init(AMELIA_MIDDLEWARE_API_URL . 'square/decrypt'); |
| 24 | |
| 25 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 26 | |
| 27 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 28 | |
| 29 | curl_setopt( |
| 30 | $ch, |
| 31 | CURLOPT_POSTFIELDS, |
| 32 | [ |
| 33 | 'access_token' => $savedAccessToken['access_token'], |
| 34 | 'refresh_token' => $savedAccessToken['refresh_token'] |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | $response = curl_exec($ch); |
| 39 | |
| 40 | if ($response && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) { |
| 41 | $response = json_decode($response, true); |
| 42 | set_transient('amelia_square_access_token', ['access_token' => $response['result']['access_token'], 'refresh_token' => $response['result']['refresh_token']], 604800); |
| 43 | } else { |
| 44 | $response = null; |
| 45 | } |
| 46 | |
| 47 | curl_close($ch); |
| 48 | |
| 49 | return $response['result']; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * |
| 54 | * @return array |
| 55 | * |
| 56 | * @throws \Exception |
| 57 | */ |
| 58 | public function getAccessToken($savedAccessToken) |
| 59 | { |
| 60 | $accessToken = get_transient('amelia_square_access_token'); |
| 61 | if ($accessToken === false) { |
| 62 | $accessToken = $this->decrypt($savedAccessToken); |
| 63 | } |
| 64 | return $accessToken; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * |
| 69 | * @return boolean |
| 70 | * |
| 71 | * @throws \Exception |
| 72 | */ |
| 73 | public function disconnectAccount($savedAccessToken, $testMode) |
| 74 | { |
| 75 | $accessToken = $this->getAccessToken($savedAccessToken); |
| 76 | |
| 77 | $ch = curl_init(AMELIA_MIDDLEWARE_API_URL . 'square/revoke?testMode=' . $testMode); |
| 78 | |
| 79 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 80 | |
| 81 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 82 | |
| 83 | curl_setopt( |
| 84 | $ch, |
| 85 | CURLOPT_POSTFIELDS, |
| 86 | [ |
| 87 | 'access_token' => $accessToken['access_token'], |
| 88 | ] |
| 89 | ); |
| 90 | |
| 91 | $response = curl_exec($ch); |
| 92 | |
| 93 | curl_close($ch); |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * |
| 101 | * @param string $authCode |
| 102 | * @param string $state |
| 103 | * |
| 104 | * @return boolean |
| 105 | * |
| 106 | * @throws \Exception |
| 107 | */ |
| 108 | public function refreshAccessToken($savedAccessToken, $testMode) |
| 109 | { |
| 110 | $accessToken = $this->getAccessToken($savedAccessToken); |
| 111 | |
| 112 | $ch = curl_init(AMELIA_MIDDLEWARE_API_URL . 'square/refresh?testMode=' . $testMode); |
| 113 | |
| 114 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 115 | |
| 116 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 117 | |
| 118 | curl_setopt( |
| 119 | $ch, |
| 120 | CURLOPT_POSTFIELDS, |
| 121 | [ |
| 122 | 'refresh_token' => $accessToken['refresh_token'], |
| 123 | ] |
| 124 | ); |
| 125 | |
| 126 | $response = curl_exec($ch); |
| 127 | |
| 128 | if ($response && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) { |
| 129 | $response = json_decode($response, true); |
| 130 | } else { |
| 131 | $response = null; |
| 132 | } |
| 133 | |
| 134 | curl_close($ch); |
| 135 | |
| 136 | return $response; |
| 137 | } |
| 138 | |
| 139 | public function getAuthUrl($testMode) |
| 140 | { |
| 141 | $ch = curl_init( |
| 142 | AMELIA_MIDDLEWARE_API_URL . 'square/authorization/url?testMode=' . $testMode . '&admin_ajax_url=' . urlencode(admin_url('admin-ajax.php', '')) |
| 143 | ); |
| 144 | |
| 145 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 146 | |
| 147 | $response = curl_exec($ch); |
| 148 | |
| 149 | $url = null; |
| 150 | if ($response) { |
| 151 | $response = json_decode($response, true); |
| 152 | $url = $response['result']; |
| 153 | } |
| 154 | |
| 155 | curl_close($ch); |
| 156 | |
| 157 | return $url; |
| 158 | } |
| 159 | } |
| 160 |