Skip to content

Commit

Permalink
Merge pull request #2 from bileto/accept-notifications
Browse files Browse the repository at this point in the history
Accept notifications from PayU
  • Loading branch information
Honza Machala authored Sep 1, 2016
2 parents a0cc9f4 + b084de6 commit 167e4ce
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env-default
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
POS_ID=
SECOND_KEY=
OAUTH_CLIENT_SECRET=
POS_AUTH_KEY=
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
"email": "jan.machala@bileto.com"
}
],
"minimum-stability": "stable",
"autoload": {
"psr-4": { "Omnipay\\PayU\\" : "src/" }
},
"require": {
"omnipay/common": "^2.3"
"omnipay/common": "^2.3",
"openpayu/openpayu_php_sdk": "^2.2"
},
"require-dev": {
"omnipay/tests": "^2.1",
"symfony/var-dumper": "^2.7",
"mockery/mockery": "^0.9.5",
"vlucas/phpdotenv": "^2.3"
"vlucas/phpdotenv": "^2.3",
"phpunit/phpunit": "3.7.*"
}
}
3 changes: 2 additions & 1 deletion examples/test-finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true);

try {
$completeRequest = ['transactionId' => 'GD8J8WF8Z2160822GUEST000P01'];
$completeRequest = ['transactionReference' => 'J9R4JP3F2G160825GUEST000P01'];
$response = $gateway->completePurchase($completeRequest);

echo "Is Successful: " . $response->isSuccessful() . PHP_EOL;
echo "TransactionId: " . $response->getTransactionId() . PHP_EOL;
echo "State code: " . $response->getCode() . PHP_EOL;
echo "PaymentId: " , $response->getTransactionReference() . PHP_EOL;
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;

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

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

use Omnipay\PayU\GatewayFactory;
use Omnipay\PayU\Messages\Notification;
use Symfony\Component\HttpFoundation\Request;

$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();

// default is official sandbox
$posId = isset($_ENV['POS_ID']) ? $_ENV['POS_ID'] : '300046';
$secondKey = isset($_ENV['SECOND_KEY']) ? $_ENV['SECOND_KEY'] : '0c017495773278c50c7b35434017b2ca';
$oAuthClientSecret = isset($_ENV['OAUTH_CLIENT_SECRET']) ? $_ENV['OAUTH_CLIENT_SECRET'] : 'c8d4b7ac61758704f38ed5564d8c0ae0';
$posAuthKey = isset($_ENV['POS_AUTH_KEY']) ? $_ENV['POS_AUTH_KEY'] : null; // Official sandbox does not provide signature key

$content = '{"order":{"orderId":"NN18KW7XJG160830GUEST000P01","extOrderId":"57c56b16d22e1","orderCreateDate":"2016-08-30T13:16:39.641+02:00","notifyUrl":"http://52.58.177.216/online-payments/uuid/notify","customerIp":"80.188.133.34","merchantPosId":"300293","description":"Shopping at myStore.com","currencyCode":"PLN","totalAmount":"15000","status":"PENDING","products":[{"name":"Lenovo ThinkPad Edge E540","unitPrice":"15000","quantity":"1"}]},"localReceiptDateTime":"2016-08-30T13:17:14.502+02:00","properties":[{"name":"PAYMENT_ID","value":"72829425"}]}';
$httpRequest = Request::create('/notify', 'POST', [], [], [], [], $content);
$httpRequest->headers->add(
[
'X-OpenPayU-Signature' => 'sender=checkout;signature=b640fa4baa73bb9e34f1cb8e5a5d4301;algorithm=MD5;content=DOCUMENT',
'ContentType' => 'application/json'
]);
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true, $posAuthKey, $httpRequest);

try {
if (!$gateway->supportsAcceptNotification()) {
echo "This Gateway does not support notifications";
}

$response = $gateway->acceptNotification();

echo "PaymentId: " , $response->getTransactionReference() . PHP_EOL;
echo "Message: " . $response->getMessage() . PHP_EOL;
echo "Status: " . $response->getTransactionStatus() . PHP_EOL;
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;

} catch (\Exception $e) {
// dump($e->getResponse()->getBody(true));
dump((string)$e);
}
2 changes: 2 additions & 0 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
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';

