Skip to content

Commit

Permalink
Merge 77d6c87 into f656821
Browse files Browse the repository at this point in the history
  • Loading branch information
Attanon committed Sep 26, 2015
2 parents f656821 + 77d6c87 commit b8d61e7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,43 @@ $storeIdCallback = function ($paymentId) use ($order) {
```

A nakonec s platbou zaplatíte :) (takto, druhý parametr je platební kanál,
kterým má být platba uskutečněna):
kterým má být platba uskutečněna, čtvrtý parametr udává, jestli platbu budete uskutečňovat pomocí
nové INLINE platební brány a nebo pomocí starého přesměrování):

```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í
Akce `pay()` vrátí `Response` objekt v případě, že se čtvrtý parametr nastaví na PAYMENT_REDIRECT, který aplikaci přesměruje na platební
bránu Gopay.

```php
$this->sendResponse($response);
```

V případě že zavoláte funkci `pay()` s čtvrtým parametrem PAYMENT_INLINE, tak Vám akce `pay()` vrátí pole podobné tomuto

```php
[
"url" => "https://gate.gopay.cz/gw/v3/3100000099",
"signature" => "25ee53a1ec­cc253a8310f5267d2de6b483f58a­f9676d883e26600ce3316ai"
];
```

Což Vám vrátí vše potřebné k implementaci nové Inline platební brány.

A 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)

Pro příklad je to tento formulář

```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>
```

V okamžiku zavolání `pay()` se mohou pokazit dvě věci:

1. Někde jsou poskytnuty špatné parametry
Expand All @@ -196,7 +220,7 @@ straně.

```php
try {
$gopay->pay($payment, $gopay::TRANSFER);
$gopay->pay($payment, $gopay::TRANSFER, $storeIdCallback, $gopay::PAYMENT_REDIRECT);
} 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": "dev-master",
"nette/utils": "~2.2",
"nette/forms": "~2.2",
"nette/application": "~2.2"
Expand Down
63 changes: 61 additions & 2 deletions src/Gopay/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Service extends Nette\Object
/** @const Platbu vybere uživatel */
const METHOD_USER_SELECT = NULL;


/** @const Czech koruna */
const CURRENCY_CZK = 'CZK';
/** @const Euro */
Expand Down Expand Up @@ -382,13 +381,73 @@ public function pay(Payment $payment, $channel, $callback)
throw new GopayException($e->getMessage(), 0, $e);
}

Nette\Utils\Callback::invokeArgs($callback, array($paymentSessionId));

$url = GopayConfig::fullIntegrationURL()
. "?sessionInfo.targetGoId=" . $this->gopayId
. "&sessionInfo.paymentSessionId=" . $paymentSessionId
. "&sessionInfo.encryptedSignature=" . $this->createSignature($paymentSessionId);

return $response = 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)
{
if ($payment instanceof ReturnedPayment) {
throw new \InvalidArgumentException("Cannot use instance of 'ReturnedPayment'! This payment has been already used for paying");
}

if (!isset($this->channels[$channel]) && $channel !== self::METHOD_USER_SELECT) {
throw new \InvalidArgumentException("Payment channel '$channel' is not supported");
}

try {
$customer = $payment->getCustomer();
$paymentSessionId = $this->soap->createPayment(
$this->gopayId,
$payment->getProductName(),
$payment->getSumInCents(),
$payment->getCurrency(),
$payment->getVariable(),
$this->successUrl,
$this->failureUrl,
array_keys($this->channels),
$channel,
$this->gopaySecretKey,
$customer->firstName,
$customer->lastName,
$customer->city,
$customer->street,
$customer->postalCode,
$customer->countryCode,
$customer->email,
$customer->phoneNumber,
NULL, NULL, NULL, NULL,
$this->lang
);
} catch(\Exception $e) {
throw new GopayException($e->getMessage(), 0, $e);
}

Nette\Utils\Callback::invokeArgs($callback, array($paymentSessionId));
return new RedirectResponse($url);

$response = array(
"url" => GopayConfig::fullNewIntegrationURL() . '/' . $paymentSessionId,
"signature" => $this->createSignature($paymentSessionId)
);

return $response;
}


Expand Down
28 changes: 28 additions & 0 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

0 comments on commit b8d61e7

Please sign in to comment.