Skip to content

Commit 167e4ce

Browse files
author
Honza Machala
authored
Merge pull request #2 from bileto/accept-notifications
Accept notifications from PayU
2 parents a0cc9f4 + b084de6 commit 167e4ce

11 files changed

+269
-17
lines changed

.env-default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
POS_ID=
22
SECOND_KEY=
33
OAUTH_CLIENT_SECRET=
4+
POS_AUTH_KEY=

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
"email": "[email protected]"
2020
}
2121
],
22+
"minimum-stability": "stable",
2223
"autoload": {
2324
"psr-4": { "Omnipay\\PayU\\" : "src/" }
2425
},
2526
"require": {
26-
"omnipay/common": "^2.3"
27+
"omnipay/common": "^2.3",
28+
"openpayu/openpayu_php_sdk": "^2.2"
2729
},
2830
"require-dev": {
2931
"omnipay/tests": "^2.1",
3032
"symfony/var-dumper": "^2.7",
3133
"mockery/mockery": "^0.9.5",
32-
"vlucas/phpdotenv": "^2.3"
34+
"vlucas/phpdotenv": "^2.3",
35+
"phpunit/phpunit": "3.7.*"
3336
}
3437
}

examples/test-finish.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true);
1616

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

2121
echo "Is Successful: " . $response->isSuccessful() . PHP_EOL;
2222
echo "TransactionId: " . $response->getTransactionId() . PHP_EOL;
2323
echo "State code: " . $response->getCode() . PHP_EOL;
2424
echo "PaymentId: " , $response->getTransactionReference() . PHP_EOL;
25+
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;
2526

2627
} catch (\Exception $e) {
2728
dump($e->getResponse()->getBody(true));

examples/test-notification.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
require '../vendor/autoload.php';
4+
5+
use Omnipay\PayU\GatewayFactory;
6+
use Omnipay\PayU\Messages\Notification;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
10+
$dotenv->load();
11+
12+
// default is official sandbox
13+
$posId = isset($_ENV['POS_ID']) ? $_ENV['POS_ID'] : '300046';
14+
$secondKey = isset($_ENV['SECOND_KEY']) ? $_ENV['SECOND_KEY'] : '0c017495773278c50c7b35434017b2ca';
15+
$oAuthClientSecret = isset($_ENV['OAUTH_CLIENT_SECRET']) ? $_ENV['OAUTH_CLIENT_SECRET'] : 'c8d4b7ac61758704f38ed5564d8c0ae0';
16+
$posAuthKey = isset($_ENV['POS_AUTH_KEY']) ? $_ENV['POS_AUTH_KEY'] : null; // Official sandbox does not provide signature key
17+
18+
$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"}]}';
19+
$httpRequest = Request::create('/notify', 'POST', [], [], [], [], $content);
20+
$httpRequest->headers->add(
21+
[
22+
'X-OpenPayU-Signature' => 'sender=checkout;signature=b640fa4baa73bb9e34f1cb8e5a5d4301;algorithm=MD5;content=DOCUMENT',
23+
'ContentType' => 'application/json'
24+
]);
25+
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true, $posAuthKey, $httpRequest);
26+
27+
try {
28+
if (!$gateway->supportsAcceptNotification()) {
29+
echo "This Gateway does not support notifications";
30+
}
31+
32+
$response = $gateway->acceptNotification();
33+
34+
echo "PaymentId: " , $response->getTransactionReference() . PHP_EOL;
35+
echo "Message: " . $response->getMessage() . PHP_EOL;
36+
echo "Status: " . $response->getTransactionStatus() . PHP_EOL;
37+
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;
38+
39+
} catch (\Exception $e) {
40+
// dump($e->getResponse()->getBody(true));
41+
dump((string)$e);
42+
}

examples/test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
try {
1919
$orderNo = uniqid();
2020
$returnUrl = 'http://localhost:8000/gateway-return.php';
21+
$notifyUrl = 'http://127.0.0.1/online-payments/uuid/notify';
2122
$description = 'Shopping at myStore.com';
2223

2324
$purchaseRequest = [
2425
'purchaseData' => [
2526
'customerIp' => '127.0.0.1',
2627
'continueUrl' => $returnUrl,
28+
'notifyUrl' => $notifyUrl,
2729
'merchantPosId' => $posId,
2830
'description' => $description,
2931
'currencyCode' => 'PLN',

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
2+
bootstrap="./vendor/autoload.php"
3+
colors="true">
4+
<testsuites>
5+
<testsuite name="PHPUnit Tests">
6+
<directory>tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<filter>
10+
<whitelist>
11+
<directory suffix=".php">src</directory>
12+
</whitelist>
13+
</filter>
14+
</phpunit>

src/Gateway.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Omnipay\PayU\Messages\AccessTokenResponse;
88
use Omnipay\PayU\Messages\CompletePurchaseRequest;
99
use Omnipay\PayU\Messages\CompletePurchaseResponse;
10+
use Omnipay\PayU\Messages\Notification;
1011
use Omnipay\PayU\Messages\PurchaseRequest;
1112
use Omnipay\PayU\Messages\PurchaseResponse;
1213

@@ -63,6 +64,11 @@ public function completePurchase(array $parameters = array())
6364
return $response;
6465
}
6566

67+
public function acceptNotification()
68+
{
69+
return new Notification($this->httpRequest, $this->httpClient, $this->getParameter('secondKey'));
70+
}
71+
6672
/**
6773
* @return string
6874
*/
@@ -82,6 +88,7 @@ public function getDefaultParameters()
8288
'secondKey' => '',
8389
'clientSecret' => '',
8490
'testMode' => true,
91+
'posAuthKey' => null,
8592
];
8693
}
8794

