Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from comicrelief/gateway-username-fix
Browse files Browse the repository at this point in the history
Use custom usernames from Gateway credentials
  • Loading branch information
ayanozturk authored Oct 6, 2017
2 parents 06284ee + 3ba72ad commit 5c9b495
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/Omnipay/WorldpayCGHosted/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ public function setPaResponse($value)
return $this->setParameter('pa_response', $value);
}

/**
* Get the separate username if configured (more secure approach for basic auth) or fallback to merchant if not
*
* @return string
*/
public function getUsername()
{
return $this->parameters->get('username', $this->getParameter('merchant'));
}

/**
* Set basic auth username
*
* @param string $value
* @return Gateway
*/
public function setUsername($value)
{
return $this->setParameter('username', $value);
}

/**
* Get password
*
Expand Down
29 changes: 20 additions & 9 deletions tests/Omnipay/WorldpayCGHosted/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function setUp()
$this->getHttpClient(),
$this->getHttpRequest()
);
$this->gateway->setMerchant('ACMECO');
$this->gateway->setTestMode(true);
$this->gateway->setInstallation('ABC123');

$this->parameters = [
'amount' => 7.45,
Expand Down Expand Up @@ -60,7 +63,6 @@ public function testPurchaseSuccess()

$purchase = $this->gateway->purchase($this->parameters);
$purchase->setCard($this->card);
$purchase->setTestMode(true);
$response = $purchase->send();

$this->assertTrue($response->isRedirect());
Expand All @@ -75,7 +77,6 @@ public function testPurchaseError()

$purchase = $this->gateway->purchase($this->parameters);
$purchase->setCard($this->card);
$purchase->setTestMode(true);
$response = $purchase->send();

$this->assertFalse($response->isSuccessful());
Expand All @@ -88,18 +89,17 @@ public function testPurchaseError()
public function testMissingCard()
{
$purchase = $this->gateway->purchase($this->parameters);
$purchase->setTestMode(true);
$purchase->send();
}

public function testPurchaseRequestDataSetUp()
{
$purchase = $this->gateway->purchase($this->parameters);
$purchase->setTestMode(true);
$purchase->setInstallation('ABC123');
$purchase->setMerchant('ACMECO');
$purchase->setCard($this->card);

// Confirm basic auth uses merchant code to authenticate when there's no username.
$this->assertEquals('ACMECO', $purchase->getUsername());

/** @var \SimpleXMLElement $data */
$data = $purchase->getData();
/** @var \SimpleXMLElement $order */
Expand Down Expand Up @@ -131,6 +131,20 @@ public function testPurchaseRequestDataSetUp()
$this->assertEquals($this->parameters['notifyUrl'], $purchase->getNotifyUrl());
}

/**
* Confirm basic auth uses a username when set rather than merchant code.
*/
public function testUsernameAuthSetup()
{
$gatewayWithUsername = clone $this->gateway;
$gatewayWithUsername->setUsername('MYSECRETUSERNAME987');

$purchase = $gatewayWithUsername->purchase($this->parameters);
$purchase->setCard($this->card);

$this->assertEquals('MYSECRETUSERNAME987', $purchase->getUsername());
}

/**
* Billing address is required with default settings
*
Expand All @@ -144,9 +158,6 @@ public function testMissingAddressWithDefaultLogic()
]);

$purchase = $this->gateway->purchase($this->parameters);
$purchase->setTestMode(true);
$purchase->setInstallation('ABC123');
$purchase->setMerchant('ACMECO');
$purchase->setCard($cardWithoutAddress);

$purchase->getData();
Expand Down

0 comments on commit 5c9b495

Please sign in to comment.