$purchaseRequest = [
'purchaseData' => [
'customerIp' => '127.0.0.1',
'continueUrl' => $returnUrl,
'notifyUrl' => $notifyUrl,
'merchantPosId' => $posId,
'description' => $description,
'currencyCode' => 'PLN',
Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="PHPUnit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
28 changes: 21 additions & 7 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Omnipay\PayU\Messages\AccessTokenResponse;
use Omnipay\PayU\Messages\CompletePurchaseRequest;
use Omnipay\PayU\Messages\CompletePurchaseResponse;
use Omnipay\PayU\Messages\Notification;
use Omnipay\PayU\Messages\PurchaseRequest;
use Omnipay\PayU\Messages\PurchaseResponse;

Expand Down Expand Up @@ -63,6 +64,11 @@ public function completePurchase(array $parameters = array())
return $response;
}

public function acceptNotification()
{
return new Notification($this->httpRequest, $this->httpClient, $this->getParameter('secondKey'));
}

/**
* @return string
*/
Expand All @@ -82,6 +88,7 @@ public function getDefaultParameters()
'secondKey' => '',
'clientSecret' => '',
'testMode' => true,
'posAuthKey' => null,
];
}

Expand Down Expand Up @@ -109,6 +116,20 @@ public function setClientSecret($clientSecret)
$this->setParameter('clientSecret', $clientSecret);
}

/**
* @param string|null $posAuthKey
*/
public function setPosAuthKey($posAuthKey = null) {
$this->setParameter('posAuthKey', $posAuthKey);
}

public function initialize(array $parameters = array())
{
parent::initialize($parameters);
$this->setApiUrl($this->getApiUrl());
return $this;
}

private function setApiUrl($apiUrl)
{
$this->setParameter('apiUrl', $apiUrl);
Expand All @@ -118,11 +139,4 @@ private function setAccessToken($accessToken)
{
$this->setParameter('accessToken', $accessToken);
}

public function initialize(array $parameters = [])
{
parent::initialize($parameters);
$this->setApiUrl($this->getApiUrl());
return $this;
}
}
10 changes: 7 additions & 3 deletions src/GatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\PayU;

use Omnipay\Omnipay;
use Symfony\Component\HttpFoundation\Request;

class GatewayFactory
{
Expand All @@ -11,17 +12,20 @@ class GatewayFactory
* @param string $secondKey (MD5)
* @param string $oAuthClientSecret (MD5)
* @param bool $isSandbox
* @param string|null $posAuthKey
* @param Request|null $httpRequest
* @return Gateway
*/
public static function createInstance($posId, $secondKey, $oAuthClientSecret, $isSandbox = false)
public static function createInstance($posId, $secondKey, $oAuthClientSecret, $isSandbox = false, $posAuthKey = null, Request $httpRequest = null)
{
/** @var \Omnipay\PayU\Gateway $gateway */
$gateway = Omnipay::create('PayU');
$gateway = Omnipay::create('PayU', null, $httpRequest);
$gateway->initialize([
'posId' => $posId,
'secondKey' => $secondKey,
'clientSecret' => $oAuthClientSecret,
'testMode' => $isSandbox
'testMode' => $isSandbox,
'posAuthKey' => $posAuthKey,
]);
return $gateway;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Messages/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function sendData($data)
'Content-Type' => 'application/json',
'Authorization' => $data['accessToken']
];
$url = $data['apiUrl'] . '/api/v2_1/orders/' . urlencode($this->getTransactionId());
$url = $data['apiUrl'] . '/api/v2_1/orders/' . urlencode($this->getTransactionReference());
$httpRequest = $this->httpClient->get($url, $headers);
$httpResponse = $httpRequest->send();

Expand Down
18 changes: 15 additions & 3 deletions src/Messages/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ class CompletePurchaseResponse extends AbstractResponse
*/
public function isSuccessful()
{
return 'SUCCESS' === $this->data['status']['statusCode'] ? true : false;
return 'SUCCESS' === $this->data['status']['statusCode'];
}

/**
* @return string
* @return string|null
*/
public function getTransactionId()
{
return (string) $this->data['orders'][0]['orderId'];
if (isset($this->data['orders'][0]['extOrderId'])) {
return (string) $this->data['orders'][0]['extOrderId'];
}
return null;
}

public function isCancelled()
{
return 'CANCELED' === $this->data['status']['statusCode'];
}

/**
Expand Down Expand Up @@ -51,4 +59,8 @@ public function getCode()
return $this->data['orders'][0]['status'];
}

public function isPending()
{
return 'PENDING' === $this->data['status']['statusCode'];
}
}
Loading

0 comments on commit 167e4ce

Please sign in to comment.