Skip to content

Commit

Permalink
Issue #6 throw exception on purchase+paypal+createCard
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Nov 24, 2018
1 parent d7e40e9 commit d1a0790
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/AbstractDatatransGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ abstract class AbstractDatatransGateway extends AbstractGateway implements Const
public function getDefaultParameters()
{
return [
'merchantId' => '',
'sign' => '',
'merchantId' => null,
'sign' => null,
'testMode' => true,
'returnMethod' => [
null,
self::RETURN_METHOD_POST,
self::RETURN_METHOD_GET,
],
'errorUrl' => null,
'language' => [
'language' => [
null, // account default
'de', // German
'en', // English
Expand Down
19 changes: 19 additions & 0 deletions src/Message/AbstractRedirectRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
use Omnipay\Common\Message\ResponseInterface;
use Omnipay\Datatrans\Traits\HasGatewayParameters;
use Omnipay\Datatrans\Gateway;
use Omnipay\Datatrans\Interfaces\Constants;
use Omnipay\Common\Exception\InvalidRequestException;

abstract class AbstractRedirectRequest extends AbstractRequest
{
/**
* @var string NOA or CAA (null to use account default)
*/
protected $requestType = Constants::REQTYPE_AUTHORIZE;

/**
* @return array
*/
Expand Down Expand Up @@ -152,6 +159,18 @@ public function getData()
// be used repeatedly to authorise payments against.

if ($this->getPaymentMethod() === Gateway::PAYMENT_METHOD_PAP) {
if ($this->requestType === Constants::REQTYPE_PURCHASE) {
// PayPal switches to authorize only if requesting an
// order ID. If we are expecting a purchase, then throw
// an exception to avoid an unexpected result.

throw new InvalidRequestException(sprintf(
'Invalid: cannot use request type %s with method %s and option createCard',
Constants::REQTYPE_PURCHASE,
Gateway::PAYMENT_METHOD_PAP
));
}

$this->setPayPalOrderId(true);
} else {
$data['useAlias'] = Gateway::USE_ALIAS;
Expand Down
68 changes: 68 additions & 0 deletions tests/Message/AuthorizeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Omnipay\Datatrans\Message;

use Omnipay\Common\CreditCard;
use Omnipay\Tests\TestCase;
use Omnipay\Datatrans\Gateway;

class AuthorizeRequestTest extends TestCase
{
/**
* @var PurchaseRequest
*/
private $request;

public function setUp()
{
parent::setUp();

$this->request = new AuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
}

/**
* See https://github.com/academe/omnipay-datatrans/issues/6
*/
public function testPayPalPurchaseCreateCardSucceeds()
{
$this->request->initialize(array(
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
'createCard' => true,
//
'merchantId' => 'asdf',
'sign' => '123',
'testMode' => true,
'amount' => '12.00',
'currency' => 'CHF',
'transactionId' => '123',
'returnUrl' => 'https://www.example.com/success',
'errorUrl' => 'https://www.example.com/error',
'cancelUrl' => 'https://www.example.com/cancel'
));

$this->request->send();
}

/**
* See https://github.com/academe/omnipay-datatrans/issues/6
*/
public function testPayPalPurchaseNonCreateCardSucceeds()
{
$this->request->initialize(array(
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
'createCard' => false,
//
'merchantId' => 'asdf',
'sign' => '123',
'testMode' => true,
'amount' => '12.00',
'currency' => 'CHF',
'transactionId' => '123',
'returnUrl' => 'https://www.example.com/success',
'errorUrl' => 'https://www.example.com/error',
'cancelUrl' => 'https://www.example.com/cancel'
));

$this->request->send();
}
}
49 changes: 49 additions & 0 deletions tests/Message/PurchaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\Common\CreditCard;
use Omnipay\Tests\TestCase;
use Omnipay\Datatrans\Gateway;

class PurchaseRequestTest extends TestCase
{
Expand Down Expand Up @@ -78,4 +79,52 @@ public function testErrorUrlDefaults()

$this->assertEquals($expected, $this->request->getData());
}

/**
* See https://github.com/academe/omnipay-datatrans/issues/6
*
* @expectedException Omnipay\Common\Exception\InvalidRequestException
*/
public function testPayPalPurchaseCreateCardFails()
{
$this->request->initialize(array(
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
'createCard' => true,
//
'merchantId' => 'asdf',
'sign' => '123',
'testMode' => true,
'amount' => '12.00',
'currency' => 'CHF',
'transactionId' => '123',
'returnUrl' => 'https://www.example.com/success',
'errorUrl' => 'https://www.example.com/error',
'cancelUrl' => 'https://www.example.com/cancel'
));

$this->request->send();
}

/**
* See https://github.com/academe/omnipay-datatrans/issues/6
*/
public function testPayPalPurchaseNonCreateCardSucceeds()
{
$this->request->initialize(array(
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
'createCard' => false,
//
'merchantId' => 'asdf',
'sign' => '123',
'testMode' => true,
'amount' => '12.00',
'currency' => 'CHF',
'transactionId' => '123',
'returnUrl' => 'https://www.example.com/success',
'errorUrl' => 'https://www.example.com/error',
'cancelUrl' => 'https://www.example.com/cancel'
));

$this->request->send();
}
}

0 comments on commit d1a0790

Please sign in to comment.