From d1a0790fa3af35d28444490c8ff220d729afe7c4 Mon Sep 17 00:00:00 2001 From: Jason Judge Date: Sat, 24 Nov 2018 23:04:39 +0000 Subject: [PATCH] Issue #6 throw exception on purchase+paypal+createCard --- src/AbstractDatatransGateway.php | 6 +-- src/Message/AbstractRedirectRequest.php | 19 +++++++ tests/Message/AuthorizeRequestTest.php | 68 +++++++++++++++++++++++++ tests/Message/PurchaseRequestTest.php | 49 ++++++++++++++++++ 4 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 tests/Message/AuthorizeRequestTest.php diff --git a/src/AbstractDatatransGateway.php b/src/AbstractDatatransGateway.php index a026034..c6921ae 100644 --- a/src/AbstractDatatransGateway.php +++ b/src/AbstractDatatransGateway.php @@ -33,8 +33,8 @@ abstract class AbstractDatatransGateway extends AbstractGateway implements Const public function getDefaultParameters() { return [ - 'merchantId' => '', - 'sign' => '', + 'merchantId' => null, + 'sign' => null, 'testMode' => true, 'returnMethod' => [ null, @@ -42,7 +42,7 @@ public function getDefaultParameters() self::RETURN_METHOD_GET, ], 'errorUrl' => null, - 'language' => [ + 'language' => [ null, // account default 'de', // German 'en', // English diff --git a/src/Message/AbstractRedirectRequest.php b/src/Message/AbstractRedirectRequest.php index c144aaa..9d415b1 100644 --- a/src/Message/AbstractRedirectRequest.php +++ b/src/Message/AbstractRedirectRequest.php @@ -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 */ @@ -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; diff --git a/tests/Message/AuthorizeRequestTest.php b/tests/Message/AuthorizeRequestTest.php new file mode 100644 index 0000000..5584048 --- /dev/null +++ b/tests/Message/AuthorizeRequestTest.php @@ -0,0 +1,68 @@ +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(); + } +} diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index b4762f1..9878ef4 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -4,6 +4,7 @@ use Omnipay\Common\CreditCard; use Omnipay\Tests\TestCase; +use Omnipay\Datatrans\Gateway; class PurchaseRequestTest extends TestCase { @@ -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(); + } }