BarionService.php
7 months ago
CurrencyService.php
7 months ago
SquareMiddlewareService.php
5 months ago
SquareService.php
7 months ago
StarterPaymentService.php
7 months ago
BarionService.php
200 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Services\Payment; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Payment\AbstractPaymentService; |
| 6 | use AmeliaBooking\Domain\Services\Payment\PaymentServiceInterface; |
| 7 | |
| 8 | class BarionService extends AbstractPaymentService implements PaymentServiceInterface |
| 9 | { |
| 10 | private function getPosKeyFromSettings() |
| 11 | { |
| 12 | return $this->settingsService->getCategorySettings('payments')['barion']['sandboxMode'] ? |
| 13 | $this->settingsService->getCategorySettings('payments')['barion']['sandboxPOSKey'] : |
| 14 | $this->settingsService->getCategorySettings('payments')['barion']['livePOSKey']; |
| 15 | } |
| 16 | |
| 17 | private function getUrl() |
| 18 | { |
| 19 | return $this->settingsService->getCategorySettings('payments')['barion']['sandboxMode'] ? |
| 20 | 'https://api.test.barion.com' : |
| 21 | 'https://api.barion.com'; |
| 22 | } |
| 23 | |
| 24 | private function getPayeeFromSettings() |
| 25 | { |
| 26 | return $this->settingsService->getCategorySettings('payments')['barion']['payeeEmail']; |
| 27 | } |
| 28 | |
| 29 | private function preparePayment($data) |
| 30 | { |
| 31 | $reservationData = $data['reservation']; |
| 32 | $paymentId = $data['paymentId']; |
| 33 | $serviceName = $reservationData->getBookable()->getName()->getValue(); |
| 34 | $serviceDescription = $reservationData->getBookable()->getDescription()->getValue() ? |
| 35 | $reservationData->getBookable()->getDescription()->getValue() : $serviceName; |
| 36 | $metaDescription = $data['info']['description'] ?: ''; |
| 37 | $payee = $this->getPayeeFromSettings(); |
| 38 | $currency = $this->settingsService->getCategorySettings('payments')['currency']; |
| 39 | $posKey = $this->getPosKeyFromSettings(); |
| 40 | $response = wp_remote_post($this->getUrl() . '/v2/payment/start', [ |
| 41 | 'headers' => [ |
| 42 | 'Content-Type' => 'application/json', |
| 43 | ], |
| 44 | 'body' => json_encode([ |
| 45 | "POSKey" => $posKey, |
| 46 | "PaymentType" => "Immediate", |
| 47 | "PaymentRequestId" => "Amelia - Transaction " . $paymentId, |
| 48 | "FundingSources" => ["All"], |
| 49 | "RedirectUrl" => $data['redirectUrl'], |
| 50 | "CallbackUrl" => $data['callbackUrl'], |
| 51 | "Currency" => $currency, |
| 52 | "Locale" => "en-US", |
| 53 | "GuestCheckout" => true, |
| 54 | "PaymentWindow" => "00:30:00", |
| 55 | "Transactions" => [ |
| 56 | [ |
| 57 | "POSTransactionId" => "Amelia - Transaction " . $paymentId, |
| 58 | "Payee" => $payee, |
| 59 | "Total" => $data['amount'], |
| 60 | "Comment" => $metaDescription, |
| 61 | "Items" => [ |
| 62 | [ |
| 63 | "Name" => $serviceName, |
| 64 | "Description" => $serviceDescription, |
| 65 | "Quantity" => 1, |
| 66 | "Unit" => "db", |
| 67 | "UnitPrice" => 1, |
| 68 | "ItemTotal" => 1, |
| 69 | "SKU" => "SM-01" |
| 70 | ] |
| 71 | ] |
| 72 | ] |
| 73 | ] |
| 74 | ]) |
| 75 | ]); |
| 76 | |
| 77 | $body = wp_remote_retrieve_body($response); |
| 78 | return json_decode($body, true); |
| 79 | } |
| 80 | |
| 81 | public function execute($data, &$transfers) |
| 82 | { |
| 83 | return $this->preparePayment($data); |
| 84 | } |
| 85 | |
| 86 | public function getPaymentLink($data) |
| 87 | { |
| 88 | $reservationData = $data['reservation']; |
| 89 | |
| 90 | $paymentId = $data['paymentId']; |
| 91 | $name = $data['name']; |
| 92 | $serviceDescription = !empty($reservationData['service']['description']) ? $reservationData['service']['description'] : ''; |
| 93 | $metaDescription = $data['info']['description'] ?: ''; |
| 94 | $payee = $this->getPayeeFromSettings(); |
| 95 | $currency = $this->settingsService->getCategorySettings('payments')['currency']; |
| 96 | $posKey = $this->getPosKeyFromSettings(); |
| 97 | $response = wp_remote_post($this->getUrl() . '/v2/payment/start', [ |
| 98 | 'headers' => [ |
| 99 | 'Content-Type' => 'application/json', |
| 100 | ], |
| 101 | 'body' => json_encode([ |
| 102 | "POSKey" => $posKey, |
| 103 | "PaymentType" => "Immediate", |
| 104 | "PaymentRequestId" => "Amelia - Transaction " . $paymentId, |
| 105 | "FundingSources" => ["All"], |
| 106 | "RedirectUrl" => $data['returnUrl'], |
| 107 | "Currency" => $currency, |
| 108 | "Locale" => "en-US", |
| 109 | "GuestCheckout" => true, |
| 110 | "PaymentWindow" => "00:30:00", |
| 111 | "Transactions" => [ |
| 112 | [ |
| 113 | "POSTransactionId" => "Amelia - Transaction " . $paymentId, |
| 114 | "Payee" => $payee, |
| 115 | "Total" => $data['amount'], |
| 116 | "Comment" => $metaDescription, |
| 117 | "Items" => [ |
| 118 | [ |
| 119 | "Name" => $name, |
| 120 | "Description" => $serviceDescription ?: $name, |
| 121 | "Quantity" => 1, |
| 122 | "Unit" => "db", |
| 123 | "UnitPrice" => 1, |
| 124 | "ItemTotal" => 1, |
| 125 | "SKU" => "SM-01" |
| 126 | ] |
| 127 | ] |
| 128 | ] |
| 129 | ] |
| 130 | ]) |
| 131 | ]); |
| 132 | |
| 133 | $body = wp_remote_retrieve_body($response); |
| 134 | return json_decode($body, true); |
| 135 | } |
| 136 | |
| 137 | public function refund($data) |
| 138 | { |
| 139 | $payment = $this->getPaymentState($data['id']); |
| 140 | $posKey = $this->getPosKeyFromSettings(); |
| 141 | |
| 142 | $transactions = []; |
| 143 | |
| 144 | foreach ($payment['Transactions'] as $transaction) { |
| 145 | if ($transaction['TransactionType'] === 'CardPayment') { // refund only customer payment |
| 146 | $transactions[] = [ |
| 147 | "TransactionId" => $transaction['TransactionId'], |
| 148 | "AmountToRefund" => $transaction['Total'], // or a partial amount |
| 149 | ]; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $response = wp_remote_post($this->getUrl() . '/v2/Payment/Refund', [ |
| 154 | 'headers' => [ |
| 155 | 'Content-Type' => 'application/json', |
| 156 | 'Accept' => 'application/json', |
| 157 | 'x-pos-key' => $posKey |
| 158 | ], |
| 159 | 'body' => json_encode([ |
| 160 | "PaymentId" => $payment['PaymentId'], |
| 161 | "TransactionsToRefund" => $transactions, |
| 162 | ]) |
| 163 | ]); |
| 164 | |
| 165 | return json_decode(wp_remote_retrieve_body($response), true); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | public function getTransactionAmount($id, $transfers) |
| 170 | { |
| 171 | $payment = $this->getPaymentState($id); |
| 172 | |
| 173 | return $payment ? $payment['Total'] : null; |
| 174 | } |
| 175 | |
| 176 | public function getErrorMessage($errors) |
| 177 | { |
| 178 | $errors = array_map( |
| 179 | function ($error) { |
| 180 | return $error['Description']; |
| 181 | }, |
| 182 | $errors |
| 183 | ); |
| 184 | return implode('; ', $errors); |
| 185 | } |
| 186 | |
| 187 | public function getPaymentState($paymentId) |
| 188 | { |
| 189 | $posKey = $this->getPosKeyFromSettings(); |
| 190 | $response = wp_remote_get($this->getUrl() . "/v4/Payment/{$paymentId}/PaymentState", [ |
| 191 | 'headers' => [ |
| 192 | 'Accept' => 'application/json', |
| 193 | 'x-pos-key' => $posKey |
| 194 | ] |
| 195 | ]); |
| 196 | |
| 197 | return json_decode(wp_remote_retrieve_body($response), true); |
| 198 | } |
| 199 | } |
| 200 |