Skip to content

Commit

Permalink
Merge pull request #9 from tomaj/recurrent
Browse files Browse the repository at this point in the history
Implemented recurrent api
  • Loading branch information
jspetrak authored Mar 12, 2019
2 parents c0d4ee8 + cf0e32a commit 3f8b591
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 2 deletions.
36 changes: 36 additions & 0 deletions examples/cancel-recurrent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require '../vendor/autoload.php';

use Guzzle\Http\Exception\ClientErrorResponseException;
use Omnipay\GoPay\GatewayFactory;
use Dotenv\Dotenv;

$dotenv = Dotenv::create(__DIR__ . '/..');
$dotenv->load();

$goId = $_ENV['GO_ID'];
$clientId = $_ENV['CLIENT_ID'];
$clientSecret = $_ENV['CLIENT_SECRET'];

$gateway = GatewayFactory::createInstance($goId, $clientId, $clientSecret, true);

try {
$returnUrl = 'http://localhost:8000/gateway-return.php';
$notifyUrl = 'http://127.0.0.1/online-payments/uuid/notify';
$description = 'Shopping at myStore.com';

$goPayOrder = [
'transactionReference' => '3081797108',
];

$response = $gateway->cancelRecurrence($goPayOrder);

echo 'Id: ' . $response->getId() . PHP_EOL;
echo "Result: " . $response->getResult() . PHP_EOL;
echo 'Is Successful: ' . (bool) $response->isSuccessful() . PHP_EOL;

} catch (ClientErrorResponseException $e) {
dump((string)$e);
dump($e->getResponse()->getBody(true));
}
3 changes: 1 addition & 2 deletions examples/test-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$clientSecret = $_ENV['CLIENT_SECRET'];

$parameters = [
'id' => '3081704379',
'id' => '8027392945',
];
$httpRequest = Request::create('/notify', 'GET', $parameters, [], [], [], []);

Expand All @@ -33,6 +33,5 @@
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;

} catch (\Exception $e) {
dump($e->getResponse()->getBody(true));
dump((string)$e);
}
56 changes: 56 additions & 0 deletions examples/test-recurrent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

require '../vendor/autoload.php';

use Guzzle\Http\Exception\ClientErrorResponseException;
use Omnipay\GoPay\GatewayFactory;
use Dotenv\Dotenv;

$dotenv = Dotenv::create(__DIR__ . '/..');
$dotenv->load();

$goId = $_ENV['GO_ID'];
$clientId = $_ENV['CLIENT_ID'];
$clientSecret = $_ENV['CLIENT_SECRET'];

$gateway = GatewayFactory::createInstance($goId, $clientId, $clientSecret, true);

try {
$orderNo = uniqid();
$returnUrl = 'http://localhost:8000/gateway-return.php';
$notifyUrl = 'http://127.0.0.1/online-payments/uuid/notify';
$description = 'Shopping at myStore.com';

$goPayOrder = [
'transactionReference' => '3081796328',
'purchaseData' => [
'amount' => 1000,
'currency' => 'CZK',
'order_number' => $orderNo,
'order_description' => $description,
'items' => [
['count' => 1, 'name' => $description, 'amount' => 1000],
],
'eet' => [
"celk_trzba" => 15000,
"zakl_dan1" => 14000,
"dan1" => 1000,
"zakl_dan2" => 14000,
"dan2" => 1000,
"mena" => 'CZK'
],
],
];

$response = $gateway->recurrence($goPayOrder);

echo 'Our OrderNo: ' . $orderNo . PHP_EOL;
echo "Parent id: " . $response->getParentId() . PHP_EOL;
echo "TransactionReference: " . $response->getTransactionReference() . PHP_EOL;
echo 'Is Successful: ' . (bool) $response->isSuccessful() . PHP_EOL;
echo 'Is redirect: ' . (bool) $response->isRedirect() . PHP_EOL;

} catch (ClientErrorResponseException $e) {
dump((string)$e);
dump($e->getResponse()->getBody(true));
}
4 changes: 4 additions & 0 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"dan2" => 1000,
"mena" => 'CZK'
],
// 'recurrence' => [
// 'recurrence_cycle' => 'ON_DEMAND',
// 'recurrence_date_to' => '2021-01-01',
// ],
'callback' => [
'return_url' => $returnUrl,
'notification_url' => $notifyUrl,
Expand Down
27 changes: 27 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Omnipay\GoPay\Message\AccessTokenResponse;
use Omnipay\GoPay\Message\PurchaseRequest;
use Omnipay\GoPay\Message\PurchaseResponse;
use Omnipay\GoPay\Message\RecurrenceRequest;
use Omnipay\GoPay\Message\CancelRecurrenceRequest;
use Omnipay\GoPay\Message\StatusRequest;
use Omnipay\GoPay\Message\Notification;

Expand Down Expand Up @@ -108,6 +110,31 @@ public function completePurchase(array $parameters = array())
return $response;
}

