Skip to content

Commit

Permalink
Merge 1edc4e6 into f656821
Browse files Browse the repository at this point in the history
  • Loading branch information
Attanon committed Oct 10, 2015
2 parents f656821 + 1edc4e6 commit be87239
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 15 deletions.
1 change: 1 addition & 0 deletions .empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
$response = $gopay->pay($payment, $gopay::METHOD_TRANSFER, $storeIdCallback, $gopay::PAYMENT_REDIRECT);
```

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
<form action="https://gate.gopay.cz/gw/v3/3100000099" method="post" id="gopay-payment-button">
<input type="hidden" name="signature" value="25ee53a1ec­cc253a8310f5267d2de6b483f58a­f9676d883e26600ce3316ai"/>
<button name="pay" type="submit">Zaplatit</button>
<script type="text/javascript" src="https://gate.gopay.cz/gp-gw/js/embed.js"></script>
</form>
```

#### 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
Expand All @@ -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.';
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 52 additions & 4 deletions src/Gopay/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,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");
Expand Down Expand Up @@ -378,21 +377,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
*
Expand Down
46 changes: 43 additions & 3 deletions tests/Gopay/ServiceTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -112,10 +140,18 @@ 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');
Assert::exception(function () use ($payment, $callback, $service) {
$service->pay($payment, Service::METHOD_CARD_GPKB, $callback);
$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->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");
}

public function testCallbackCalled()
Expand Down Expand Up @@ -149,6 +185,10 @@ class ServiceTest extends BaseTestCase
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);
}

}
Expand Down

0 comments on commit be87239

Please sign in to comment.