From 36e25634f929672a7639046a98a1e67117da82f1 Mon Sep 17 00:00:00 2001 From: Jakub Vrchota Date: Sun, 2 Aug 2015 19:13:24 +0200 Subject: [PATCH] Add way to get information for INLINE payment [closes #45] --- README.md | 40 ++++++++++++++++++++++---- composer.json | 2 +- src/Gopay/Service.php | 56 +++++++++++++++++++++++++++++++++--- tests/Gopay/ServiceTest.phpt | 50 +++++++++++++++++++++++++++++--- 4 files changed, 133 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 82ed004..f3574e4 100644 --- a/README.md +++ b/README.md @@ -169,22 +169,48 @@ $storeIdCallback = function ($paymentId) use ($order) { $order->setPaymentId($paymentId); }; ``` +Samotné placení lze provést dvěma způsoby. -A nakonec s platbou zaplatíte :) (takto, druhý parametr je platební kanál, -kterým má být platba uskutečněna): +### REDIRECT ```php $response = $gopay->pay($payment, $gopay::METHOD_TRANSFER, $storeIdCallback); ``` -Akce `pay()` vrátí `Response` objekt, který aplikaci přesměruje na platební -bránu Gopay. +Akce `pay()` vrátí `Response` objekt. ```php $this->sendResponse($response); ``` -V okamžiku zavolání `pay()` se mohou pokazit dvě věci: +### INLINE brána + +```php +$response = $gopay->payInline($payment, $gopay::METHOD_TRANSFER, $storeIdCallback); +``` + +Akce `payInline()` vám vrátí pole s klíči **url** a **signature**. + +```php +[ + "url" => "https://gate.gopay.cz/gw/v3/3100000099", + "signature" => "25ee53a1ec­cc253a8310f5267d2de6b483f58a­f9676d883e26600ce3316ai" +]; +``` + +Platební bránu je možné vytvořit pomocí formuláře, který najdete v [dokumentaci](https://help.gopay.com/cs/tema/integrace-platebni-brany/integrace-nova-platebni-brany/integrace-nove-platebni-brany-pro-stavajici-zakazniky). + +```html +
+ + + +
+``` + +#### Chyby s platbou + +V okamžiku zavolání `pay()` nebo `payInline()` se mohou pokazit dvě věci: 1. Někde jsou poskytnuty špatné parametry 2. Je pokažená oficiální platební brána Gopay @@ -196,7 +222,9 @@ straně. ```php try { - $gopay->pay($payment, $gopay::TRANSFER); + $gopay->pay($payment, $gopay::TRANSFER, $storeIdCallback); + // nebo + $gopay->payInline($payment, $gopay::TRANSFER, $storeIdCallback); } catch (GopayException $e) { echo 'Platební služba Gopay bohužel momentálně nefunguje. Zkuste to prosím za chvíli.'; diff --git a/composer.json b/composer.json index 9e449d4..c20539a 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ ], "require": { "php": ">=5.3.2", - "markette/gopay-api": "~2.5.0", + "markette/gopay-api": "~2.5.1", "nette/utils": "~2.2", "nette/forms": "~2.2", "nette/application": "~2.2" diff --git a/src/Gopay/Service.php b/src/Gopay/Service.php index 7a2d60f..25b5b15 100644 --- a/src/Gopay/Service.php +++ b/src/Gopay/Service.php @@ -349,17 +349,16 @@ public function restorePayment($values, $valuesToBeVerified) /** - * Executes payment via redirecting to GoPay payment gate + * Check and create payment * * @param Payment * @param string|null - * @param callback - * @return RedirectResponse + * @return int * @throws \InvalidArgumentException on undefined channel or provided ReturnedPayment * @throws GopayFatalException on maldefined parameters * @throws GopayException on failed communication with WS */ - public function pay(Payment $payment, $channel, $callback) + protected function createPaymentInternal(Payment $payment, $channel) { if ($payment instanceof ReturnedPayment) { throw new \InvalidArgumentException("Cannot use instance of 'ReturnedPayment'! This payment has been already used for paying"); @@ -399,21 +398,70 @@ public function pay(Payment $payment, $channel, $callback) NULL, NULL, NULL, NULL, $this->lang ); + + return $paymentSessionId; } catch(\Exception $e) { throw new GopayException($e->getMessage(), 0, $e); } + } + + + + /** + * Executes payment via redirecting to GoPay payment gate + * + * @param Payment + * @param string|null + * @param callback + * @return RedirectResponse + * @throws \InvalidArgumentException on undefined channel or provided ReturnedPayment + * @throws GopayFatalException on maldefined parameters + * @throws GopayException on failed communication with WS + */ + public function pay(Payment $payment, $channel, $callback) + { + $paymentSessionId = $this->createPaymentInternal($payment, $channel); + $url = GopayConfig::fullIntegrationURL() . "?sessionInfo.targetGoId=" . $this->gopayId . "&sessionInfo.paymentSessionId=" . $paymentSessionId . "&sessionInfo.encryptedSignature=" . $this->createSignature($paymentSessionId); Nette\Utils\Callback::invokeArgs($callback, array($paymentSessionId)); + return new RedirectResponse($url); } + /** + * Executes payment via INLINE GoPay payment gate + * + * @param Payment + * @param string|null + * @param callback + * @return RedirectResponse + * @throws \InvalidArgumentException on undefined channel or provided ReturnedPayment + * @throws GopayFatalException on maldefined parameters + * @throws GopayException on failed communication with WS + */ + public function payInline(Payment $payment, $channel, $callback) + { + $paymentSessionId = $this->createPaymentInternal($payment, $channel); + + $response = array( + "url" => GopayConfig::fullNewIntegrationURL() . '/' . $paymentSessionId, + "signature" => $this->createSignature($paymentSessionId) + ); + + Nette\Utils\Callback::invokeArgs($callback, array($paymentSessionId)); + + return $response; + } + + + /** * Binds payment buttons fo form * diff --git a/tests/Gopay/ServiceTest.phpt b/tests/Gopay/ServiceTest.phpt index b319b91..295c281 100644 --- a/tests/Gopay/ServiceTest.phpt +++ b/tests/Gopay/ServiceTest.phpt @@ -40,6 +40,34 @@ class ServiceTest extends BaseTestCase Assert::same(302, $response->getCode()); } + public function testPayInline() + { + $soap = Mockery::mock('Markette\Gopay\Api\GopaySoap'); + $soap->shouldReceive('createPayment')->once()->andReturn(3000000001); + + $payment = new Payment(array('sum' => 999, 'customer' => array())); + $callback = function ($id) { + }; + + $lang = Service::LANG_CS; + + $service = new Service($soap, 1234567890, 'fruC9a9e8ajuwrace4r3chaxu', TRUE); + $service->addChannel(Service::METHOD_CARD_GPKB, 'KB'); + $service->setLang($lang); + + $response = $service->payInline($payment, Service::METHOD_CARD_GPKB, $callback); + + Assert::type('array', $response); + Assert::count(2, $response); + Assert::same('https://testgw.gopay.cz/gw/v3/3000000001', + $response['url'] + ); + + Assert::same('999c4a90f42af5bdd9b5b7eaff43f27eb671b03a1efd4662b729dd21b9be41c22d5b25fe5955ff8d', + $response['signature'] + ); + } + public function testUrls() { $service = $this->createContainer('config.neon')->getService('gopay.service'); @@ -112,9 +140,17 @@ class ServiceTest extends BaseTestCase $service->pay($payment, 'nonexisting', $callback); }, '\InvalidArgumentException', "Payment channel 'nonexisting' is not supported"); - $payment = new ReturnedPayment(array('sum' => 999, 'customer' => array()), 1234567890, 'fruC9a9e8ajuwrace4r3chaxu'); + $paymentRet = new ReturnedPayment(array('sum' => 999, 'customer' => array()), 1234567890, 'fruC9a9e8ajuwrace4r3chaxu'); + Assert::exception(function () use ($paymentRet, $callback, $service) { + $service->pay($paymentRet, Service::METHOD_CARD_GPKB, $callback); + }, '\InvalidArgumentException', "Cannot use instance of 'ReturnedPayment'! This payment has been already used for paying"); + Assert::exception(function () use ($payment, $callback, $service) { - $service->pay($payment, Service::METHOD_CARD_GPKB, $callback); + $service->payInline($payment, 'nonexisting', $callback); + }, '\InvalidArgumentException', "Payment channel 'nonexisting' is not supported"); + + Assert::exception(function () use ($paymentRet, $callback, $service) { + $service->payInline($paymentRet, Service::METHOD_CARD_GPKB, $callback); }, '\InvalidArgumentException', "Cannot use instance of 'ReturnedPayment'! This payment has been already used for paying"); } @@ -146,8 +182,14 @@ class ServiceTest extends BaseTestCase $service = new Service($soap, 1234567890, 'fruC9a9e8ajuwrace4r3chaxu', TRUE); $service->addChannel(Service::METHOD_CARD_GPKB, 'KB'); - Assert::throws(function() use ($service, $payment) { - $response = $service->pay($payment, Service::METHOD_CARD_GPKB, function(){}); + Assert::throws(function () use ($service, $payment) { + $response = $service->pay($payment, Service::METHOD_CARD_GPKB, function () { + }); + }, 'Markette\Gopay\GopayException', $exmsg); + + Assert::throws(function () use ($service, $payment) { + $response = $service->payInline($payment, Service::METHOD_CARD_GPKB, function () { + }); }, 'Markette\Gopay\GopayException', $exmsg); }