Skip to content

Commit d1a0790

Browse files
committed
Issue #6 throw exception on purchase+paypal+createCard
1 parent d7e40e9 commit d1a0790

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

src/AbstractDatatransGateway.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ abstract class AbstractDatatransGateway extends AbstractGateway implements Const
3333
public function getDefaultParameters()
3434
{
3535
return [
36-
'merchantId' => '',
37-
'sign' => '',
36+
'merchantId' => null,
37+
'sign' => null,
3838
'testMode' => true,
3939
'returnMethod' => [
4040
null,
4141
self::RETURN_METHOD_POST,
4242
self::RETURN_METHOD_GET,
4343
],
4444
'errorUrl' => null,
45-
'language' => [
45+
'language' => [
4646
null, // account default
4747
'de', // German
4848
'en', // English

src/Message/AbstractRedirectRequest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
use Omnipay\Common\Message\ResponseInterface;
2525
use Omnipay\Datatrans\Traits\HasGatewayParameters;
2626
use Omnipay\Datatrans\Gateway;
27+
use Omnipay\Datatrans\Interfaces\Constants;
28+
use Omnipay\Common\Exception\InvalidRequestException;
2729

2830
abstract class AbstractRedirectRequest extends AbstractRequest
2931
{
32+
/**
33+
* @var string NOA or CAA (null to use account default)
34+
*/
35+
protected $requestType = Constants::REQTYPE_AUTHORIZE;
36+
3037
/**
3138
* @return array
3239
*/
@@ -152,6 +159,18 @@ public function getData()
152159
// be used repeatedly to authorise payments against.
153160

154161
if ($this->getPaymentMethod() === Gateway::PAYMENT_METHOD_PAP) {
162+
if ($this->requestType === Constants::REQTYPE_PURCHASE) {
163+
// PayPal switches to authorize only if requesting an
164+
// order ID. If we are expecting a purchase, then throw
165+
// an exception to avoid an unexpected result.
166+
167+
throw new InvalidRequestException(sprintf(
168+
'Invalid: cannot use request type %s with method %s and option createCard',
169+
Constants::REQTYPE_PURCHASE,
170+
Gateway::PAYMENT_METHOD_PAP
171+
));
172+
}
173+
155174
$this->setPayPalOrderId(true);
156175
} else {
157176
$data['useAlias'] = Gateway::USE_ALIAS;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Omnipay\Datatrans\Message;
4+
5+
use Omnipay\Common\CreditCard;
6+
use Omnipay\Tests\TestCase;
7+
use Omnipay\Datatrans\Gateway;
8+
9+
class AuthorizeRequestTest extends TestCase
10+
{
11+
/**
12+
* @var PurchaseRequest
13+
*/
14+
private $request;
15+
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
20+
$this->request = new AuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
21+
}
22+
23+
/**
24+
* See https://github.com/academe/omnipay-datatrans/issues/6
25+
*/
26+
public function testPayPalPurchaseCreateCardSucceeds()
27+
{
28+
$this->request->initialize(array(
29+
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
30+
'createCard' => true,
31+
//
32+
'merchantId' => 'asdf',
33+
'sign' => '123',
34+
'testMode' => true,
35+
'amount' => '12.00',
36+
'currency' => 'CHF',
37+
'transactionId' => '123',
38+
'returnUrl' => 'https://www.example.com/success',
39+
'errorUrl' => 'https://www.example.com/error',
40+
'cancelUrl' => 'https://www.example.com/cancel'
41+
));
42+
43+
$this->request->send();
44+
}
45+
46+
/**
47+
* See https://github.com/academe/omnipay-datatrans/issues/6
48+
*/
49+
public function testPayPalPurchaseNonCreateCardSucceeds()
50+
{
51+
$this->request->initialize(array(
52+
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
53+
'createCard' => false,
54+
//
55+
'merchantId' => 'asdf',
56+
'sign' => '123',
57+
'testMode' => true,
58+
'amount' => '12.00',
59+
'currency' => 'CHF',
60+
'transactionId' => '123',
61+
'returnUrl' => 'https://www.example.com/success',
62+
'errorUrl' => 'https://www.example.com/error',
63+
'cancelUrl' => 'https://www.example.com/cancel'
64+
));
65+
66+
$this->request->send();
67+
}
68+
}

tests/Message/PurchaseRequestTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Omnipay\Common\CreditCard;
66
use Omnipay\Tests\TestCase;
7+
use Omnipay\Datatrans\Gateway;
78

89
class PurchaseRequestTest extends TestCase
910
{
@@ -78,4 +79,52 @@ public function testErrorUrlDefaults()
7879

7980
$this->assertEquals($expected, $this->request->getData());
8081
}
82+
83+
/**
84+
* See https://github.com/academe/omnipay-datatrans/issues/6
85+
*
86+
* @expectedException Omnipay\Common\Exception\InvalidRequestException
87+
*/
88+
public function testPayPalPurchaseCreateCardFails()
89+
{
90+
$this->request->initialize(array(
91+
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
92+
'createCard' => true,
93+
//
94+
'merchantId' => 'asdf',
95+
'sign' => '123',
96+
'testMode' => true,
97+
'amount' => '12.00',
98+
'currency' => 'CHF',
99+
'transactionId' => '123',
100+
'returnUrl' => 'https://www.example.com/success',
101+
'errorUrl' => 'https://www.example.com/error',
102+
'cancelUrl' => 'https://www.example.com/cancel'
103+
));
104+
105+
$this->request->send();
106+
}
107+
108+
/**
109+
* See https://github.com/academe/omnipay-datatrans/issues/6
110+
*/
111+
public function testPayPalPurchaseNonCreateCardSucceeds()
112+
{
113+
$this->request->initialize(array(
114+
'paymentMethod' => Gateway::PAYMENT_METHOD_PAP,
115+
'createCard' => false,
116+
//
117+
'merchantId' => 'asdf',
118+
'sign' => '123',
119+
'testMode' => true,
120+
'amount' => '12.00',
121+
'currency' => 'CHF',
122+
'transactionId' => '123',
123+
'returnUrl' => 'https://www.example.com/success',
124+
'errorUrl' => 'https://www.example.com/error',
125+
'cancelUrl' => 'https://www.example.com/cancel'
126+
));
127+
128+
$this->request->send();
129+
}
81130
}

0 commit comments

Comments
 (0)