@@ -76,7 +76,7 @@ |
||
| 76 | 76 | 'terminal' => array( |
| 77 | 77 | 'tid' => $terminal_id, |
| 78 | 78 | 'terminal_type' => $this->terminal_type |
| 79 | - ), |
|
| 79 | + ), |
|
| 80 | 80 | 'amount' => array( |
| 81 | 81 | 'base_amount' => $this->getTotal() |
| 82 | 82 | ), |
@@ -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,45 +293,45 @@ 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() ); |
|
| 306 | - |
|
| 307 | - return $token->access_token; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Generates a tokenization request |
|
| 312 | - * |
|
| 313 | - * @param string $merchant_id |
|
| 314 | - * @param string $terminal_id |
|
| 315 | - * @param array $card |
|
| 316 | - * |
|
| 317 | - * @return array |
|
| 318 | - */ |
|
| 319 | - private function generateTokenizationRequest( $merchant_id, $terminal_id, $card ) { |
|
| 320 | - $token_request = array( |
|
| 321 | - 'mid' => $merchant_id, |
|
| 322 | - 'terminal' => array( |
|
| 323 | - 'tid' => $terminal_id, |
|
| 324 | - 'terminal_type' => 'com.givepay.terminal-types.ecommerce' |
|
| 325 | - ), |
|
| 326 | - 'card' => array( |
|
| 327 | - 'card_number' => $card['card_number'], |
|
| 328 | - 'card_present' => false, |
|
| 329 | - 'expiration_month' => $card['expiration_month'], |
|
| 330 | - 'expiration_year' => $card['expiration_year'], |
|
| 331 | - 'cvv' => $card['cvv'] |
|
| 332 | - ) |
|
| 333 | - ); |
|
| 334 | - |
|
| 335 | - return $token_request; |
|
| 336 | - } |
|
| 305 | + $token = json_decode( $response->getContent() ); |
|
| 306 | + |
|
| 307 | + return $token->access_token; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Generates a tokenization request |
|
| 312 | + * |
|
| 313 | + * @param string $merchant_id |
|
| 314 | + * @param string $terminal_id |
|
| 315 | + * @param array $card |
|
| 316 | + * |
|
| 317 | + * @return array |
|
| 318 | + */ |
|
| 319 | + private function generateTokenizationRequest( $merchant_id, $terminal_id, $card ) { |
|
| 320 | + $token_request = array( |
|
| 321 | + 'mid' => $merchant_id, |
|
| 322 | + 'terminal' => array( |
|
| 323 | + 'tid' => $terminal_id, |
|
| 324 | + 'terminal_type' => 'com.givepay.terminal-types.ecommerce' |
|
| 325 | + ), |
|
| 326 | + 'card' => array( |
|
| 327 | + 'card_number' => $card['card_number'], |
|
| 328 | + 'card_present' => false, |
|
| 329 | + 'expiration_month' => $card['expiration_month'], |
|
| 330 | + 'expiration_year' => $card['expiration_year'], |
|
| 331 | + 'cvv' => $card['cvv'] |
|
| 332 | + ) |
|
| 333 | + ); |
|
| 334 | + |
|
| 335 | + return $token_request; |
|
| 336 | + } |
|
| 337 | 337 | } |
| 338 | 338 | \ No newline at end of file |
@@ -60,17 +60,17 @@ discard block |
||
| 60 | 60 | * @throws Exception |
| 61 | 61 | * @return TransactionResult |
| 62 | 62 | */ |
| 63 | - public function chargeAmount( $merchant_id, $terminal_id, $sale ) { |
|
| 64 | - if ( null == $sale ) { |
|
| 65 | - throw new \Exception( '$sale is null' ); |
|
| 63 | + public function chargeAmount($merchant_id, $terminal_id, $sale) { |
|
| 64 | + if (null == $sale) { |
|
| 65 | + throw new \Exception('$sale is null'); |
|
| 66 | 66 | } |
| 67 | 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.' ); |
|
| 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 | 71 | } |
| 72 | 72 | |
| 73 | - return $this->makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ); |
|
| 73 | + return $this->makeSaleRequest($access_token, $merchant_id, $terminal_id, $sale); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -81,17 +81,17 @@ discard block |
||
| 81 | 81 | * @throws Exception |
| 82 | 82 | * @return TransactionResult |
| 83 | 83 | */ |
| 84 | - public function voidTransaction( $transaction_id, $merchant_id, $terminal_id ) { |
|
| 85 | - if ( null == $transaction_id ) { |
|
| 86 | - throw new Exception( 'Transaction ID is null' ); |
|
| 84 | + public function voidTransaction($transaction_id, $merchant_id, $terminal_id) { |
|
| 85 | + if (null == $transaction_id) { |
|
| 86 | + throw new Exception('Transaction ID is null'); |
|
| 87 | 87 | } |
| 88 | 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.' ); |
|
| 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 | 92 | } |
| 93 | 93 | |
| 94 | - return $this->makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ); |
|
| 94 | + return $this->makeVoidRequest($access_token, $transaction_id, $merchant_id, $terminal_id); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -104,17 +104,17 @@ discard block |
||
| 104 | 104 | * @return string |
| 105 | 105 | * @throws Exception |
| 106 | 106 | */ |
| 107 | - public function storeCard( $merchant_id, $terminal_id, $card ) { |
|
| 108 | - if ( null == $card ) { |
|
| 109 | - throw new Exception( 'Card is null' ); |
|
| 107 | + public function storeCard($merchant_id, $terminal_id, $card) { |
|
| 108 | + if (null == $card) { |
|
| 109 | + throw new Exception('Card is null'); |
|
| 110 | 110 | } |
| 111 | 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.' ); |
|
| 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 | 115 | } |
| 116 | 116 | |
| 117 | - return $this->makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ); |
|
| 117 | + return $this->makeStoreCardRequest($access_token, $merchant_id, $terminal_id, $card); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -125,43 +125,43 @@ discard block |
||
| 125 | 125 | * |
| 126 | 126 | * @return TransactionResult |
| 127 | 127 | */ |
| 128 | - private function makeSaleRequest( $access_token, $merchant_id, $terminal_id, $sale ) { |
|
| 128 | + private function makeSaleRequest($access_token, $merchant_id, $terminal_id, $sale) { |
|
| 129 | 129 | $sale_request = $sale->serialize($merchant_id, $terminal_id); |
| 130 | - $body = json_encode( $sale_request ); |
|
| 130 | + $body = json_encode($sale_request); |
|
| 131 | 131 | |
| 132 | - $this->logger->info("Starting transaction for $" . $sale->getTotal()); |
|
| 132 | + $this->logger->info("Starting transaction for $".$sale->getTotal()); |
|
| 133 | 133 | |
| 134 | - $request = new Request($this->gateway_url . 'api/v1/transactions/sale'); |
|
| 134 | + $request = new Request($this->gateway_url.'api/v1/transactions/sale'); |
|
| 135 | 135 | $request->getOptions() |
| 136 | 136 | ->set(CURLOPT_RETURNTRANSFER, true) |
| 137 | 137 | ->set(CURLOPT_POST, true) |
| 138 | 138 | ->set(CURLOPT_POSTFIELDS, $body) |
| 139 | 139 | ->set(CURLOPT_HTTPHEADER, array( |
| 140 | 140 | 'Content-Type: application/json', |
| 141 | - 'Content-Length: ' . strlen($body), |
|
| 141 | + 'Content-Length: '.strlen($body), |
|
| 142 | 142 | 'Accept: application/json', |
| 143 | - 'Authorization: Bearer ' . $access_token |
|
| 143 | + 'Authorization: Bearer '.$access_token |
|
| 144 | 144 | )); |
| 145 | 145 | $response = $request->send(); |
| 146 | 146 | |
| 147 | - $this->logger->debug( "Transaction completed" ); |
|
| 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 ) { |
|
| 151 | + if ($sale_response->success) { |
|
| 152 | 152 | $transaction_id = $sale_response->result->transaction_id; |
| 153 | 153 | |
| 154 | - $this->logger->info( 'Payment completed. Transaction ID: ' . $transaction_id ); |
|
| 154 | + $this->logger->info('Payment completed. Transaction ID: '.$transaction_id); |
|
| 155 | 155 | |
| 156 | - return new TransactionResult( true, $transaction_id ); |
|
| 156 | + return new TransactionResult(true, $transaction_id); |
|
| 157 | 157 | } else { |
| 158 | 158 | $error_message = $sale_response->error->message; |
| 159 | 159 | $code = $sale_response->error->code; |
| 160 | 160 | |
| 161 | - $this->logger->debug( "Sale response: " . var_export( $sale_response, true ) ); |
|
| 162 | - $this->logger->error( "Payment failed." ); |
|
| 161 | + $this->logger->debug("Sale response: ".var_export($sale_response, true)); |
|
| 162 | + $this->logger->error("Payment failed."); |
|
| 163 | 163 | |
| 164 | - return new TransactionResult( false, null, $error_message, $code ); |
|
| 164 | + return new TransactionResult(false, null, $error_message, $code); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
@@ -175,45 +175,45 @@ discard block |
||
| 175 | 175 | * |
| 176 | 176 | * @return TransactionResult |
| 177 | 177 | */ |
| 178 | - private function makeVoidRequest( $access_token, $transaction_id, $merchant_id, $terminal_id ) { |
|
| 178 | + private function makeVoidRequest($access_token, $transaction_id, $merchant_id, $terminal_id) { |
|
| 179 | 179 | $void = new V0id(TerminalType::$ECommerce, $transaction_id); |
| 180 | 180 | $void_request = $void->serialize($merchant_id, $terminal_id); |
| 181 | 181 | |
| 182 | - $body = json_encode( $void_request ); |
|
| 182 | + $body = json_encode($void_request); |
|
| 183 | 183 | |
| 184 | - $this->logger->info( "Starting void transaction for transaction# " . $transaction_id ); |
|
| 184 | + $this->logger->info("Starting void transaction for transaction# ".$transaction_id); |
|
| 185 | 185 | |
| 186 | - $request = new Request($this->gateway_url . 'api/v1/transactions/void'); |
|
| 186 | + $request = new Request($this->gateway_url.'api/v1/transactions/void'); |
|
| 187 | 187 | $request->getOptions() |
| 188 | 188 | ->set(CURLOPT_RETURNTRANSFER, true) |
| 189 | 189 | ->set(CURLOPT_POST, true) |
| 190 | 190 | ->set(CURLOPT_POSTFIELDS, $body) |
| 191 | 191 | ->set(CURLOPT_HTTPHEADER, array( |
| 192 | 192 | 'Content-Type: application/json', |
| 193 | - 'Content-Length: ' . strlen($body), |
|
| 193 | + 'Content-Length: '.strlen($body), |
|
| 194 | 194 | 'Accept: application/json', |
| 195 | - 'Authorization: Bearer ' . $access_token |
|
| 195 | + 'Authorization: Bearer '.$access_token |
|
| 196 | 196 | )); |
| 197 | 197 | $response = $request->send(); |
| 198 | 198 | |
| 199 | - $this->logger->debug( "Transaction completed" ); |
|
| 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 ) { |
|
| 203 | + if ($void_response->success) { |
|
| 204 | 204 | $transaction_id = $void_response->result->transaction_id; |
| 205 | 205 | |
| 206 | - $this->logger->info( 'Void completed. Transaction ID: ' . $transaction_id ); |
|
| 206 | + $this->logger->info('Void completed. Transaction ID: '.$transaction_id); |
|
| 207 | 207 | |
| 208 | - return new TransactionResult( true, $transaction_id ); |
|
| 208 | + return new TransactionResult(true, $transaction_id); |
|
| 209 | 209 | } else { |
| 210 | 210 | $error_message = $void_response->error->message; |
| 211 | 211 | $code = $void_response->error->code; |
| 212 | 212 | |
| 213 | - $this->logger->debug( "Void response: " . var_export( $void_response, true ) ); |
|
| 214 | - $this->logger->error( "Void failed." ); |
|
| 213 | + $this->logger->debug("Void response: ".var_export($void_response, true)); |
|
| 214 | + $this->logger->error("Void failed."); |
|
| 215 | 215 | |
| 216 | - return new TransactionResult( false, null, $error_message, $code ); |
|
| 216 | + return new TransactionResult(false, null, $error_message, $code); |
|
| 217 | 217 | } |
| 218 | 218 | } |
| 219 | 219 | |
@@ -227,39 +227,39 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @return string |
| 229 | 229 | */ |
| 230 | - private function makeStoreCardRequest( $access_token, $merchant_id, $terminal_id, $card ) { |
|
| 230 | + private function makeStoreCardRequest($access_token, $merchant_id, $terminal_id, $card) { |
|
| 231 | 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 | - $this->logger->info( "Starting request for tokenization" ); |
|
| 235 | + $this->logger->info("Starting request for tokenization"); |
|
| 236 | 236 | |
| 237 | - $request = new Request($this->gateway_url . 'api/v1/transactions/tokenize'); |
|
| 237 | + $request = new Request($this->gateway_url.'api/v1/transactions/tokenize'); |
|
| 238 | 238 | $request->getOptions() |
| 239 | 239 | ->set(CURLOPT_RETURNTRANSFER, true) |
| 240 | 240 | ->set(CURLOPT_POST, true) |
| 241 | 241 | ->set(CURLOPT_POSTFIELDS, $body) |
| 242 | 242 | ->set(CURLOPT_HTTPHEADER, array( |
| 243 | 243 | 'Content-Type: application/json', |
| 244 | - 'Content-Length: ' . strlen($body), |
|
| 244 | + 'Content-Length: '.strlen($body), |
|
| 245 | 245 | 'Accept: application/json', |
| 246 | - 'Authorization: Bearer ' . $access_token |
|
| 246 | + 'Authorization: Bearer '.$access_token |
|
| 247 | 247 | )); |
| 248 | 248 | $response = $request->send(); |
| 249 | 249 | |
| 250 | - $this->logger->debug( "Transaction completed" ); |
|
| 250 | + $this->logger->debug("Transaction completed"); |
|
| 251 | 251 | |
| 252 | - $token_response = json_decode( $response->getContent() ); |
|
| 252 | + $token_response = json_decode($response->getContent()); |
|
| 253 | 253 | |
| 254 | - if ( $token_response->success ) { |
|
| 254 | + if ($token_response->success) { |
|
| 255 | 255 | $transaction_id = $token_response->result->transaction_id; |
| 256 | 256 | |
| 257 | - $this->logger->info( 'Tokenization completed. Transaction ID: ' . $transaction_id ); |
|
| 257 | + $this->logger->info('Tokenization completed. Transaction ID: '.$transaction_id); |
|
| 258 | 258 | |
| 259 | 259 | return $token_response->result->token; |
| 260 | 260 | } else { |
| 261 | - $this->logger->debug( "Tokenization response: " . var_export( $token_response, true ) ); |
|
| 262 | - $this->logger->error( "Tokenization failed." ); |
|
| 261 | + $this->logger->debug("Tokenization response: ".var_export($token_response, true)); |
|
| 262 | + $this->logger->error("Tokenization failed."); |
|
| 263 | 263 | |
| 264 | 264 | return ''; |
| 265 | 265 | } |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @return string |
| 276 | 276 | */ |
| 277 | - private function getAccessToken( $client_id, $client_secret, $token_url ) { |
|
| 277 | + private function getAccessToken($client_id, $client_secret, $token_url) { |
|
| 278 | 278 | $token_data = array( |
| 279 | 279 | 'client_id' => $client_id, |
| 280 | 280 | 'grant_type' => 'client_credentials', |
@@ -293,16 +293,16 @@ discard block |
||
| 293 | 293 | )); |
| 294 | 294 | $response = $request->send(); |
| 295 | 295 | |
| 296 | - if ( $response->hasError() ) { |
|
| 297 | - $this->logger->debug( 'Token response status code was ' . $response->getError()->getCode() ); |
|
| 298 | - $this->logger->debug( "Token request ended in failure: " . var_export( $response, true ) ); |
|
| 299 | - $this->logger->error( "Gateway authorization failed. Check credentials." ); |
|
| 296 | + if ($response->hasError()) { |
|
| 297 | + $this->logger->debug('Token response status code was '.$response->getError()->getCode()); |
|
| 298 | + $this->logger->debug("Token request ended in failure: ".var_export($response, true)); |
|
| 299 | + $this->logger->error("Gateway authorization failed. Check credentials."); |
|
| 300 | 300 | |
| 301 | 301 | return null; |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - $this->logger->debug( "Token request was a success: " . $response->getContent()); |
|
| 305 | - $token = json_decode( $response->getContent() ); |
|
| 304 | + $this->logger->debug("Token request was a success: ".$response->getContent()); |
|
| 305 | + $token = json_decode($response->getContent()); |
|
| 306 | 306 | |
| 307 | 307 | return $token->access_token; |
| 308 | 308 | } |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @return array |
| 318 | 318 | */ |
| 319 | - private function generateTokenizationRequest( $merchant_id, $terminal_id, $card ) { |
|
| 319 | + private function generateTokenizationRequest($merchant_id, $terminal_id, $card) { |
|
| 320 | 320 | $token_request = array( |
| 321 | 321 | 'mid' => $merchant_id, |
| 322 | 322 | 'terminal' => array( |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | private $error_message; |
| 15 | 15 | private $code; |
| 16 | 16 | |
| 17 | - public function __construct( $success, $transaction_id = null, $error_message = null, $code = null ) { |
|
| 17 | + public function __construct($success, $transaction_id = null, $error_message = null, $code = null) { |
|
| 18 | 18 | $this->success = $success; |
| 19 | 19 | $this->transaction_id = $transaction_id; |
| 20 | 20 | $this->error_message = $error_message; |