Skip to content

Commit

Permalink
Issue #8 Alternative hashing tests. Some additional documentation on …
Browse files Browse the repository at this point in the history
…frontend.
  • Loading branch information
judgej committed Apr 15, 2017
1 parent cedaa27 commit 3712f43
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 3 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,29 @@ in the page. The two things you need to build the form is the target URL, and th
The form items are supplied as name/value pairs.

```php
// This form needs javascript to auto-submit.
// This form needs javascript to auto-submit on page load.
echo '<form action="' . $response->getRedirectUrl() . '" method="POST" target="target-iframe">';
foreach($response->getRedirectData() as $name => $value) {
echo '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
echo '<input type="hidden" name="'.$name.'" value="'.htmlspecialchars($value).'" />';
}
echo '</form>';

// The autp-submitted form, tagetting at this iframe, will generate the
// remote credit card payment form within this iframe.
echo '<iframe name="target-iframe" width="400" height="650"></iframe>';
```

On return from the remote gateway, if using the iframe, you will need to break out
of the iframe to get to the final page in your merchant site. The PAYONE API does have
iframe-busting functionality built in. Set the `setTargetWindow()` on your authorize request
to tell the gateway where to take the user.
Accepted values are given in `ShopFrontendAuthorizeRequest::TARGET_WINDOW_*`.

Note that this driver does not attempt to generate HTML forms. It will instead give you the
data for creating your own HTML forms.

After the user has completed their details on the PAYONE site, a notification of the result
will be sent back to your merchant site, and then the user will be returned to either the
will be sent back to your merchant site, and then the user will be returned to either the
"success" page or the "failure" page. No data will be carried with that redirect, so the
transaction details must be retained in the session to match up with the results in
the notification back-channel.
Expand Down
220 changes: 220 additions & 0 deletions test/ShopFrontendGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php

namespace Omnipay\Payone;

use Omnipay\Tests\GatewayTestCase;