@@ -109,6 +116,20 @@ public function setClientSecret($clientSecret)
109116
$this->setParameter('clientSecret', $clientSecret);
110117
}
111118

119+
/**
120+
* @param string|null $posAuthKey
121+
*/
122+
public function setPosAuthKey($posAuthKey = null) {
123+
$this->setParameter('posAuthKey', $posAuthKey);
124+
}
125+
126+
public function initialize(array $parameters = array())
127+
{
128+
parent::initialize($parameters);
129+
$this->setApiUrl($this->getApiUrl());
130+
return $this;
131+
}
132+
112133
private function setApiUrl($apiUrl)
113134
{
114135
$this->setParameter('apiUrl', $apiUrl);
@@ -118,11 +139,4 @@ private function setAccessToken($accessToken)
118139
{
119140
$this->setParameter('accessToken', $accessToken);
120141
}
121-
122-
public function initialize(array $parameters = [])
123-
{
124-
parent::initialize($parameters);
125-
$this->setApiUrl($this->getApiUrl());
126-
return $this;
127-
}
128142
}

src/GatewayFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Omnipay\PayU;
44

55
use Omnipay\Omnipay;
6+
use Symfony\Component\HttpFoundation\Request;
67

78
class GatewayFactory
89
{
@@ -11,17 +12,20 @@ class GatewayFactory
1112
* @param string $secondKey (MD5)
1213
* @param string $oAuthClientSecret (MD5)
1314
* @param bool $isSandbox
15+
* @param string|null $posAuthKey
16+
* @param Request|null $httpRequest
1417
* @return Gateway
1518
*/
16-
public static function createInstance($posId, $secondKey, $oAuthClientSecret, $isSandbox = false)
19+
public static function createInstance($posId, $secondKey, $oAuthClientSecret, $isSandbox = false, $posAuthKey = null, Request $httpRequest = null)
1720
{
1821
/** @var \Omnipay\PayU\Gateway $gateway */
19-
$gateway = Omnipay::create('PayU');
22+
$gateway = Omnipay::create('PayU', null, $httpRequest);
2023
$gateway->initialize([
2124
'posId' => $posId,
2225
'secondKey' => $secondKey,
2326
'clientSecret' => $oAuthClientSecret,
24-
'testMode' => $isSandbox
27+
'testMode' => $isSandbox,
28+
'posAuthKey' => $posAuthKey,
2529
]);
2630
return $gateway;
2731
}

src/Messages/CompletePurchaseRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function sendData($data)
3636
'Content-Type' => 'application/json',
3737
'Authorization' => $data['accessToken']
3838
];
39-
$url = $data['apiUrl'] . '/api/v2_1/orders/' . urlencode($this->getTransactionId());
39+
$url = $data['apiUrl'] . '/api/v2_1/orders/' . urlencode($this->getTransactionReference());
4040
$httpRequest = $this->httpClient->get($url, $headers);
4141
$httpResponse = $httpRequest->send();
4242

src/Messages/CompletePurchaseResponse.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ class CompletePurchaseResponse extends AbstractResponse
1111
*/
1212
public function isSuccessful()
1313
{
14-
return 'SUCCESS' === $this->data['status']['statusCode'] ? true : false;
14+
return 'SUCCESS' === $this->data['status']['statusCode'];
1515
}
1616

1717
/**
18-
* @return string
18+
* @return string|null
1919
*/
2020
public function getTransactionId()
2121
{
22-
return (string) $this->data['orders'][0]['orderId'];
22+
if (isset($this->data['orders'][0]['extOrderId'])) {
23+
return (string) $this->data['orders'][0]['extOrderId'];
24+
}
25+
return null;
26+
}
27+
28+
public function isCancelled()
29+
{
30+
return 'CANCELED' === $this->data['status']['statusCode'];
2331
}
2432

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

62+
public function isPending()
63+
{
64+
return 'PENDING' === $this->data['status']['statusCode'];
65+
}
5466
}

0 commit comments

Comments
 (0)