Skip to content

Commit

Permalink
auto generate a default transaction id factory if necessary fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dsge committed Dec 10, 2016
1 parent df3b81b commit 4ec3e02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
1 change: 0 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ $gateway = Omnipay\Omnipay::create("\\".Clapp\OtpHu\Gateway::class);

$gateway->setShopId("0199123456");
$gateway->setPrivateKey(file_get_contents('myShopKey.privKey.pem'));
$gateway->setTransactionId('myGeneratedTransactionId');
$gateway->setReturnUrl("https://www.example.com/processing-your-payment");
$gateway->setTestMode(false);

Expand Down
13 changes: 11 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ public function purchase($options){
'returnUrl'
);
/**
* generáltassunk az OTP-vel transactionId-t, ha nem lenne nekünk
* if we did not provide a transactionID then generate one using the gateway
*/
if (empty($transactionId)){
if (empty($this->transactionIdFactory)){
throw new InvalidArgumentException('missing factory for auto generating transaction_id');
$this->transactionIdFactory = $this->getDefaultTransactionIdFactory();
}
$transactionId = $this->transactionIdFactory->generateTransactionId(array_merge($options, $this->getParameters()));
}
$this->setTransactionId($transactionId);
$request->setTransactionId($transactionId);

$request->validate(
'shop_id',
Expand All @@ -104,6 +105,14 @@ public function purchase($options){

return $request;
}
/**
* create the default TransactionIdFactory
*
* @return TransactionIdFactoryContract default TransactionIdFactory
*/
protected function getDefaultTransactionIdFactory(){
return new TransactionIdFactoryUsingPaymentGateway($this->httpClient);
}
/**
* Get the details of a transaction from the gateway, including whether or not it's already completed.
*
Expand Down
41 changes: 15 additions & 26 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Guzzle\Http\Client as HttpClient;
use Clapp\OtpHu\Contract\TransactionIdFactoryContract;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Http\Message\Response;

class GatewayTest extends TestCase{
public function testGatewayCreation(){
Expand Down Expand Up @@ -52,20 +54,6 @@ public function testReturnUrlGetter(){
$this->assertEquals($returnUrl, $gateway->getReturnUrl());
}

public function testMissingTransactionIdFactory(){
$gateway = Omnipay::create("\\".OtpHuGateway::class);
$gateway->setShopId($this->faker->randomNumber);
$gateway->setPrivateKey($this->getDummyRsaPrivateKey());
$gateway->setReturnUrl("https://www.example.com/processing-your-payment");

try {
$gateway->purchase([]);
}catch(InvalidArgumentException $e){
$this->setLastException($e);
}
$this->assertLastException(InvalidArgumentException::class);
}

public function testTransactionIdWithoutFactory(){
$gateway = Omnipay::create("\\".OtpHuGateway::class);

Expand Down Expand Up @@ -105,7 +93,12 @@ public function testTransactionIdWithoutFactoryOnGateway(){
}

public function testTransactionIdWithFactory(){
$gateway = Omnipay::create("\\".OtpHuGateway::class);
$plugin = new MockPlugin();
$plugin->addResponse(new Response(200, null, self::$successfulTransactionIdGenerationResponseBody));
$client = new HttpClient();
$client->addSubscriber($plugin);

$gateway = Omnipay::create("\\".OtpHuGateway::class, $client);
$gateway->setShopId($this->faker->randomNumber);
$gateway->setPrivateKey($this->getDummyRsaPrivateKey());
$gateway->setReturnUrl("https://www.example.com/processing-your-payment");
Expand All @@ -116,23 +109,19 @@ public function testTransactionIdWithFactory(){
])
->getMock();

$mockResponse = $this->getMockBuilder(GenerateTransactionIdRequest::class)
->setConstructorArgs([new HttpClient(), new HttpRequest()])
->getMock();

$generatedTransactionId = $this->faker->creditCardNumber;

$mock->expects($this->once())
->method('generateTransactionId')
->will($this->returnValue($mockResponse));
->will($this->returnValue($generatedTransactionId));

$gateway->setTransactionIdFactory($mock);
try{
$gateway->purchase([]);
}catch(InvalidRequestException $e){
$this->setLastException($e);
}
$this->assertLastException(InvalidRequestException::class);

$gateway->purchase([]);

$this->assertNotEmpty($gateway->getTransactionId());
$this->assertTrue(is_string($gateway->getTransactionId()));
$this->assertEquals($generatedTransactionId, $gateway->getTransactionId());

$this->assertTrue($gateway->getTransactionIdFactory() instanceof TransactionIdFactoryContract);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UsageExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testUsageExamplePurchase(){
* mock
*/
$plugin = new MockPlugin();
$plugin->addResponse(new Response(200, null, self::$successfulTransactionIdGenerationResponseBody));
$plugin->addResponse(new Response(200, null, self::$successfulPurchaseResponseBody));
$client = new HttpClient();
$client->addSubscriber($plugin);
Expand All @@ -25,7 +26,6 @@ public function testUsageExamplePurchase(){

$gateway->setShopId("0199123456");
$gateway->setPrivateKey($this->getDummyRsaPrivateKey());
$gateway->setTransactionId('myGeneratedTransactionId');
$gateway->setReturnUrl("https://www.example.com/processing-your-payment");
$gateway->setTestMode(false);

Expand Down

0 comments on commit 4ec3e02

Please sign in to comment.