class ShopFrontendGatewayTest extends GatewayTestCase
{
public function setUp()
{
parent::setUp();

$this->gateway = new ShopFrontendGateway($this->getHttpClient(), $this->getHttpRequest());

$this->options = array(
'merchantId' => 12345678,
'subAccountId' => 12345,
'amount' => '10.00',
'card' => $this->getValidCard(),
);

$this->purchaseOptions = array(
'amount' => '10.00',
'currency' => 'GBP',
'transactionId' => '123',
'card' => $this->getValidCard(),
);

$this->captureOptions = array(
'amount' => '10.00',
'transactionId' => '123',
'transactionReference' => '????',
);
}

/**
* Generate a random value for a gateway parameter.
* The value generated will depend on the data type of
* the default parameter value.
*
* @parame mixed $default
* @return mixed
*/
protected function makeUnique($default)
{
switch (gettype($default)) {
case 'integer':
$value = rand(100000, 999999);
break;

case 'array':
$value = $default[array_rand($default)];
break;

case 'string':
default:
$value = uniqid();
break;
}

return $value;
}

/**
* Override parent test.
* See here for details, and remove when fixed:
* https://github.com/thephpleague/omnipay-tests/issues/10
*/
public function testDefaultParametersHaveMatchingMethods()
{
$settings = $this->gateway->getDefaultParameters();

foreach ($settings as $key => $default) {
$getter = 'get'.ucfirst($key);
$setter = 'set'.ucfirst($key);
$value = $this->makeUnique($default);
$this->assertTrue(method_exists($this->gateway, $getter), "Gateway must implement $getter()");
$this->assertTrue(method_exists($this->gateway, $setter), "Gateway must implement $setter()");

// setter must return instance
$this->assertSame($this->gateway, $this->gateway->$setter($value));
$this->assertSame($value, $this->gateway->$getter());
}
}

/**
* Override parent test.
* See here for details, and remove when fixed:
* https://github.com/thephpleague/omnipay-tests/issues/10
*/
public function testAuthorizeParameters()
{
if ($this->gateway->supportsAuthorize()) {
foreach ($this->gateway->getDefaultParameters() as $key => $default) {
// set property on gateway
$getter = 'get'.ucfirst($key);
$setter = 'set'.ucfirst($key);
$value = $this->makeUnique($default);
$this->gateway->$setter($value);

// request should have matching property, with correct value
$request = $this->gateway->authorize();
$this->assertSame($value, $request->$getter());
}
}
}

/**
* Override parent test.
* See here for details, and remove when fixed:
* https://github.com/thephpleague/omnipay-tests/issues/10
*/
public function testCaptureParameters()
{
if ($this->gateway->supportsCapture()) {
foreach ($this->gateway->getDefaultParameters() as $key => $default) {
// set property on gateway
$getter = 'get'.ucfirst($key);
$setter = 'set'.ucfirst($key);
$value = $this->makeUnique($default);
$this->gateway->$setter($value);

// request should have matching property, with correct value
$request = $this->gateway->capture();
$this->assertSame($value, $request->$getter());
}
}
}

/**
* Override parent test.
* See here for details, and remove when fixed:
* https://github.com/thephpleague/omnipay-tests/issues/10
*/
public function testPurchaseParameters()
{
foreach ($this->gateway->getDefaultParameters() as $key => $default) {
// set property on gateway
$getter = 'get'.ucfirst($key);
$setter = 'set'.ucfirst($key);
$value = $this->makeUnique($default);
$this->gateway->$setter($value);

// request should have matching property, with correct value
$request = $this->gateway->purchase();
$this->assertSame($value, $request->$getter());
}
}

/**
* Override parent test.
* See here for details, and remove when fixed:
* https://github.com/thephpleague/omnipay-tests/issues/10
*/
public function testVoidParameters()
{
if ($this->gateway->supportsVoid()) {
foreach ($this->gateway->getDefaultParameters() as $key => $default) {
// set property on gateway
$getter = 'get'.ucfirst($key);
$setter = 'set'.ucfirst($key);
$value = $this->makeUnique($default);
$this->gateway->$setter($value);

// request should have matching property, with correct value
$request = $this->gateway->void();
$this->assertSame($value, $request->$getter());
}
}
}

public function testAuthorizeHashSuccess()
{
$response = $this->gateway->authorize($this->purchaseOptions)->send();

// Success will be false because the transactino is not complete yet - the user
// needs to be presented with a form.
$this->assertFalse($response->isSuccessful());

// The redirect is for redirecting the full page OR into an iframe
$this->assertTrue($response->isRedirect());

$this->assertInternalType('array', $response->getRedirectData());

// TODO: validate the full $response->getRedirectData() array.
}

// Note: the following two tests rely on the request data not changing.
// They may fail if the OmniPay core test data (address, name etc) changes.

public function testAuthorizeHashMd5()
{
$this->gateway->setHashMethod('MD5');

$response = $this->gateway->authorize($this->purchaseOptions)->send();

$redirectData = $response->getRedirectData();

//
$this->assertArrayHasKey('hash', $redirectData);

// An MD5 hash is generated.
$this->assertSame('d6db6163f94a03997bde9e670cb9fddb', $redirectData['hash']);
}

public function testAuthorizeHashSha2()
{
$this->gateway->setHashMethod('SHA2_384');

$response = $this->gateway->authorize($this->purchaseOptions)->send();

$redirectData = $response->getRedirectData();

//
$this->assertArrayHasKey('hash', $redirectData);

// An SHA2-384 hash is generated.
$this->assertSame('7c27ce102dc1be6f59e30ff4ab436cd22d6430dd425544a10c46a88b38ad15fe87ea6ee8cc5c965095dcb279a7e1f0e0', $redirectData['hash']);
}
}

0 comments on commit 3712f43

Please sign in to comment.