Skip to content

Commit

Permalink
Merge pull request #7 from Lewiatan/omnipay3_update
Browse files Browse the repository at this point in the history
Omnipay 3 update
  • Loading branch information
northys authored Jun 24, 2021
2 parents 7d7110b + 00ffd37 commit e9f9738
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 286 deletions.
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@
"psr-4": { "Omnipay\\PayU\\" : "src/" }
},
"require": {
"php": "^7.2|^8",
"ext-curl": "*",
"ext-json": "*",
"omnipay/common": "^2.5",
"omnipay/common": "^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",
"phpunit/phpunit": "3.7.*",
"phpstan/phpstan": "^0.12.42"
"omnipay/tests": "^4.1",
"symfony/var-dumper": "^5.3",
"mockery/mockery": "^1.4",
"vlucas/phpdotenv": "^5.3",
"phpunit/phpunit": "^9",
"phpstan/phpstan": "^0.12.9",
"php-http/guzzle7-adapter": "^1.0"
}
}
15 changes: 10 additions & 5 deletions examples/test-finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
require '../vendor/autoload.php';

use Omnipay\PayU\GatewayFactory;
use Omnipay\PayU\Messages\CompletePurchaseResponse;

$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv = Dotenv\Dotenv::createImmutable(__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';
$posId = isset($_ENV['POS_ID']) ? $_ENV['POS_ID'] : '300746';
$secondKey = isset($_ENV['SECOND_KEY']) ? $_ENV['SECOND_KEY'] : 'b6ca15b0d1020e8094d9b5f8d163db54';
$oAuthClientSecret = isset($_ENV['OAUTH_CLIENT_SECRET']) ? $_ENV['OAUTH_CLIENT_SECRET'] : '2ee86a66e5d97e3fadc400c9f19b065d';
$posAuthKey = isset($_ENV['POS_AUTH_KEY']) ? $_ENV['POS_AUTH_KEY'] : '300746';

$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true);
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true, $posAuthKey);

try {
$completeRequest = ['transactionReference' => 'J9R4JP3F2G160825GUEST000P01'];
/** @var CompletePurchaseResponse $response */
$response = $gateway->completePurchase($completeRequest);

echo "Is Successful: " . $response->isSuccessful() . PHP_EOL;
Expand All @@ -24,6 +27,8 @@
echo "PaymentId: " , $response->getTransactionReference() . PHP_EOL;
echo "Data: " . var_export($response->getData(), true) . PHP_EOL;

} catch (OpenPayU_Exception $e) {
dump($e->getMessage());
} catch (Exception $e) {
dump((string)$e);
}
12 changes: 7 additions & 5 deletions examples/test-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
use Omnipay\PayU\GatewayFactory;
use Symfony\Component\HttpFoundation\Request;

$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv = Dotenv\Dotenv::createImmutable(__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
$posId = isset($_ENV['POS_ID']) ? $_ENV['POS_ID'] : '300746';
$secondKey = isset($_ENV['SECOND_KEY']) ? $_ENV['SECOND_KEY'] : 'b6ca15b0d1020e8094d9b5f8d163db54';
$oAuthClientSecret = isset($_ENV['OAUTH_CLIENT_SECRET']) ? $_ENV['OAUTH_CLIENT_SECRET'] : '2ee86a66e5d97e3fadc400c9f19b065d';
$posAuthKey = isset($_ENV['POS_AUTH_KEY']) ? $_ENV['POS_AUTH_KEY'] : '300746';

$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true, $posAuthKey);

$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);
Expand Down
21 changes: 12 additions & 9 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

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

use Guzzle\Http\Exception\ClientErrorResponseException;
use Omnipay\PayU\GatewayFactory;
use Omnipay\PayU\Messages\PurchaseResponse;

$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv = Dotenv\Dotenv::createImmutable(__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';
$posId = isset($_ENV['POS_ID']) ? $_ENV['POS_ID'] : '300746';
$secondKey = isset($_ENV['SECOND_KEY']) ? $_ENV['SECOND_KEY'] : 'b6ca15b0d1020e8094d9b5f8d163db54';
$oAuthClientSecret = isset($_ENV['OAUTH_CLIENT_SECRET']) ? $_ENV['OAUTH_CLIENT_SECRET'] : '2ee86a66e5d97e3fadc400c9f19b065d';
$posAuthKey = isset($_ENV['POS_AUTH_KEY']) ? $_ENV['POS_AUTH_KEY'] : '300746';

$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true);
$gateway = GatewayFactory::createInstance($posId, $secondKey, $oAuthClientSecret, true, $posAuthKey);

try {
$orderNo = uniqid();
Expand Down Expand Up @@ -53,6 +54,7 @@
]
];

/** @var PurchaseResponse $response */
$response = $gateway->purchase($purchaseRequest);