/**
* @param array $options
* @return PurchaseResponse
*/
public function recurrence(array $options = array())
{
$this->setToken($this->getAccessToken()->getToken());
$request = parent::createRequest(RecurrenceRequest::class, $options);
$response = $request->send();
return $response;
}

/**
* @param array $options
* @return PurchaseResponse
*/
public function cancelRecurrence(array $options = array())
{
$this->setToken($this->getAccessToken()->getToken());
$request = parent::createRequest(CancelRecurrenceRequest::class, $options);
$response = $request->send();
return $response;
}


public function acceptNotification()
{
$this->setToken($this->getAccessToken()->getToken());
Expand Down
84 changes: 84 additions & 0 deletions src/Message/CancelRecurrenceRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Omnipay\GoPay\Message;

use Omnipay\Common\Message\AbstractRequest;

class CancelRecurrenceRequest extends AbstractRequest
{

/**
* Get the raw data array for this message. The format of this varies from gateway to
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
*
* @return mixed
*/
public function getData()
{
return $this->getParameter('transactionReference');
}

/**
* Get the transaction reference which was used to create recurrent profile
*
* @return mixed
*/
public function getTransactionReference()
{
return $this->getParameter('transactionReference');
}

/**
* Send the request with specified data
*
* @param mixed $data The data to send
* @return RecurrenceResponse
*/
public function sendData($data)
{
$headers = [
'Accept' => 'application/json',
'Accept-Language' => 'en-US',
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => $this->getParameter('token'),
];

$transactionReference = $this->getTransactionReference();

$httpResponse = $this->httpClient->request(
'POST',
$this->getParameter('apiUrl') . '/api/payments/payment/' . $transactionReference . '/void-recurrence',
$headers,
json_encode($data)
);

$purchaseResponseData = json_decode($httpResponse->getBody()->getContents(), true);

$response = new CancelRecurrenceResponse($this, $purchaseResponseData);
return $response;
}

/**
* @param string $token
*/
public function setToken($token)
{
$this->setParameter('token', $token);
}

/**
* @param array $data
*/
public function setPurchaseData($data)
{
$this->setParameter('purchaseData', $data);
}

/**
* @param string $apiUrl
*/
public function setApiUrl($apiUrl)
{
$this->setParameter('apiUrl', $apiUrl);
}
}
48 changes: 48 additions & 0 deletions src/Message/CancelRecurrenceResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Omnipay\GoPay\Message;

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RedirectResponseInterface;

class CancelRecurrenceResponse extends AbstractResponse implements RedirectResponseInterface
{
/**
* Is the response successful?
*
* @return boolean
*/
public function isSuccessful()
{
return $this->getResult() == 'FINISHED';
}

public function isRedirect()
{
return false;
}

/**
* Gets the redirect form data array, if the redirect method is POST.
*/
public function getRedirectData()
{
return null;
}

public function getId()
{
if (isset($this->data['id']) && !empty(isset($this->data['id']))) {
return (string) $this->data['id'];
}
return null;
}

public function getResult()
{
if (isset($this->data['result']) && !empty($this->data['result'])) {
return (string) $this->data['result'];
}
return null;
}
}
84 changes: 84 additions & 0 deletions src/Message/RecurrenceRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Omnipay\GoPay\Message;

use Omnipay\Common\Message\AbstractRequest;

class RecurrenceRequest extends AbstractRequest
{

/**
* Get the raw data array for this message. The format of this varies from gateway to
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
*
* @return mixed
*/
public function getData()
{
return $this->getParameter('purchaseData');
}

/**
* Get the transaction reference which was used to create recurrent profile
*
* @return mixed
*/
public function getTransactionReference()
{
return $this->getParameter('transactionReference');
}

/**
* Send the request with specified data
*
* @param mixed $data The data to send
* @return RecurrenceResponse
*/
public function sendData($data)
{
$headers = [
'Accept' => 'application/json',
'Accept-Language' => 'en-US',
'Content-Type' => 'application/json',
'Authorization' => $this->getParameter('token'),
];

$transactionReference = $this->getTransactionReference();

$httpResponse = $this->httpClient->request(
'POST',
$this->getParameter('apiUrl') . '/api/payments/payment/' . $transactionReference . '/create-recurrence',
$headers,
json_encode($data)
);

$purchaseResponseData = json_decode($httpResponse->getBody()->getContents(), true);

$response = new RecurrenceResponse($this, $purchaseResponseData);
return $response;
}

/**
* @param string $token
*/
public function setToken($token)
{
$this->setParameter('token', $token);
}

/**
* @param array $data
*/
public function setPurchaseData($data)
{
$this->setParameter('purchaseData', $data);
}

/**
* @param string $apiUrl
*/
public function setApiUrl($apiUrl)
{
$this->setParameter('apiUrl', $apiUrl);
}
}
Loading

0 comments on commit 3f8b591

Please sign in to comment.