Skip to content

Commit f36d4a1

Browse files
authored
Merge pull request #7 from firewizard/master
billing/shipping + other tweaks
2 parents 07bfab6 + 15b4f43 commit f36d4a1

File tree

3 files changed

+123
-25
lines changed

3 files changed

+123
-25
lines changed

src/DataTrait.php

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Adrianbarbos\Mobilpay;
44

5-
trait DataTrait {
6-
protected function initData() {
5+
use Omnipay\MobilPay\Api\Address;
6+
7+
trait DataTrait
8+
{
9+
protected function initData()
10+
{
711
$this->data = [
812
'orderId' => '',
913
'amount' => '',
@@ -13,15 +17,33 @@ protected function initData() {
1317
'returnUrl' => config('mobilpay.return_url'),
1418
'cancelUrl' => config('mobilpay.cancel_url'),
1519
'testMode' => config('mobilpay.testMode'),
16-
'params' => []
20+
'params' => []
1721
];
22+
23+
//ensure absolute urls
24+
foreach (['confirmUrl', 'returnUrl', 'cancelUrl'] as $var) {
25+
if ($this->isRelativeUrl($this->data[$var])) {
26+
$this->data[$var] = url($this->data[$var]);
27+
}
28+
}
29+
}
30+
31+
/**
32+
* @param string $url
33+
* @return bool
34+
*/
35+
protected function isRelativeUrl($url)
36+
{
37+
$url = parse_url($url);
38+
return empty($url['host']);
1839
}
1940

2041
/**
2142
* @param $value string
2243
* @return $this
2344
*/
24-
public function setOrderId($value) {
45+
public function setOrderId($value)
46+
{
2547
$this->data['orderId'] = $value;
2648

2749
return $this;
@@ -31,7 +53,8 @@ public function setOrderId($value) {
3153
* @param $value string
3254
* @return $this
3355
*/
34-
public function setAmount($value) {
56+
public function setAmount($value)
57+
{
3558
$this->data['amount'] = $value;
3659

3760
return $this;
@@ -41,7 +64,8 @@ public function setAmount($value) {
4164
* @param $value string
4265
* @return $this
4366
*/
44-
public function setCurrency($value) {
67+
public function setCurrency($value)
68+
{
4569
$this->data['currency'] = $value;
4670

4771
return $this;
@@ -51,7 +75,8 @@ public function setCurrency($value) {
5175
* @param $value string
5276
* @return $this
5377
*/
54-
public function setDetails($value) {
78+
public function setDetails($value)
79+
{
5580
$this->data['details'] = $value;
5681

5782
return $this;
@@ -61,7 +86,8 @@ public function setDetails($value) {
6186
* @param $value string
6287
* @return $this
6388
*/
64-
public function setConfirmUrl($value) {
89+
public function setConfirmUrl($value)
90+
{
6591
$this->data['confirmUrl'] = $value;
6692

6793
return $this;
@@ -71,7 +97,8 @@ public function setConfirmUrl($value) {
7197
* @param $value string
7298
* @return $this
7399
*/
74-
public function setReturnUrl($value) {
100+
public function setReturnUrl($value)
101+
{
75102
$this->data['returnUrl'] = $value;
76103

77104
return $this;
@@ -81,7 +108,8 @@ public function setReturnUrl($value) {
81108
* @param $value string
82109
* @return $this
83110
*/
84-
public function setCancelUrl($value) {
111+
public function setCancelUrl($value)
112+
{
85113
$this->data['cancelUrl'] = $value;
86114

87115
return $this;
@@ -91,7 +119,8 @@ public function setCancelUrl($value) {
91119
* @param $value boolean
92120
* @return $this
93121
*/
94-
public function setTestMode($value) {
122+
public function setTestMode($value)
123+
{
95124
$this->data['testMode'] = $value;
96125

97126
return $this;
@@ -101,9 +130,56 @@ public function setTestMode($value) {
101130
* @param array $value
102131
* @return $this
103132
*/
104-
public function setAdditionalParams(array $value) {
133+
public function setAdditionalParams(array $value)
134+
{
105135
$this->data['params'] = $value;
106136

107137
return $this;
108138
}
139+
140+
/**
141+
* @param array $value
142+
* @return $this
143+
*/
144+
public function setBillingAddress(array $value)
145+
{
146+
$this->data['billingAddress'] = $this->ensureAddressDefaults($value);
147+
148+
return $this;
149+
}
150+
151+
/**
152+
* @param array $value
153+
* @return $this
154+
*/
155+
public function setShippingAddress(array $value)
156+
{
157+
$this->data['shippingAddress'] = $this->ensureAddressDefaults($value);
158+
159+
return $this;
160+
}
161+
162+
/**
163+
* @param array $address
164+
* @return array
165+
*/
166+
protected function ensureAddressDefaults(array $address)
167+
{
168+
$fields = [
169+
'type', 'firstName', 'lastName', 'fiscalNumber', 'identityNumber', 'country', 'county',
170+
'city', 'zipCode', 'address', 'email', 'mobilePhone', 'bank', 'iban'
171+
];
172+
173+
foreach ($fields as $field) {
174+
if (!array_key_exists($field, $address)) {
175+
$address[$field] = '';
176+
}
177+
}
178+
179+
if (!in_array($address['type'], [Address::TYPE_COMPANY, Address::TYPE_PERSON])) {
180+
$address['type'] = Address::TYPE_PERSON;
181+
}
182+
183+
return $address;
184+
}
109185
}

src/Mobilpay.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
namespace Adrianbarbos\Mobilpay;
44

5-
class Mobilpay extends \Illuminate\Support\Facades\Facade {
6-
5+
/**
6+
* Class Mobilpay
7+
* @package Adrianbarbos\Mobilpay
8+
* @method static \Omnipay\Common\Message\ResponseInterface purchase(bool $autoRedirect = true)
9+
* @method static \Omnipay\Common\Message\ResponseInterface response()
10+
* @method static MobilpayGateway setOrderId($orderId)
11+
* @method static MobilpayGateway setAmount($amount)
12+
* @method static MobilpayGateway setCurrency($currency)
13+
* @method static MobilpayGateway setDetails($setDetails)
14+
* @method static MobilpayGateway setConfirmUrl($url)
15+
* @method static MobilpayGateway setReturnUrl($url)
16+
* @method static MobilpayGateway setCancelUrl($url)
17+
* @method static MobilpayGateway setTestMode(bool $flag)
18+
* @method static MobilpayGateway setAdditionalParams(array $value)
19+
*/
20+
class Mobilpay extends \Illuminate\Support\Facades\Facade
21+
{
722
/**
823
* {@inheritDoc}
924
*/
10-
protected static function getFacadeAccessor() { return 'mobilpay'; }
11-
12-
}
25+
protected static function getFacadeAccessor()
26+
{
27+
return 'mobilpay';
28+
}
29+
}

src/MobilpayGateway.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,34 @@
44

55
use Omnipay\Omnipay;
66

7-
class MobilpayGateway {
8-
7+
class MobilpayGateway
8+
{
99
protected $data;
1010

1111
use DataTrait;
1212

13-
public function __construct() {
13+
public function __construct()
14+
{
1415
$this->initData();
1516
}
1617

17-
public function purchase() {
18+
public function purchase($autoRedirect = true)
19+
{
1820
$gateway = Omnipay::create('MobilPay');
1921
$gateway->setMerchantId(config('mobilpay.merchant_id'));
2022
$gateway->setPublicKey(config('mobilpay.public_key_path'));
2123

2224
$response = $gateway->purchase($this->data)->send();
2325

24-
$response->redirect();
26+
if ($autoRedirect) {
27+
$response->redirect();
28+
}
29+
30+
return $response;
2531
}
2632

27-
public function response() {
33+
public function response()
34+
{
2835
$gateway = Omnipay::create('MobilPay');
2936
$gateway->setPrivateKey(config('mobilpay.private_key_path'));
3037

@@ -33,6 +40,4 @@ public function response() {
3340

3441
return $response;
3542
}
36-
3743
}
38-

0 commit comments

Comments
 (0)