Skip to content

Commit

Permalink
Merge pull request #27 from apoca/develop
Browse files Browse the repository at this point in the history
closed: Added new field optionalParameters.
  • Loading branch information
apoca committed Apr 17, 2019
2 parents a6423eb + ffd28d1 commit d08002a
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 21 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ $request = [
'brand' => 'CHECKOUT',
'amount' => 100,
'currency' => 'EUR',
'type' => 'DB'
'type' => 'DB',
'optionalParameters' => [],
];

$response = Sibs::checkout($request)->pay();
Expand Down Expand Up @@ -150,6 +151,7 @@ $request = [
'expiry_month' => 05,
'expiry_year' => 2020,
'cvv' => 123,
'optionalParameters' => [],
];
$response = Sibs::checkout($request)->pay();
```
Expand All @@ -164,6 +166,7 @@ $request = [
'brand' => 'MBWAY',
'type' => 'PA',
'accountId' => '351#911222111',
'optionalParameters' => [],
];
$response = Sibs::checkout($request)->pay();
```
Expand Down
31 changes: 27 additions & 4 deletions src/Brands/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ class Checkout implements PaymentInterface
*/
protected $clientConfig = [];

/**
* @var array
*/
protected $optionalParameters = [];

/**
* Checkout constructor.
*
* @param float $amount
* @param string $currency
* @param string $paymentType
* @param array $optionalParameters
*/
public function __construct(float $amount, string $currency, string $paymentType)
public function __construct(float $amount, string $currency, string $paymentType, array $optionalParameters)
{
$this->setAmount($amount);
$this->setCurrency($currency);
$this->setPaymentType($paymentType);
$this->setOptionalParameters($optionalParameters);

if (config('sibs.mode') === 'test') {
$this->clientConfig = [
Expand All @@ -59,6 +66,22 @@ public function __construct(float $amount, string $currency, string $paymentType
$this->endpoint = config('sibs.host') . config('sibs.version') . '/';
}

/**
* @return array
*/
public function getOptionalParameters(): array
{
return $this->optionalParameters;
}

/**
* @param array $optionalParameters
*/
public function setOptionalParameters(array $optionalParameters): void
{
$this->optionalParameters = $optionalParameters;
}

/**
* @return float
*/
Expand Down Expand Up @@ -130,18 +153,18 @@ public function pay(): object
'Content-Length' => ob_get_length(),
'Authorization' => config('sibs.authentication.token'),
],
'form_params' => $payload,
'form_params' => array_merge($payload, $this->getOptionalParameters()),
]);

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
} catch (ClientException $e) {
$response = $e->getResponse();

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
}
Expand Down
25 changes: 24 additions & 1 deletion src/Brands/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ abstract class Payment implements PaymentInterface
*/
protected $brand;

/**
* @var array
*/
protected $optionalParameters = [];

/**
* @var string
*/
Expand All @@ -38,13 +43,31 @@ abstract class Payment implements PaymentInterface
* @param string $currency
* @param string $brand
* @param string $type
* @param array $optionalParameters
*/
public function __construct(float $amount, string $currency, string $brand, string $type)
public function __construct(float $amount, string $currency, string $brand, string $type, array $optionalParameters)
{
$this->setAmount($amount);
$this->setCurrency($currency);
$this->setBrand($brand);
$this->setType($type);
$this->setOptionalParameters($optionalParameters);
}

/**
* @return array
*/
public function getOptionalParameters(): array
{
return $this->optionalParameters;
}

