Skip to content

Commit c86ba96

Browse files
committed
Fixed stored card issues and did some refactoring
1 parent a0b42b6 commit c86ba96

File tree

2 files changed

+127
-97
lines changed

2 files changed

+127
-97
lines changed

src/Message/BaseRequest.php

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,84 @@ public function getMerchantReference()
4747
return $this->getParameter('merchantReference');
4848
}
4949

50-
// Store Card Options
50+
public function setType($value)
51+
{
52+
return $this->setParameter('type', $value);
53+
}
54+
55+
public function getType()
56+
{
57+
return $this->getParameter('type') ?? 'purchase';
58+
}
59+
60+
public function setLanguage($value)
61+
{
62+
return $this->setParameter('language', $value);
63+
}
64+
65+
public function getLanguage()
66+
{
67+
return $this->getParameter('language') ?? 'en';
68+
}
69+
70+
/**
71+
* @param $list
72+
* Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac', 'unionpay', 'oxipay', 'visacheckout', 'wechat']
73+
*
74+
* @return PurchaseRequest
75+
*/
76+
public function setPaymentMethods($list)
77+
{
78+
$options = [
79+
'card', 'account2account', 'alipay', 'applepay',
80+
'googlepay', 'paypal', 'interac', 'unionpay',
81+
'oxipay', 'visacheckout', 'wechat'
82+
];
83+
84+
foreach ( $list as $method ) {
85+
if ( !in_array($method, $options) ) {
86+
throw new InvalidRequestException("Unknown payment method: {$method}");
87+
}
88+
}
89+
90+
return $this->setParameter('paymentMethods', $list);
91+
}
92+
93+
public function getPaymentMethods()
94+
{
95+
return $this->getParameter('paymentMethods');
96+
}
97+
98+
public function setCardTypes($list)
99+
{
100+
return $this->setParameter('cardTypes', $list);
101+
}
102+
103+
public function getCardTypes()
104+
{
105+
return $this->getParameter('cardTypes');
106+
}
107+
108+
public function setExpiresAt($value)
109+
{
110+
return $this->setParameter('expiresAt', $value);
111+
}
112+
113+
public function getExpiresAt()
114+
{
115+
return $this->getParameter('expiresAt');
116+
}
117+
118+
public function setDeclineUrl($url)
119+
{
120+
return $this->setParameter('declineUrl', $url);
121+
}
122+
123+
public function getDeclineUrl()
124+
{
125+
return $this->getParameter('declineUrl');
126+
}
127+
51128
public function setStoreCard($value)
52129
{
53130
return $this->setParameter('storeCard', $value);
@@ -63,23 +140,22 @@ public function setStoredCardIndicator($value)
63140
$options = [
64141
'single', 'recurringfixed', 'recurringvariable', 'installment',
65142
'recurringnoexpiry', 'recurringinitial', 'installmentinitial', 'credentialonfileinitial',
66-
'unscheduledcredentialonfileinitial', 'credentialonfile', 'unscheduledcredentialonfile',
67-
'incremental', 'resubmission', 'reauthorisation', 'delayedcharges', 'noshow'
143+
'unscheduledcredentialonfileinitial', 'credentialonfile', 'unscheduledcredentialonfile', 'incremental',
144+
'resubmission', 'reauthorisation', 'delayedcharges', 'noshow'
68145
];
69146

70-
if (!in_array($value, $options)) {
147+
if ( ! in_array($value, $options) ) {
71148
throw new InvalidRequestException("Invalid option '{$value}' set for StoredCardIndicator.");
72149
}
73150

74-
return $this->setParameter('storeCardIndicator', $value);
151+
return $this->setParameter('storedCardIndicator', $value);
75152
}
76153

77154
public function getStoredCardIndicator()
78155
{
79-
return $this->getParameter('storeCardIndicator');
156+
return $this->getParameter('storedCardIndicator');
80157
}
81158

82-
// Metadata
83159
public function setMetadata($data)
84160
{
85161
return $this->setParameter('metaData', $data);
@@ -90,7 +166,7 @@ public function getMetadata()
90166
return $this->getParameter('metaData');
91167
}
92168

93-
// Recurring Frequency
169+
94170
public function setRecurringFrequency($value)
95171
{
96172
$options = [
@@ -100,7 +176,7 @@ public function setRecurringFrequency($value)
100176
'twomonthly', 'threemonthly', 'fourmonthly', 'sixmonthly', 'annually'
101177
];
102178

103-
if (!in_array($value, $options)) {
179+
if ( ! in_array($value, $options) ) {
104180
throw new InvalidRequestException("Invalid option '{$value}' set for RecurringFrequency.");
105181
}
106182

@@ -112,6 +188,19 @@ public function getRecurringFrequency()
112188
return $this->getParameter('recurringFrequency');
113189
}
114190

191+
public function setRecurringExpiry($value)
192+
{
193+
// For scenarios where no expiry/end date is established i.e.,
194+
// subscription payments, the merchant web application should use "9999-12-31" as the value.
195+
196+
return $this->setParameter('recurringExpiry', $value);
197+
}
198+
199+
public function getRecurringExpiry()
200+
{
201+
return $this->getParameter('recurringExpiry');
202+
}
203+
115204
// Endpoint selection based on test mode
116205
protected function getEndpoint($path = '')
117206
{

src/Message/PurchaseRequest.php

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -14,125 +14,66 @@ public function initialize(array $parameters = [])
1414
return parent::initialize($parameters);
1515
}
1616

17-
// Purchase type, language, and payment method specifics
18-
public function setType($value)
19-
{
20-
return $this->setParameter('type', $value);
21-
}
22-
23-
public function getType()
24-
{
25-
return $this->getParameter('type') ?? 'purchase';
26-
}
27-
28-
public function setLanguage($value)
29-
{
30-
return $this->setParameter('language', $value);
31-
}
32-
33-
public function getLanguage()
34-
{
35-
return $this->getParameter('language') ?? 'en';
36-
}
37-
38-
/**
39-
* @param array $list
40-
* Possible methods: ['card', 'account2account', 'alipay', 'applepay', 'googlepay', 'paypal', 'interac', 'unionpay', 'oxipay', 'visacheckout', 'wechat']
41-
*
42-
* @return PurchaseRequest
43-
*/
44-
public function setPaymentMethods($list)
45-
{
46-
$options = [
47-
'card', 'account2account', 'alipay', 'applepay',
48-
'googlepay', 'paypal', 'interac', 'unionpay',
49-
'oxipay', 'visacheckout', 'wechat'
50-
];
51-
52-
foreach ($list as $method) {
53-
if (!in_array($method, $options)) {
54-
throw new InvalidRequestException("Unknown payment method: {$method}");
55-
}
56-
}
57-
58-
return $this->setParameter('paymentMethods', $list);
59-
}
60-
61-
public function getPaymentMethods()
62-
{
63-
return $this->getParameter('paymentMethods');
64-
}
65-
66-
public function setCardTypes($list)
67-
{
68-
return $this->setParameter('cardTypes', $list);
69-
}
70-
71-
public function getCardTypes()
72-
{
73-
return $this->getParameter('cardTypes');
74-
}
75-
76-
public function setExpiresAt($value)
77-
{
78-
return $this->setParameter('expiresAt', $value);
79-
}
80-
81-
public function getExpiresAt()
82-
{
83-
return $this->getParameter('expiresAt');
84-
}
85-
86-
public function setDeclineUrl($url)
87-
{
88-
return $this->setParameter('declineUrl', $url);
89-
}
90-
91-
public function getDeclineUrl()
92-
{
93-
return $this->getParameter('declineUrl');
94-
}
95-
9617
public function getData()
9718
{
9819
$this->validate('apiUsername', 'apiKey', 'amount', 'currency');
9920

10021
$data = [];
22+
10123
$data['type'] = $this->getType();
10224
$data['amount'] = $this->getAmount();
10325
$data['currency'] = $this->getCurrency();
104-
$data['storeCard'] = (bool)$this->getStoreCard() ?? false;
10526
$data['callbackUrls'] = [];
10627

107-
if (is_array($this->getPaymentMethods())) {
28+
if ( $this->getStoreCard() ) {
29+
$data['storeCard'] = true;
30+
}
31+
32+
if ( $this->getStoredCardIndicator() ) {
33+
$data['storedCardIndicator'] = $this->getStoredCardIndicator();
34+
}
35+
36+
if ( $this->getRecurringExpiry() ) {
37+
$data['recurringExpiry'] = $this->getRecurringExpiry();
38+
}
39+
40+
if ( $this->getRecurringFrequency() ) {
41+
$data['recurringFrequency'] = $this->getRecurringFrequency();
42+
}
43+
44+
if ( $this->getToken() ) {
45+
$data['cardId'] = $this->getToken();
46+
}
47+
48+
if ( is_array($this->getPaymentMethods()) ) {
10849
$data['methods'] = $this->getPaymentMethods();
10950
}
11051

111-
if (is_array($this->getCardTypes())) {
52+
if ( is_array($this->getCardTypes()) ) {
11253
$data['cardTypes'] = $this->getCardTypes();
11354
}
11455

115-
if (is_array($this->getMetadata())) {
56+
if ( is_array($this->getMetadata()) ) {
11657
$data['metaData'] = $this->getMetadata();
11758
}
11859

119-
if ($this->getMerchantReference()) {
60+
if ( $this->getMerchantReference() ) {
12061
$data['merchantReference'] = $this->getMerchantReference();
12162
}
12263

123-
if ($this->getReturnUrl()) {
64+
if ( $this->getReturnUrl() ) {
12465
$data['callbackUrls']['approved'] = $this->getReturnUrl();
12566
}
12667

127-
if ($this->getDeclineUrl()) {
68+
if ( $this->getDeclineUrl() ) {
12869
$data['callbackUrls']['declined'] = $this->getDeclineUrl();
12970
}
13071

131-
if ($this->getCancelUrl()) {
72+
if ( $this->getCancelUrl() ) {
13273
$data['callbackUrls']['cancelled'] = $this->getCancelUrl();
13374
}
13475

135-
if ($this->getNotifyUrl()) {
76+
if ( $this->getNotifyUrl() ) {
13677
$data['notificationUrl'] = $this->getNotifyUrl();
13778
}
13879

0 commit comments

Comments
 (0)