echo "TransactionId: " . $response->getTransactionId() . PHP_EOL;
Expand All @@ -62,7 +64,8 @@

// Payment init OK, redirect to the payment gateway
echo $response->getRedirectUrl() . PHP_EOL;
} catch (ClientErrorResponseException $e) {
} catch (OpenPayU_Exception $e) {
dump($e->getMessage());
} catch (Exception $e) {
dump((string)$e);
dump($e->getResponse()->getBody(true));
}
}
108 changes: 46 additions & 62 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,79 @@
namespace Omnipay\PayU;

use Omnipay\Common\AbstractGateway;
use Omnipay\PayU\Messages\AccessTokenRequest;
use Omnipay\PayU\Messages\AccessTokenResponse;
use Omnipay\Common\Message\ResponseInterface;
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;
use OpenPayU_Configuration;
use OpenPayU_Exception;
use OpenPayU_Exception_Configuration;

class Gateway extends AbstractGateway
{
const URL_SANDBOX = 'https://secure.snd.payu.com';
const URL_PRODUCTION = 'https://secure.payu.com';
const ENV_TEST = 'sandbox';
const ENV_PRODUCTION = 'secure';

/**
* Get gateway display name
*/
public function getName()
public function getName(): string
{
return 'PayU';
}

/**
* @return AccessTokenResponse
* @param array $parameters
* @return $this|Gateway
* @throws OpenPayU_Exception_Configuration
*/
public function getAccessToken()
public function initialize(array $parameters = []): self
{
$request = parent::createRequest(AccessTokenRequest::class, [
'clientId' => $this->getParameter('posId'),
'clientSecret' => $this->getParameter('clientSecret'),
'apiUrl' => $this->getApiUrl()
]);
$response = $request->send();

return $response;
parent::initialize($parameters);

OpenPayU_Configuration::setEnvironment($this->getEnvironment());
OpenPayU_Configuration::setOauthGrantType('client_credentials');
OpenPayU_Configuration::setMerchantPosId($this->getParameter('posId'));
OpenPayU_Configuration::setSignatureKey($this->getParameter('secondKey'));
OpenPayU_Configuration::setOauthClientId($this->getParameter('posAuthKey'));
OpenPayU_Configuration::setOauthClientSecret($this->getParameter('clientSecret'));

return $this;
}

/**
* @param array $parameters
* @return PurchaseResponse
* @param array $options
* @throws OpenPayU_Exception
* @return PurchaseResponse|ResponseInterface
*/
public function purchase(array $parameters = [])
public function purchase(array $options = []): ResponseInterface
{
$this->setAccessToken($this->getAccessToken()->getAccessToken());
$request = parent::createRequest(PurchaseRequest::class, $parameters);
$response = $request->send();
$request = $this->createRequest(PurchaseRequest::class, $options);

return $response;
return $request->send();
}

/**
* @param array $parameters
* @return CompletePurchaseResponse
* @param array $options
* @throws OpenPayU_Exception
* @return CompletePurchaseResponse|ResponseInterface
*/
public function completePurchase(array $parameters = [])
public function completePurchase(array $options = []): ResponseInterface
{
$this->setAccessToken($this->getAccessToken()->getAccessToken());
$request = self::createRequest(CompletePurchaseRequest::class, $parameters);
$response = $request->send();
$request = $this->createRequest(CompletePurchaseRequest::class, $options);

return $response;
return $request->send();
}

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

/**
* @return string
*/
private function getApiUrl()
{
if ($this->getTestMode()) {
return self::URL_SANDBOX;
} else {
return self::URL_PRODUCTION;
}
}

public function getDefaultParameters()
/** @return array<string, mixed> */
public function getDefaultParameters(): array
{
return [
'posId' => '',
Expand All @@ -96,50 +89,41 @@ public function getDefaultParameters()
/**
* @param string $secondKey
*/
public function setSecondKey($secondKey)
public function setSecondKey(string $secondKey): void
{
$this->setParameter('secondKey', $secondKey);
}

/**
* @param string $posId
*/
public function setPosId($posId)
public function setPosId(string $posId): void
{
$this->setParameter('posId', $posId);
}

/**
* @param string $clientSecret
*/
public function setClientSecret($clientSecret)
public function setClientSecret(string $clientSecret): void
{
$this->setParameter('clientSecret', $clientSecret);
}

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

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

return $this;
}

private function setApiUrl($apiUrl)
private function getEnvironment(): string
{
$this->setParameter('apiUrl', $apiUrl);
}
if ($this->getTestMode()) {
return self::ENV_TEST;
}

private function setAccessToken($accessToken)
{
$this->setParameter('accessToken', $accessToken);
return self::ENV_PRODUCTION;
}
}
}
61 changes: 0 additions & 61 deletions src/Messages/AccessTokenRequest.php

This file was deleted.

Loading

0 comments on commit e9f9738

Please sign in to comment.