/**
* @param array $optionalParameters
*/
public function setOptionalParameters(array $optionalParameters): void
{
$this->optionalParameters = $optionalParameters;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Brands/PaymentWithCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ class PaymentWithCard extends Payment
* @param string $currency
* @param string $brand
* @param string $type
* @param array $optionalParameters
* @param Card $card
*/
public function __construct(
float $amount,
string $currency,
string $brand,
string $type,
array $optionalParameters,
Card $card
) {
parent::__construct($amount, $currency, $brand, $type);
parent::__construct($amount, $currency, $brand, $type, $optionalParameters);
$this->card = $card;

if (config('sibs.mode') === 'test') {
Expand Down Expand Up @@ -85,18 +87,18 @@ public function pay(): object
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => $payload,
'form_params' => array_merge($payload, $this->getOptionalParameters()),
]);

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
} catch (ClientException $e) {
$response = $e->getResponse();

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Brands/PaymentWithMBWay.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ class PaymentWithMBWay extends Payment
* @param string $brand
* @param string $type
* @param string $accountId
* @param array $optionalParameters
*/
public function __construct(
float $amount,
string $currency,
string $brand,
string $type,
string $accountId
string $accountId,
array $optionalParameters
) {
parent::__construct($amount, $currency, $brand, $type);
parent::__construct($amount, $currency, $brand, $type, $optionalParameters);
$this->accountId = $accountId;
$this->endpoint = config('sibs.host') . config('sibs.version') . '/';
}
Expand Down Expand Up @@ -80,18 +82,18 @@ public function pay()
'headers' => [
'Authorization' => config('sibs.authentication.token'),
],
'form_params' => $payload,
'form_params' => array_merge($payload, $this->getOptionalParameters()),
]);

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
} catch (ClientException $e) {
$response = $e->getResponse();

$data->status = $response->getStatusCode();
$data->response = json_decode($response->getBody()->getContents());
$data->response = json_decode($response->getBody()->getContents(), false);

return $data;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Services/SibsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function checkout(array $request): PaymentInterface
strtoupper($request['currency']),
strtoupper($request['brand']),
strtoupper($request['type']),
$request['optionalParameters'],
new Card(
$request['number'],
$request['holder'],
Expand All @@ -48,7 +49,7 @@ public function checkout(array $request): PaymentInterface
);
break;
case 'CHECKOUT':
$payment = new Checkout($request['amount'], $request['currency'], $request['type']);
$payment = new Checkout($request['amount'], $request['currency'], $request['type'], $request['optionalParameters']);
break;
case 'SIBS_MULTIBANCO':
throw new \RuntimeException('SIBS_MULTIBANCO Service Payment not found.', 404);
Expand All @@ -62,7 +63,8 @@ public function checkout(array $request): PaymentInterface
strtoupper($request['currency']),
strtoupper($request['brand']),
strtoupper($request['type']),
$request['accountId']
$request['accountId'],
$request['optionalParameters']
);
break;
default:
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Apoca\Sibs\Tests\Feature;

use Apoca\Sibs\Tests\TestCase;
use Apoca\Sibs\Facade\Sibs;
use Apoca\Sibs\Tests\TestCase;

/**
* Class CheckoutTest
Expand All @@ -26,7 +26,8 @@ public function verifyResponseCheckoutSuccess(): void
'brand' => 'CHECKOUT',
'amount' => 100,
'currency' => 'EUR',
'type' => 'DB'
'type' => 'DB',
'optionalParameters' => [],
];

$response = Sibs::checkout($request)->pay();
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/PaymentWithCardsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function verifyResponsePaymentCardsSuccess(): void
'expiry_month' => 5,
'expiry_year' => 2020,
'cvv' => 123,
'optionalParameters' => [],
];

$response = Sibs::checkout($request)->pay();
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/PaymentWithMBWayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function verifyResponsePaymentWithMbwayDefaultTest(): void
'brand' => 'MBWAY',
'type' => 'PA',
'accountId' => '351#911222111',
'optionalParameters' => [],
];

$response = Sibs::checkout($request)->pay();
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/SibsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Apoca\Sibs\Tests\Feature;

use Apoca\Sibs\Tests\TestCase;
use Apoca\Sibs\Facade\Sibs;
use Apoca\Sibs\Tests\TestCase;

/**
* Class CheckoutTest
Expand All @@ -26,7 +26,8 @@ public function verifyResponseStatusInvalidEntity(): void
'brand' => 'CHECKOUT',
'amount' => 100,
'currency' => 'EUR',
'type' => 'DB'
'type' => 'DB',
'optionalParameters' => [],
];

$response = Sibs::checkout($request)->pay();
Expand Down

0 comments on commit d08002a

Please sign in to comment.