@@ -13,126 +13,126 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | final class GivePayGatewayClient { |
| 15 | 15 | |
| 16 | - public $token_endpoint; |
|
| 17 | - |
|
| 18 | - public $gateway_url; |
|
| 19 | - |
|
| 20 | - private $client_secret; |
|
| 21 | - |
|
| 22 | - private $client_id; |
|
| 23 | - |
|
| 24 | - private $logger; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * FRP_Gateway constructor. |
|
| 28 | - * |
|
| 29 | - * @param string $client_id |
|
| 30 | - * @param string $client_secret |
|
| 31 | - * @param string $token_endpoint |
|
| 32 | - * @param string $gateway_url |
|
| 33 | - * @param LoggerInterface $logger |
|
| 34 | - */ |
|
| 35 | - public function __construct( |
|
| 36 | - $client_id, |
|
| 37 | - $client_secret, |
|
| 38 | - $token_endpoint = 'https://portal.flatratepay.com/connect/token', |
|
| 39 | - $gateway_url = 'https://gateway.givepaycommerce.com', |
|
| 16 | + public $token_endpoint; |
|
| 17 | + |
|
| 18 | + public $gateway_url; |
|
| 19 | + |
|
| 20 | + private $client_secret; |
|
| 21 | + |
|
| 22 | + private $client_id; |
|
| 23 | + |
|
| 24 | + private $logger; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * FRP_Gateway constructor. |
|
| 28 | + * |
|
| 29 | + * @param string $client_id |
|
| 30 | + * @param string $client_secret |
|
| 31 | + * @param string $token_endpoint |
|
| 32 | + * @param string $gateway_url |
|
| 33 | + * @param LoggerInterface $logger |
|
| 34 | + */ |
|
| 35 | + public function __construct( |
|
| 36 | + $client_id, |
|
| 37 | + $client_secret, |
|
| 38 | + $token_endpoint = 'https://portal.flatratepay.com/connect/token', |
|
| 39 | + $gateway_url = 'https://gateway.givepaycommerce.com', |
|
| 40 | 40 | $logger = null |
| 41 | - ) { |
|
| 42 | - $this->token_endpoint = $token_endpoint; |
|
| 43 | - $this->gateway_url = $gateway_url; |
|
| 41 | + ) { |
|
| 42 | + $this->token_endpoint = $token_endpoint; |
|
| 43 | + $this->gateway_url = $gateway_url; |
|
| 44 | 44 | |
| 45 | - $this->client_secret = $client_secret; |
|
| 46 | - $this->client_id = $client_id; |
|
| 45 | + $this->client_secret = $client_secret; |
|
| 46 | + $this->client_id = $client_id; |
|
| 47 | 47 | |
| 48 | - if ($logger == null) { |
|
| 49 | - $this->logger = new NullLogger(); |
|
| 48 | + if ($logger == null) { |
|
| 49 | + $this->logger = new NullLogger(); |
|
| 50 | 50 | } else { |
| 51 | - $this->logger = $logger; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param string $merchant_id The merchant ID for the website |
|
| 57 | - * @param string $terminal_id The terminal ID for the website |
|
| 58 | - * @param Transactions\Sale $sale The transaction information |
|
| 59 | - * |
|
| 60 | - * @throws Exception |
|
| 61 | - * @return TransactionResult |
|
| 62 | - */ |
|
| 63 | - public function chargeAmount( $merchant_id, $terminal_id, $sale ) { |
|
| 64 | - if ( null == $sale ) { |
|
| 65 | - throw new \Exception( '$sale is null' ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 69 | - if ( null == $access_token ) { |
|
| 70 | - throw new \Exception( 'Could not authorize with gateway.' ); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $this->makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $transaction_id |
|
| 78 | - * @param string $merchant_id |
|
| 79 | - * @param string $terminal_id |
|
| 80 | - * |
|
| 81 | - * @throws Exception |
|
| 82 | - * @return TransactionResult |
|
| 83 | - */ |
|
| 84 | - public function voidTransaction( $transaction_id, $merchant_id, $terminal_id ) { |
|
| 85 | - if ( null == $transaction_id ) { |
|
| 86 | - throw new Exception( 'Transaction ID is null' ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 90 | - if ( null == $access_token ) { |
|
| 91 | - throw new Exception( 'Could not authorize with gateway.' ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return $this->makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Stores the card and gets a token from the gateway |
|
| 99 | - * |
|
| 100 | - * @param string $merchant_id |
|
| 101 | - * @param string $terminal_id |
|
| 102 | - * @param Card $card |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - * @throws Exception |
|
| 106 | - */ |
|
| 107 | - public function storeCard( $merchant_id, $terminal_id, $card ) { |
|
| 108 | - if ( null == $card ) { |
|
| 109 | - throw new Exception( 'Card is null' ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 113 | - if ( null == $access_token ) { |
|
| 114 | - throw new Exception( 'Could not store card with gateway.' ); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return $this->makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param string $access_token |
|
| 122 | - * @param string $merchant_id |
|
| 123 | - * @param string $terminal_id |
|
| 124 | - * @param Transactions\Sale $sale |
|
| 125 | - * |
|
| 126 | - * @return TransactionResult |
|
| 127 | - */ |
|
| 128 | - private function makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ) { |
|
| 129 | - $sale_request = $sale->serialize($merchant_id, $terminal_id); |
|
| 130 | - $body = json_encode( $sale_request ); |
|
| 131 | - |
|
| 132 | - $this->logger->info("Starting transaction for $" . $sale->getTotal()); |
|
| 133 | - |
|
| 134 | - $request = new Request($this->gateway_url . 'api/v1/transactions/sale'); |
|
| 135 | - $request->getOptions() |
|
| 51 | + $this->logger = $logger; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param string $merchant_id The merchant ID for the website |
|
| 57 | + * @param string $terminal_id The terminal ID for the website |
|
| 58 | + * @param Transactions\Sale $sale The transaction information |
|
| 59 | + * |
|
| 60 | + * @throws Exception |
|
| 61 | + * @return TransactionResult |
|
| 62 | + */ |
|
| 63 | + public function chargeAmount( $merchant_id, $terminal_id, $sale ) { |
|
| 64 | + if ( null == $sale ) { |
|
| 65 | + throw new \Exception( '$sale is null' ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 69 | + if ( null == $access_token ) { |
|
| 70 | + throw new \Exception( 'Could not authorize with gateway.' ); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $this->makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $transaction_id |
|
| 78 | + * @param string $merchant_id |
|
| 79 | + * @param string $terminal_id |
|
| 80 | + * |
|
| 81 | + * @throws Exception |
|
| 82 | + * @return TransactionResult |
|
| 83 | + */ |
|
| 84 | + public function voidTransaction( $transaction_id, $merchant_id, $terminal_id ) { |
|
| 85 | + if ( null == $transaction_id ) { |
|
| 86 | + throw new Exception( 'Transaction ID is null' ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 90 | + if ( null == $access_token ) { |
|
| 91 | + throw new Exception( 'Could not authorize with gateway.' ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return $this->makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Stores the card and gets a token from the gateway |
|
| 99 | + * |
|
| 100 | + * @param string $merchant_id |
|
| 101 | + * @param string $terminal_id |
|
| 102 | + * @param Card $card |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + * @throws Exception |
|
| 106 | + */ |
|
| 107 | + public function storeCard( $merchant_id, $terminal_id, $card ) { |
|
| 108 | + if ( null == $card ) { |
|
| 109 | + throw new Exception( 'Card is null' ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $access_token = $this->getAccessToken( $this->client_id, $this->client_secret, $this->token_endpoint ); |
|
| 113 | + if ( null == $access_token ) { |
|
| 114 | + throw new Exception( 'Could not store card with gateway.' ); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return $this->makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param string $access_token |
|
| 122 | + * @param string $merchant_id |
|
| 123 | + * @param string $terminal_id |
|
| 124 | + * @param Transactions\Sale $sale |
|
| 125 | + * |
|
| 126 | + * @return TransactionResult |
|
| 127 | + */ |
|
| 128 | + private function makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ) { |
|
| 129 | + $sale_request = $sale->serialize($merchant_id, $terminal_id); |
|
| 130 | + $body = json_encode( $sale_request ); |
|
| 131 | + |
|
| 132 | + $this->logger->info("Starting transaction for $" . $sale->getTotal()); |
|
| 133 | + |
|
| 134 | + $request = new Request($this->gateway_url . 'api/v1/transactions/sale'); |
|
| 135 | + $request->getOptions() |
|
| 136 | 136 | ->set(CURLOPT_RETURNTRANSFER, true) |
| 137 | 137 | ->set(CURLOPT_POST, true) |
| 138 | 138 | ->set(CURLOPT_POSTFIELDS, $body) |
@@ -146,40 +146,40 @@ discard block |
||
| 146 | 146 | |
| 147 | 147 | $this->logger->debug( "Transaction completed" ); |
| 148 | 148 | |
| 149 | - $sale_response = json_decode( $response->getContent() ); |
|
| 149 | + $sale_response = json_decode( $response->getContent() ); |
|
| 150 | 150 | |
| 151 | - if ( $sale_response->success ) { |
|
| 152 | - $transaction_id = $sale_response->result->transaction_id; |
|
| 151 | + if ( $sale_response->success ) { |
|
| 152 | + $transaction_id = $sale_response->result->transaction_id; |
|
| 153 | 153 | |
| 154 | 154 | $this->logger->info( 'Payment completed. Transaction ID: ' . $transaction_id ); |
| 155 | 155 | |
| 156 | - return new TransactionResult( true, $transaction_id ); |
|
| 157 | - } else { |
|
| 158 | - $error_message = $sale_response->error->message; |
|
| 159 | - $code = $sale_response->error->code; |
|
| 156 | + return new TransactionResult( true, $transaction_id ); |
|
| 157 | + } else { |
|
| 158 | + $error_message = $sale_response->error->message; |
|
| 159 | + $code = $sale_response->error->code; |
|
| 160 | 160 | |
| 161 | 161 | $this->logger->debug( "Sale response: " . var_export( $sale_response, true ) ); |
| 162 | 162 | $this->logger->error( "Payment failed." ); |
| 163 | 163 | |
| 164 | - return new TransactionResult( false, null, $error_message, $code ); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Makes a VOID request |
|
| 170 | - * |
|
| 171 | - * @param string $access_token |
|
| 172 | - * @param string $transaction_id |
|
| 173 | - * @param string $merchant_id |
|
| 174 | - * @param string $terminal_id |
|
| 175 | - * |
|
| 176 | - * @return TransactionResult |
|
| 177 | - */ |
|
| 178 | - private function makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ) { |
|
| 179 | - $void = new V0id(TerminalType::$ECommerce, $transaction_id); |
|
| 180 | - $void_request = $void->serialize($merchant_id, $terminal_id); |
|
| 181 | - |
|
| 182 | - $body = json_encode( $void_request ); |
|
| 164 | + return new TransactionResult( false, null, $error_message, $code ); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Makes a VOID request |
|
| 170 | + * |
|
| 171 | + * @param string $access_token |
|
| 172 | + * @param string $transaction_id |
|
| 173 | + * @param string $merchant_id |
|
| 174 | + * @param string $terminal_id |
|
| 175 | + * |
|
| 176 | + * @return TransactionResult |
|
| 177 | + */ |
|
| 178 | + private function makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ) { |
|
| 179 | + $void = new V0id(TerminalType::$ECommerce, $transaction_id); |
|
| 180 | + $void_request = $void->serialize($merchant_id, $terminal_id); |
|
| 181 | + |
|
| 182 | + $body = json_encode( $void_request ); |
|
| 183 | 183 | |
| 184 | 184 | $this->logger->info( "Starting void transaction for transaction# " . $transaction_id ); |
| 185 | 185 | |
@@ -198,39 +198,39 @@ discard block |
||
| 198 | 198 | |
| 199 | 199 | $this->logger->debug( "Transaction completed" ); |
| 200 | 200 | |
| 201 | - $void_response = json_decode( $response->getContent() ); |
|
| 201 | + $void_response = json_decode( $response->getContent() ); |
|
| 202 | 202 | |
| 203 | - if ( $void_response->success ) { |
|
| 204 | - $transaction_id = $void_response->result->transaction_id; |
|
| 203 | + if ( $void_response->success ) { |
|
| 204 | + $transaction_id = $void_response->result->transaction_id; |
|
| 205 | 205 | |
| 206 | 206 | $this->logger->info( 'Void completed. Transaction ID: ' . $transaction_id ); |
| 207 | 207 | |
| 208 | - return new TransactionResult( true, $transaction_id ); |
|
| 209 | - } else { |
|
| 210 | - $error_message = $void_response->error->message; |
|
| 211 | - $code = $void_response->error->code; |
|
| 208 | + return new TransactionResult( true, $transaction_id ); |
|
| 209 | + } else { |
|
| 210 | + $error_message = $void_response->error->message; |
|
| 211 | + $code = $void_response->error->code; |
|
| 212 | 212 | |
| 213 | 213 | $this->logger->debug( "Void response: " . var_export( $void_response, true ) ); |
| 214 | 214 | $this->logger->error( "Void failed." ); |
| 215 | 215 | |
| 216 | - return new TransactionResult( false, null, $error_message, $code ); |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * stores a card in the gateway |
|
| 222 | - * |
|
| 223 | - * @param string $access_token |
|
| 224 | - * @param string $terminal_id |
|
| 225 | - * @param string $merchant_id |
|
| 226 | - * @param mixed $card |
|
| 227 | - * |
|
| 228 | - * @return string |
|
| 229 | - */ |
|
| 230 | - private function makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ) { |
|
| 231 | - $token_request = new TokenRequest($card, TerminalType::$ECommerce); |
|
| 216 | + return new TransactionResult( false, null, $error_message, $code ); |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * stores a card in the gateway |
|
| 222 | + * |
|
| 223 | + * @param string $access_token |
|
| 224 | + * @param string $terminal_id |
|
| 225 | + * @param string $merchant_id |
|
| 226 | + * @param mixed $card |
|
| 227 | + * |
|
| 228 | + * @return string |
|
| 229 | + */ |
|
| 230 | + private function makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ) { |
|
| 231 | + $token_request = new TokenRequest($card, TerminalType::$ECommerce); |
|
| 232 | 232 | $token_request_string = $token_request->serialize($merchant_id, $terminal_id); |
| 233 | - $body = json_encode( $token_request_string ); |
|
| 233 | + $body = json_encode( $token_request_string ); |
|
| 234 | 234 | |
| 235 | 235 | $this->logger->info( "Starting request for tokenization" ); |
| 236 | 236 | |
@@ -251,36 +251,36 @@ discard block |
||
| 251 | 251 | |
| 252 | 252 | $token_response = json_decode( $response->getContent() ); |
| 253 | 253 | |
| 254 | - if ( $token_response->success ) { |
|
| 255 | - $transaction_id = $token_response->result->transaction_id; |
|
| 254 | + if ( $token_response->success ) { |
|
| 255 | + $transaction_id = $token_response->result->transaction_id; |
|
| 256 | 256 | |
| 257 | 257 | $this->logger->info( 'Tokenization completed. Transaction ID: ' . $transaction_id ); |
| 258 | 258 | |
| 259 | - return $token_response->result->token; |
|
| 260 | - } else { |
|
| 259 | + return $token_response->result->token; |
|
| 260 | + } else { |
|
| 261 | 261 | $this->logger->debug( "Tokenization response: " . var_export( $token_response, true ) ); |
| 262 | 262 | $this->logger->error( "Tokenization failed." ); |
| 263 | 263 | |
| 264 | - return ''; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Gets an access token from the auth server |
|
| 270 | - * |
|
| 271 | - * @param string $client_id the client ID |
|
| 272 | - * @param string $client_secret the client secret |
|
| 273 | - * @param string $token_url the token endpoint |
|
| 274 | - * |
|
| 275 | - * @return string |
|
| 276 | - */ |
|
| 277 | - private function getAccessToken( $client_id, $client_secret, $token_url ) { |
|
| 278 | - $token_data = array( |
|
| 279 | - 'client_id' => $client_id, |
|
| 280 | - 'grant_type' => 'client_credentials', |
|
| 281 | - 'client_secret' => $client_secret, |
|
| 282 | - 'scope' => 'authorize:transactions capture:transactions sale:transactions refund:transactions void:transactions tokenize:transactions' |
|
| 283 | - ); |
|
| 264 | + return ''; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Gets an access token from the auth server |
|
| 270 | + * |
|
| 271 | + * @param string $client_id the client ID |
|
| 272 | + * @param string $client_secret the client secret |
|
| 273 | + * @param string $token_url the token endpoint |
|
| 274 | + * |
|
| 275 | + * @return string |
|
| 276 | + */ |
|
| 277 | + private function getAccessToken( $client_id, $client_secret, $token_url ) { |
|
| 278 | + $token_data = array( |
|
| 279 | + 'client_id' => $client_id, |
|
| 280 | + 'grant_type' => 'client_credentials', |
|
| 281 | + 'client_secret' => $client_secret, |
|
| 282 | + 'scope' => 'authorize:transactions capture:transactions sale:transactions refund:transactions void:transactions tokenize:transactions' |
|
| 283 | + ); |
|
| 284 | 284 | |
| 285 | 285 | $request = new Request($token_url); |
| 286 | 286 | $request->getOptions() |
@@ -293,17 +293,17 @@ discard block |
||
| 293 | 293 | )); |
| 294 | 294 | $response = $request->send(); |
| 295 | 295 | |
| 296 | - if ( $response->hasError() ) { |
|
| 296 | + if ( $response->hasError() ) { |
|
| 297 | 297 | $this->logger->debug( 'Token response status code was ' . $response->getError()->getCode() ); |
| 298 | 298 | $this->logger->debug( "Token request ended in failure: " . var_export( $response, true ) ); |
| 299 | 299 | $this->logger->error( "Gateway authorization failed. Check credentials." ); |
| 300 | 300 | |
| 301 | - return null; |
|
| 302 | - } |
|
| 301 | + return null; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | 304 | $this->logger->debug( "Token request was a success: " . $response->getContent()); |
| 305 | - $token = json_decode( $response->getContent() ); |
|
| 305 | + $token = json_decode( $response->getContent() ); |
|
| 306 | 306 | |
| 307 | - return $token->access_token; |
|
| 308 | - } |
|
| 307 | + return $token->access_token; |
|
| 308 | + } |
|
| 309 | 309 | } |
| 310 | 310 | \ No newline at end of file |