Skip to content

Commit

Permalink
Merge pull request #7 from firewizard/master
Browse files Browse the repository at this point in the history
billing/shipping + other tweaks
  • Loading branch information
adrianbarbos authored Oct 17, 2019
2 parents 07bfab6 + 15b4f43 commit f36d4a1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 25 deletions.
100 changes: 88 additions & 12 deletions src/DataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
}
27 changes: 22 additions & 5 deletions src/Mobilpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'; }

}
protected static function getFacadeAccessor()
{
return 'mobilpay';
}
}
21 changes: 13 additions & 8 deletions src/MobilpayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

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

return $response;
}

}

0 comments on commit f36d4a1

Please sign in to comment.