Skip to content

Commit

Permalink
Merge 0584730 into d267e5b
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Dec 11, 2019
2 parents d267e5b + 0584730 commit 0461467
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/v3/creating-bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
null,
'Mior Muhammad Zaki',
300,
'https://localhost/webhook/billplz',
new Billplz\PaymentCompletion('https://localhost/webhook/billplz'),
'My first bill'
);

Expand Down
12 changes: 9 additions & 3 deletions src/Base/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Laravie\Codex\Contracts\Response;
use Billplz\Contracts\Bill as Contract;
use Laravie\Codex\Concerns\Request\Multipart;
use Billplz\PaymentCompletion as PaymentCompletionUrl;
use Billplz\Contracts\PaymentCompletion as PaymentCompletionContract;

abstract class Bill extends Request implements Contract
{
Expand All @@ -17,7 +19,7 @@ abstract class Bill extends Request implements Contract
* Create a new bill.
*
* @param \Money\Money|\Duit\MYR|int $amount
* @param array|string $callbackUrl
* @param \Billplz\Contracts\PaymentCompletion|string $paymentCompletion
*
* @throws \InvalidArgumentException
*
Expand All @@ -29,7 +31,7 @@ public function create(
?string $mobile,
string $name,
$amount,
$callbackUrl,
$paymentCompletion,
string $description,
array $optional = []
): Response {
Expand All @@ -43,7 +45,11 @@ public function create(

$body['collection_id'] = $collectionId;

$body = $this->parseRedirectAndCallbackUrlFromRequest($body, $callbackUrl);
$paymentCompletion = $paymentCompletion instanceof PaymentCompletionContract
? $paymentCompletion
: new PaymentCompletionUrl($paymentCompletion);

$body = \array_merge($body, $paymentCompletion->toArray());

return $this->stream('POST', 'bills', [], $body);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Bill extends Request
* Create a new bill.
*
* @param \Money\Money|\Duit\MYR|int $amount
* @param array|string $callbackUrl
* @param \Billplz\Contracts\PaymentCompletion|string $paymentCompletion
*
* @throws \InvalidArgumentException
*/
Expand All @@ -21,7 +21,7 @@ public function create(
?string $mobile,
string $name,
$amount,
$callbackUrl,
$paymentCompletion,
string $description,
array $optional = []
): Response;
Expand Down
21 changes: 21 additions & 0 deletions src/Contracts/PaymentCompletion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Billplz\Contracts;

interface PaymentCompletion
{
/**
* Get Webhook URL.
*/
public function webhookUrl(): string;

/**
* Get Redirect URL.
*/
public function redirectUrl(): ?string;

/**
* Convert to array.
*/
public function toArray(): array;
}
56 changes: 56 additions & 0 deletions src/PaymentCompletion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Billplz;

class PaymentCompletion implements Contracts\PaymentCompletion
{
/**
* Webhook URL.
*
* @var string
*/
protected $webhookUrl;

/**
* Redirect URL.
*
* @var string|null
*/
protected $redirectUrl;

/**
* Construct Payment Completion.
*/
public function __construct(string $callbackUrl, ?string $redirectUrl = null)
{
$this->webhookUrl = $callbackUrl;
$this->redirectUrl = $redirectUrl;
}

/**
* Get Webhook URL.
*/
public function webhookUrl(): string
{
return $this->webhookUrl;
}

/**
* Get Redirect URL.
*/
public function redirectUrl(): ?string
{
return $this->redirectUrl;
}

/**
* Convert to array.
*/
public function toArray(): array
{
return [
'callback_url' => $this->webhookUrl,
'redirect_url' => $this->redirectUrl,
];
}
}
17 changes: 0 additions & 17 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,4 @@ protected function sanitizeWith(): Sanitizer
{
return new Sanitizer();
}

/**
* Parse callback URL from request.
*
* @param array|string $url
*/
final protected function parseRedirectAndCallbackUrlFromRequest(array $body, $url): array
{
if (\is_string($url)) {
$body['callback_url'] = $url;
} elseif (\is_array($url)) {
$body['callback_url'] = $url['callback_url'] ?? null;
$body['redirect_url'] = $url['redirect_url'] ?? null;
}

return $body;
}
}
12 changes: 6 additions & 6 deletions tests/Base/BillTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Duit\MYR;
use Billplz\Tests\TestCase;
use Laravie\Codex\Response;
use Billplz\PaymentCompletion;
use Laravie\Codex\Exceptions\HttpException;

abstract class BillTestCase extends TestCase
Expand Down Expand Up @@ -44,7 +45,7 @@ public function it_can_be_created()
$payload['mobile'],
$payload['name'],
MYR::given($payload['amount']),
$payload['callback_url'],
new PaymentCompletion($payload['callback_url']),
$payload['description']
);

Expand Down Expand Up @@ -83,10 +84,9 @@ public function it_can_be_created_with_url_as_array()
$payload['mobile'],
$payload['name'],
MYR::given($payload['amount']),
[
'callback_url' => $payload['callback_url'],
'redirect_url' => $payload['redirect_url'],
],
new PaymentCompletion(
$payload['callback_url'], $payload['redirect_url']
),
$payload['description']
);

Expand Down Expand Up @@ -122,7 +122,7 @@ public function it_cant_be_created_given_empty_email_and_mobile()
$payload['mobile'],
$payload['name'],
MYR::given($payload['amount']),
$payload['callback_url'],
new PaymentCompletion($payload['callback_url']),
$payload['description']
);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/PaymentCompletionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Billplz\Tests;

use Billplz\PaymentCompletion;
use PHPUnit\Framework\TestCase;

class PaymentCompletionTest extends TestCase
{
/** @test */
public function it_can_generate_without_redirect_url()
{
$payment = new PaymentCompletion('http://example.com/webhook/');

$this->assertSame('http://example.com/webhook/', $payment->webhookUrl());
$this->assertNull($payment->redirectUrl());
$this->assertSame([
'callback_url' => 'http://example.com/webhook/',
'redirect_url' => null,
], $payment->toArray());
}

/** @test */
public function it_can_generate_with_redirect_url()
{
$payment = new PaymentCompletion('http://example.com/webhook/', 'http://example.com/redirect/');

$this->assertSame('http://example.com/webhook/', $payment->webhookUrl());
$this->assertSame('http://example.com/redirect/', $payment->redirectUrl());
$this->assertSame([
'callback_url' => 'http://example.com/webhook/',
'redirect_url' => 'http://example.com/redirect/',
], $payment->toArray());
}
}

0 comments on commit 0461467

Please sign in to comment.