Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#1): adds reference for createPayment #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 ##
Expand All @@ -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);
```
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"email": "[email protected]"
}
],
"require": {}
"require": {
"php": ">=5.6"
}
}
222 changes: 160 additions & 62 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand All @@ -43,40 +57,60 @@ 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;
}

/**
* 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;
}

/**
* 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)
{
Expand All @@ -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
*
Expand All @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixing apiKey and accessToken doesn't make it easier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing that I deprecated the accessToken but to be backwards compatible this mix is temporarily needed until a new major version is released and the accessToken is removed.

];
}

/**
* 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);
}

}
* 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);
}
}
Loading