diff --git a/README.md b/README.md index a8f3bfc..3ece92a 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, $reference, $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, $reference, $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..09bd1fb 100644 --- a/composer.json +++ b/composer.json @@ -7,5 +7,7 @@ "email": "glenn.engelen@eventsquare.co" } ], - "require": {} + "require": { + "php": ">=5.6" + } } diff --git a/src/Client.php b/src/Client.php index 2d53e98..1394105 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,27 +3,41 @@ namespace Payconiq; use Payconiq\Support\Exceptions\CreateTransactionFailedException; +use Payconiq\Support\Exceptions\CreatePaymentFailedException; +use Payconiq\Support\Exceptions\GetPaymentDetailsFailedException; use Payconiq\Support\Exceptions\RetrieveTransactionFailedException; +use Payconiq\Support\Exceptions\GetPaymentsListFailedException; class Client { - - protected $merchant_id; - protected $access_token; - protected $endpoint = 'https://api.payconiq.com/v3'; - + + const ENVIRONMENT_PROD = 'prod'; + const ENVIRONMENT_EXT = 'ext'; + + protected $merchantId; + /** + * @deprecated + */ + protected $accessToken; + protected $apiKey; + protected $endpoint; + /** * 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 (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 */ - public function __construct($merchant_id = null, $access_token = null) + public function __construct($merchantId = null, $accessToken = null, $apiKey = null, $environment = self::ENVIRONMENT_PROD) { - $this->merchant_id = $merchant_id; - $this->access_token = $access_token; + $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'; } /** @@ -43,13 +57,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 +71,30 @@ 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 + * + * @deprecated Use setApiKey instead + * @see setApiKey + */ + public function setAccessToken($accessToken) + { + $this->accessToken = $accessToken; + + return $this; + } + + /** + * Set the API key + * + * @param string $apiKey Used to secure request between merchant backend and Payconiq backend. * * @return self */ - public function setAccessToken($access_token) + public function setApiKey($apiKey) { - $this->access_token = $access_token; + $this->apiKey = $apiKey; return $this; } @@ -71,12 +102,15 @@ public function setAccessToken($access_token) /** * 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 * @throws CreateTransactionFailedException If the response has no transactionid + * + * @deprecated Use createPayment instead + * @see createPayment */ public function createTransaction($amount, $currency, $callbackUrl) { @@ -86,29 +120,94 @@ 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']; } /** - * 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 object payment object + * @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; + } + + /** + * Retrieve an existing payment + * + * @param string $transactionId The transaction id provided by Payconiq + * + * @return object Response object by Payconiq + * + * @deprecated Use getPaymentDetails instead + * @see getPaymentDetails + */ + public function retrieveTransaction($transactionId) + { + $response = $this->curl('GET', $this->getEndpoint('/payments/' . $transactionId), $this->constructHeaders()); + + if (empty($response->paymentId)) + throw new RetrieveTransactionFailedException($response->message); + + return $response; + } + + /** + * Get payment details of an existing payment * - * @param string $transaction_id The transaction id provided by Payconiq + * @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 retrieveTransaction($transaction_id) + public function getPaymentDetails($paymentId) { - $response = $this->curl('GET', $this->getEndpoint('/payments/'.$transaction_id), $this->constructHeaders()); + $response = $this->curl('GET', $this->getEndpoint('/payments/' . $paymentId), $this->constructHeaders()); - if(empty($response['paymentId'])) - throw new RetrieveTransactionFailedException($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 * @@ -128,41 +227,40 @@ private function constructHeaders() { return [ 'Content-Type: application/json', - 'Authorization: '.$this->access_token, + 'Authorization: ' . (!is_null($this->apiKey) ? $this->apiKey : $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 static 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); + } +} 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 @@ +