diff --git a/src/DataTrait.php b/src/DataTrait.php index 0c8a60f..67a2150 100644 --- a/src/DataTrait.php +++ b/src/DataTrait.php @@ -2,8 +2,12 @@ namespace Adrianbarbos\Mobilpay; -trait DataTrait { - protected function initData() { +use Omnipay\MobilPay\Api\Address; + +trait DataTrait +{ + protected function initData() + { $this->data = [ 'orderId' => '', 'amount' => '', @@ -13,15 +17,33 @@ protected function initData() { 'returnUrl' => config('mobilpay.return_url'), 'cancelUrl' => config('mobilpay.cancel_url'), 'testMode' => config('mobilpay.testMode'), - 'params' => [] + 'params' => [] ]; + + //ensure absolute urls + foreach (['confirmUrl', 'returnUrl', 'cancelUrl'] as $var) { + if ($this->isRelativeUrl($this->data[$var])) { + $this->data[$var] = url($this->data[$var]); + } + } + } + + /** + * @param string $url + * @return bool + */ + protected function isRelativeUrl($url) + { + $url = parse_url($url); + return empty($url['host']); } /** * @param $value string * @return $this */ - public function setOrderId($value) { + public function setOrderId($value) + { $this->data['orderId'] = $value; return $this; @@ -31,7 +53,8 @@ public function setOrderId($value) { * @param $value string * @return $this */ - public function setAmount($value) { + public function setAmount($value) + { $this->data['amount'] = $value; return $this; @@ -41,7 +64,8 @@ public function setAmount($value) { * @param $value string * @return $this */ - public function setCurrency($value) { + public function setCurrency($value) + { $this->data['currency'] = $value; return $this; @@ -51,7 +75,8 @@ public function setCurrency($value) { * @param $value string * @return $this */ - public function setDetails($value) { + public function setDetails($value) + { $this->data['details'] = $value; return $this; @@ -61,7 +86,8 @@ public function setDetails($value) { * @param $value string * @return $this */ - public function setConfirmUrl($value) { + public function setConfirmUrl($value) + { $this->data['confirmUrl'] = $value; return $this; @@ -71,7 +97,8 @@ public function setConfirmUrl($value) { * @param $value string * @return $this */ - public function setReturnUrl($value) { + public function setReturnUrl($value) + { $this->data['returnUrl'] = $value; return $this; @@ -81,7 +108,8 @@ public function setReturnUrl($value) { * @param $value string * @return $this */ - public function setCancelUrl($value) { + public function setCancelUrl($value) + { $this->data['cancelUrl'] = $value; return $this; @@ -91,7 +119,8 @@ public function setCancelUrl($value) { * @param $value boolean * @return $this */ - public function setTestMode($value) { + public function setTestMode($value) + { $this->data['testMode'] = $value; return $this; @@ -101,9 +130,56 @@ public function setTestMode($value) { * @param array $value * @return $this */ - public function setAdditionalParams(array $value) { + public function setAdditionalParams(array $value) + { $this->data['params'] = $value; return $this; } + + /** + * @param array $value + * @return $this + */ + public function setBillingAddress(array $value) + { + $this->data['billingAddress'] = $this->ensureAddressDefaults($value); + + return $this; + } + + /** + * @param array $value + * @return $this + */ + public function setShippingAddress(array $value) + { + $this->data['shippingAddress'] = $this->ensureAddressDefaults($value); + + return $this; + } + + /** + * @param array $address + * @return array + */ + protected function ensureAddressDefaults(array $address) + { + $fields = [ + 'type', 'firstName', 'lastName', 'fiscalNumber', 'identityNumber', 'country', 'county', + 'city', 'zipCode', 'address', 'email', 'mobilePhone', 'bank', 'iban' + ]; + + foreach ($fields as $field) { + if (!array_key_exists($field, $address)) { + $address[$field] = ''; + } + } + + if (!in_array($address['type'], [Address::TYPE_COMPANY, Address::TYPE_PERSON])) { + $address['type'] = Address::TYPE_PERSON; + } + + return $address; + } } \ No newline at end of file diff --git a/src/Mobilpay.php b/src/Mobilpay.php index 1a99df0..6efb4ea 100644 --- a/src/Mobilpay.php +++ b/src/Mobilpay.php @@ -2,11 +2,28 @@ namespace Adrianbarbos\Mobilpay; -class Mobilpay extends \Illuminate\Support\Facades\Facade { - +/** + * Class Mobilpay + * @package Adrianbarbos\Mobilpay + * @method static \Omnipay\Common\Message\ResponseInterface purchase(bool $autoRedirect = true) + * @method static \Omnipay\Common\Message\ResponseInterface response() + * @method static MobilpayGateway setOrderId($orderId) + * @method static MobilpayGateway setAmount($amount) + * @method static MobilpayGateway setCurrency($currency) + * @method static MobilpayGateway setDetails($setDetails) + * @method static MobilpayGateway setConfirmUrl($url) + * @method static MobilpayGateway setReturnUrl($url) + * @method static MobilpayGateway setCancelUrl($url) + * @method static MobilpayGateway setTestMode(bool $flag) + * @method static MobilpayGateway setAdditionalParams(array $value) + */ +class Mobilpay extends \Illuminate\Support\Facades\Facade +{ /** * {@inheritDoc} */ - protected static function getFacadeAccessor() { return 'mobilpay'; } - -} \ No newline at end of file + protected static function getFacadeAccessor() + { + return 'mobilpay'; + } +} diff --git a/src/MobilpayGateway.php b/src/MobilpayGateway.php index 4271711..782dcb1 100644 --- a/src/MobilpayGateway.php +++ b/src/MobilpayGateway.php @@ -4,27 +4,34 @@ use Omnipay\Omnipay; -class MobilpayGateway { - +class MobilpayGateway +{ protected $data; use DataTrait; - public function __construct() { + public function __construct() + { $this->initData(); } - public function purchase() { + public function purchase($autoRedirect = true) + { $gateway = Omnipay::create('MobilPay'); $gateway->setMerchantId(config('mobilpay.merchant_id')); $gateway->setPublicKey(config('mobilpay.public_key_path')); $response = $gateway->purchase($this->data)->send(); - $response->redirect(); + if ($autoRedirect) { + $response->redirect(); + } + + return $response; } - public function response() { + public function response() + { $gateway = Omnipay::create('MobilPay'); $gateway->setPrivateKey(config('mobilpay.private_key_path')); @@ -33,6 +40,4 @@ public function response() { return $response; } - } -