From 261574a2e7e009deb88664f4ec419770d2ecb634 Mon Sep 17 00:00:00 2001 From: Djairho Geuens Date: Wed, 24 Jun 2020 22:32:15 +0200 Subject: [PATCH 1/6] feat(#1): adds reference for createPayment --- src/Client.php | 162 ++++++++++++------ .../CreatePaymentFailedException.php | 11 ++ .../CreateTransactionFailedException.php | 5 + .../GetPaymentDetailsFailedException.php | 11 ++ .../RetrieveTransactionFailedException.php | 5 + 5 files changed, 138 insertions(+), 56 deletions(-) create mode 100644 src/Support/Exceptions/CreatePaymentFailedException.php create mode 100644 src/Support/Exceptions/GetPaymentDetailsFailedException.php diff --git a/src/Client.php b/src/Client.php index 2d53e98..36ff35f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,27 +3,29 @@ namespace Payconiq; use Payconiq\Support\Exceptions\CreateTransactionFailedException; +use Payconiq\Support\Exceptions\CreatePaymentFailedException; +use Payconiq\Support\Exceptions\GetPaymentDetailsFailedException; use Payconiq\Support\Exceptions\RetrieveTransactionFailedException; class Client { - - protected $merchant_id; - protected $access_token; + + protected $merchantId; + protected $accessToken; protected $endpoint = 'https://api.payconiq.com/v3'; - + /** * Construct * - * @param string $merchent_id The merchant ID registered with Payconiq. - * @param string $access_token Used to secure request between merchant backend and Payconiq backend. + * @param string $merchantId The merchant ID registered with Payconiq. + * @param string $accessToken Used to secure request between merchant backend and Payconiq backend. * * @return void */ - public function __construct($merchant_id = null, $access_token = null) + public function __construct($merchantId = null, $accessToken = null) { - $this->merchant_id = $merchant_id; - $this->access_token = $access_token; + $this->merchantId = $merchantId; + $this->accessToken = $accessToken; } /** @@ -43,13 +45,13 @@ public function setEndpoint($url) /** * Set the merchant id * - * @param string $merchent_id The merchant ID registered with Payconiq. + * @param string $merchantId The merchant ID registered with Payconiq. * * @return self */ - public function setMerchantId($merchant_id) + public function setMerchantId($merchantId) { - $this->merchant_id = $merchant_id; + $this->merchantId = $merchantId; return $this; } @@ -57,13 +59,13 @@ public function setMerchantId($merchant_id) /** * Set the access token * - * @param string $access_token Used to secure request between merchant backend and Payconiq backend. + * @param string $accessToken Used to secure request between merchant backend and Payconiq backend. * * @return self */ - public function setAccessToken($access_token) + public function setAccessToken($accessToken) { - $this->access_token = $access_token; + $this->accessToken = $accessToken; return $this; } @@ -77,6 +79,9 @@ public function setAccessToken($access_token) * * @return string transaction_id * @throws CreateTransactionFailedException If the response has no transactionid + * + * @deprecated Use createPayment instead + * @see createPayment */ public function createTransaction($amount, $currency, $callbackUrl) { @@ -86,29 +91,75 @@ public function createTransaction($amount, $currency, $callbackUrl) 'callbackUrl' => $callbackUrl, ]); - if(empty($response['transactionId'])) + if (empty($response['transactionId'])) throw new CreateTransactionFailedException($response['message']); return $response['transactionId']; } /** - * Retrieve an existing transaction + * Create a new payment + * + * @param float $amount Payment amount in cents + * @param string $currency Payment currency code in IOS 4217 format + * @param string $reference External payment reference used to reference the Payconiq payment in the calling party's system + * @param string $callbackUrl A url to which the merchant or partner will be notified of a payment + * + * @return string paymentId + * @throws CreatePaymentFailedException If the response has no transactionid + */ + public function createPayment($amount, $currency = 'EUR', $reference, $callbackUrl) + { + $response = $this->curl('POST', $this->getEndpoint('/payments'), $this->constructHeaders(), [ + 'amount' => $amount, + 'currency' => $currency, + 'reference' => $reference, + 'callbackUrl' => $callbackUrl, + ]); + + if (empty($response['paymentId'])) + throw new CreatePaymentFailedException($response['message']); + + return $response['paymentId']; + } + + /** + * Retrieve an existing payment * - * @param string $transaction_id The transaction id provided by Payconiq + * @param string $transactionId The transaction id provided by Payconiq * * @return array Response object by Payconiq + * + * @deprecated Use getPaymentDetails instead + * @see getPaymentDetails */ - public function retrieveTransaction($transaction_id) + public function retrieveTransaction($transactionId) { - $response = $this->curl('GET', $this->getEndpoint('/payments/'.$transaction_id), $this->constructHeaders()); + $response = $this->curl('GET', $this->getEndpoint('/payments/' . $transactionId), $this->constructHeaders()); - if(empty($response['paymentId'])) + if (empty($response['paymentId'])) throw new RetrieveTransactionFailedException($response['message']); return $response; } + /** + * Get payment details of an existing payment + * + * @param string $paymentId The unique Payconiq identifier of a payment as provided by the create payment service + * + * @return array Response object by Payconiq + */ + public function getPaymentDetails($paymentId) + { + $response = $this->curl('GET', $this->getEndpoint('/payments/' . $paymentId), $this->constructHeaders()); + + if (empty($response['paymentId'])) + throw new GetPaymentDetailsFailedException($response['message']); + + return $response; + } + /** * Get the endpoint for the call * @@ -128,41 +179,40 @@ private function constructHeaders() { return [ 'Content-Type: application/json', - 'Authorization: '.$this->access_token, + 'Authorization: ' . $this->accessToken, ]; } - + /** - * cURL request - * - * @param string $method - * @param string $url - * @param array $headers - * @param array $parameters - * - * @return response - */ - private function cURL($method, $url, $headers=[], $parameters=[]) - { - $curl = curl_init(); - - curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_VERBOSE, 0); - curl_setopt($curl, CURLOPT_HEADER, 1); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,20); - curl_setopt($curl, CURLOPT_TIMEOUT, 20); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); - - $response = curl_exec($curl); - $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $body = substr($response, $header_size); - curl_close($curl); - - return json_decode($body,true); - } - -} \ No newline at end of file + * cURL request + * + * @param string $method + * @param string $url + * @param array $headers + * @param array $parameters + * + * @return response + */ + private function cURL($method, $url, $headers = [], $parameters = []) + { + $curl = curl_init(); + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_VERBOSE, 0); + curl_setopt($curl, CURLOPT_HEADER, 1); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20); + curl_setopt($curl, CURLOPT_TIMEOUT, 20); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); + + $response = curl_exec($curl); + $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $body = substr($response, $header_size); + curl_close($curl); + + return json_decode($body, true); + } +} diff --git a/src/Support/Exceptions/CreatePaymentFailedException.php b/src/Support/Exceptions/CreatePaymentFailedException.php new file mode 100644 index 0000000..2b69161 --- /dev/null +++ b/src/Support/Exceptions/CreatePaymentFailedException.php @@ -0,0 +1,11 @@ + Date: Sat, 27 Jun 2020 19:20:59 +0200 Subject: [PATCH 2/6] feat(#1): adds better support for different environments and introduces apiKey instead of access token --- src/Client.php | 55 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/src/Client.php b/src/Client.php index 36ff35f..15f99de 100644 --- a/src/Client.php +++ b/src/Client.php @@ -10,22 +10,32 @@ class Client { + const ENVIRONMENT_PROD = 'prod'; + const ENVIRONMENT_EXT = 'ext'; + protected $merchantId; + /** + * @deprecated + */ protected $accessToken; - protected $endpoint = 'https://api.payconiq.com/v3'; + protected $apiKey; + protected $endpoint; /** * Construct * - * @param string $merchantId The merchant ID registered with Payconiq. - * @param string $accessToken Used to secure request between merchant backend and Payconiq backend. + * @param string $merchantId The merchant ID registered with Payconiq. + * @param string $accessToken Used to secure request between merchant backend and Payconiq backend (deprecated: use $apiKey instead). + * @param string $apiKey Used to secure request between merchant backend and Payconiq backend. * * @return void */ - public function __construct($merchantId = null, $accessToken = null) + public function __construct($merchantId = null, $accessToken = null, $apiKey = null, $environment = self::ENVIRONMENT_PROD) { $this->merchantId = $merchantId; $this->accessToken = $accessToken; + $this->apiKey = $apiKey; + $this->endpoint = $environment == self::ENVIRONMENT_PROD ? 'https://api.payconiq.com/v3' : 'https://api.ext.payconiq.com/v3'; } /** @@ -62,6 +72,9 @@ public function setMerchantId($merchantId) * @param string $accessToken Used to secure request between merchant backend and Payconiq backend. * * @return self + * + * @deprecated Use setApiKey instead + * @see setApiKey */ public function setAccessToken($accessToken) { @@ -70,11 +83,25 @@ public function setAccessToken($accessToken) return $this; } + /** + * Set the API key + * + * @param string $apiKey Used to secure request between merchant backend and Payconiq backend. + * + * @return self + */ + public function setApiKey($apiKey) + { + $this->apiKey = $apiKey; + + return $this; + } + /** * Create a new transaction * - * @param float $amount Transaction amount in cents - * @param string $currency Amount currency + * @param float $amount Transaction amount in cents + * @param string $currency Amount currency * @param string $callbackUrl Callback where payconiq needs to send confirmation status * * @return string transaction_id @@ -100,12 +127,12 @@ public function createTransaction($amount, $currency, $callbackUrl) /** * Create a new payment * - * @param float $amount Payment amount in cents - * @param string $currency Payment currency code in IOS 4217 format - * @param string $reference External payment reference used to reference the Payconiq payment in the calling party's system + * @param float $amount Payment amount in cents + * @param string $currency Payment currency code in IOS 4217 format + * @param string $reference External payment reference used to reference the Payconiq payment in the calling party's system * @param string $callbackUrl A url to which the merchant or partner will be notified of a payment * - * @return string paymentId + * @return object payment object * @throws CreatePaymentFailedException If the response has no transactionid */ public function createPayment($amount, $currency = 'EUR', $reference, $callbackUrl) @@ -120,7 +147,7 @@ public function createPayment($amount, $currency = 'EUR', $reference, $callbackU if (empty($response['paymentId'])) throw new CreatePaymentFailedException($response['message']); - return $response['paymentId']; + return $response; } /** @@ -179,7 +206,7 @@ private function constructHeaders() { return [ 'Content-Type: application/json', - 'Authorization: ' . $this->accessToken, + 'Authorization: ' . (!is_null($this->apiKey) ? $this->apiKey : $this->accessToken) ]; } @@ -193,7 +220,7 @@ private function constructHeaders() * * @return response */ - private function cURL($method, $url, $headers = [], $parameters = []) + private static function cURL($method, $url, $headers = [], $parameters = []) { $curl = curl_init(); @@ -213,6 +240,6 @@ private function cURL($method, $url, $headers = [], $parameters = []) $body = substr($response, $header_size); curl_close($curl); - return json_decode($body, true); + return json_decode($body); } } From 18b6b2c134100ff7cf9d42802a813be3590a93cf Mon Sep 17 00:00:00 2001 From: Djairho Geuens Date: Sat, 27 Jun 2020 19:30:09 +0200 Subject: [PATCH 3/6] chore(#1): updates readme and composer json --- README.md | 38 +++++++++++++++++++------------------- composer.json | 4 +++- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a8f3bfc..9173034 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Accepting [Payconiq](https://www.payconiq.com/) payments with the use of the QR ## Requirements ## To use the Payconiq API client, the following things are required: -+ Payconiq Merchant Id and Access Token ++ Payconiq Merchant Id and API key + PHP >= 5.6 + PHP cURL extension @@ -26,8 +26,8 @@ You may also git checkout or [download all the files](https://github.com/EventSq We use the following parameters in the examples below: ```php -$merchant_id = ''; // The merchant ID registered with Payconiq. -$access_token = ''; // Used to secure request between merchant backend and Payconiq backend. +$merchantId = 'abc'; // The merchant ID registered with Payconiq. +$apiKey = 'apiKey 123456'; // Used to secure request between merchant backend and Payconiq backend. $amount = 1000; // Transaction amount in cents $currency = 'EUR'; // Currency @@ -39,30 +39,30 @@ To learn more about how, when and what Payconiq will POST to your callbackUrl, ## Usage ## -### Create a transaction ### +### Create a payment ### ```php use Payconiq\Client; -$payconiq = new Client($merchant_id, $access_token); +$payconiq = new Client($merchantId, $apiKey); -// Create a new transaction -$transaction_id = $payconiq->createTransaction($amount, $currency, $callbackUrl); +// Create a new payment +$payment = $payconiq->createPayment($amount, $currency, $callbackUrl); // Assemble QR code content -$qrcode = 'https://payconiq.com/pay/1/' . $transaction_id; +$qrcode = $payment->_links->qrcode->href; ``` -### Retrieve a transaction ### +### Retrieve a payment ### ```php use Payconiq\Client; -$payconiq = new Client($merchant_id, $access_token); +$payconiq = new Client($merchantId, $accessToken); -// Retrieve a transaction -$transaction = $payconiq->retrieveTransaction($transaction_id); +// Retrieve a payment +$payment = $payconiq->retrievePayment($paymentId); ``` ## Laravel support ## @@ -88,21 +88,21 @@ Publish the Payconiq config file with the artisan command and fill in your crede php artisan vendor:publish ``` -### Create a transaction ### +### Create a payment ### ```php use Payconiq; -// Create a new transaction -$transaction_id = Payconiq::createTransaction($amount, $currency, $callbackUrl); +// Create a new payment +$payment = Payconiq::createPayment($amount, $currency, $callbackUrl); // Assemble QR code content -$qrcode = 'https://payconiq.com/pay/1/' . $transaction_id; +$qrcode = $payment->_links->qrcode->href; ``` -### Retrieve a transaction ### +### Retrieve a payment ### ```php use Payconiq; -// Retrieve a transaction -$transaction = Payconiq::retrieveTransaction($transaction_id); +// Retrieve a payment +$payment = Payconiq::retrievePayment($payment); ``` diff --git a/composer.json b/composer.json index bcd2617..fe57e80 100644 --- a/composer.json +++ b/composer.json @@ -7,5 +7,7 @@ "email": "glenn.engelen@eventsquare.co" } ], - "require": {} + "require": { + "php": ">=5.3" + } } From 1aeaff359ae9ed6b93828ab61a50fd33854d400c Mon Sep 17 00:00:00 2001 From: Djairho Geuens Date: Sat, 27 Jun 2020 19:33:18 +0200 Subject: [PATCH 4/6] chore(#1): fixes documentation --- src/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Client.php b/src/Client.php index 15f99de..f04a72d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,6 +27,7 @@ class Client * @param string $merchantId The merchant ID registered with Payconiq. * @param string $accessToken Used to secure request between merchant backend and Payconiq backend (deprecated: use $apiKey instead). * @param string $apiKey Used to secure request between merchant backend and Payconiq backend. + * @param string $environment Environment to use when making API calls * * @return void */ From 79b317a3bcfb255baa0ba13ce3580d927a8653e3 Mon Sep 17 00:00:00 2001 From: Djairho Geuens Date: Sun, 28 Jun 2020 22:52:11 +0200 Subject: [PATCH 5/6] feat(#1): add endpoint for payments list --- src/Client.php | 40 ++++++++++++++----- .../GetPaymentsListFailedException.php | 11 +++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 src/Support/Exceptions/GetPaymentsListFailedException.php diff --git a/src/Client.php b/src/Client.php index f04a72d..1394105 100644 --- a/src/Client.php +++ b/src/Client.php @@ -6,6 +6,7 @@ use Payconiq\Support\Exceptions\CreatePaymentFailedException; use Payconiq\Support\Exceptions\GetPaymentDetailsFailedException; use Payconiq\Support\Exceptions\RetrieveTransactionFailedException; +use Payconiq\Support\Exceptions\GetPaymentsListFailedException; class Client { @@ -119,8 +120,8 @@ public function createTransaction($amount, $currency, $callbackUrl) 'callbackUrl' => $callbackUrl, ]); - if (empty($response['transactionId'])) - throw new CreateTransactionFailedException($response['message']); + if (empty($response->transactionId)) + throw new CreateTransactionFailedException($response->message); return $response['transactionId']; } @@ -145,8 +146,8 @@ public function createPayment($amount, $currency = 'EUR', $reference, $callbackU 'callbackUrl' => $callbackUrl, ]); - if (empty($response['paymentId'])) - throw new CreatePaymentFailedException($response['message']); + if (empty($response->paymentId)) + throw new CreatePaymentFailedException($response->message); return $response; } @@ -156,7 +157,7 @@ public function createPayment($amount, $currency = 'EUR', $reference, $callbackU * * @param string $transactionId The transaction id provided by Payconiq * - * @return array Response object by Payconiq + * @return object Response object by Payconiq * * @deprecated Use getPaymentDetails instead * @see getPaymentDetails @@ -165,8 +166,8 @@ public function retrieveTransaction($transactionId) { $response = $this->curl('GET', $this->getEndpoint('/payments/' . $transactionId), $this->constructHeaders()); - if (empty($response['paymentId'])) - throw new RetrieveTransactionFailedException($response['message']); + if (empty($response->paymentId)) + throw new RetrieveTransactionFailedException($response->message); return $response; } @@ -176,18 +177,37 @@ public function retrieveTransaction($transactionId) * * @param string $paymentId The unique Payconiq identifier of a payment as provided by the create payment service * - * @return array Response object by Payconiq + * @return object Response object by Payconiq */ public function getPaymentDetails($paymentId) { $response = $this->curl('GET', $this->getEndpoint('/payments/' . $paymentId), $this->constructHeaders()); - if (empty($response['paymentId'])) - throw new GetPaymentDetailsFailedException($response['message']); + if (empty($response->paymentId)) + throw new GetPaymentDetailsFailedException($response->message); return $response; } + /** + * Get payments list + * + * @param string $reference External payment reference used to reference the Payconiq payment in the calling party's system + * + * @return array Response objects by Payconiq + */ + public function getPaymentsList($reference) + { + $response = $this->curl('POST', $this->getEndpoint('/payments/search'), $this->constructHeaders(), [ + 'reference' => $reference + ]); + + if (empty($response->size)) + throw new GetPaymentsListFailedException($response->message); + + return $response->details; + } + /** * Get the endpoint for the call * diff --git a/src/Support/Exceptions/GetPaymentsListFailedException.php b/src/Support/Exceptions/GetPaymentsListFailedException.php new file mode 100644 index 0000000..30e85aa --- /dev/null +++ b/src/Support/Exceptions/GetPaymentsListFailedException.php @@ -0,0 +1,11 @@ + Date: Wed, 5 Aug 2020 20:48:00 +0200 Subject: [PATCH 6/6] fixes PR comments --- README.md | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9173034..3ece92a 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ use Payconiq\Client; $payconiq = new Client($merchantId, $apiKey); // Create a new payment -$payment = $payconiq->createPayment($amount, $currency, $callbackUrl); +$payment = $payconiq->createPayment($amount, $currency, $reference, $callbackUrl); // Assemble QR code content $qrcode = $payment->_links->qrcode->href; @@ -93,7 +93,7 @@ php artisan vendor:publish use Payconiq; // Create a new payment -$payment = Payconiq::createPayment($amount, $currency, $callbackUrl); +$payment = Payconiq::createPayment($amount, $currency, $reference, $callbackUrl); // Assemble QR code content $qrcode = $payment->_links->qrcode->href; diff --git a/composer.json b/composer.json index fe57e80..09bd1fb 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,6 @@ } ], "require": { - "php": ">=5.3" + "php": ">=5.6" } }