Skip to content

Commit

Permalink
rename vpos to card
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Oct 14, 2024
1 parent 7ff19b7 commit 3abdc21
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $flexpay = new Flexpay(

```php
use Devscast\Flexpay\Data\Currency;
use Devscast\Flexpay\Request\VposRequest;
use Devscast\Flexpay\Request\CardRequest;
use Devscast\Flexpay\Request\MobileRequest;

$mobile = new MobileRequest(
Expand All @@ -51,7 +51,7 @@ $mobile = new MobileRequest(
callbackUrl: "your_website_webhook_url",
);

$card = new VposRequest(
$card = new CardRequest(
amount: 10, // 10 USD
currency: Currency::USD,
reference: "your_unique_transaction_reference",
Expand Down
14 changes: 7 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Devscast\Flexpay;

use Devscast\Flexpay\Exception\NetworkException;
use Devscast\Flexpay\Request\CardRequest;
use Devscast\Flexpay\Request\MobileRequest;
use Devscast\Flexpay\Request\Request;
use Devscast\Flexpay\Request\VposRequest;
use Devscast\Flexpay\Response\CardResponse;
use Devscast\Flexpay\Response\CheckResponse;
use Devscast\Flexpay\Response\FlexpayResponse;
use Devscast\Flexpay\Response\PaymentResponse;
use Devscast\Flexpay\Response\VposResponse;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
use Symfony\Component\HttpClient\RetryableHttpClient;
Expand Down Expand Up @@ -89,14 +89,14 @@ public function mobile(MobileRequest $request): PaymentResponse
*
* @throws NetworkException
*/
public function vpos(VposRequest $request): VposResponse
public function card(CardRequest $request): CardResponse
{
$request->setCredential($this->credential);

try {
/** @var VposResponse $response */
/** @var CardResponse $response */
$response = $this->getMappedData(
type: VposResponse::class,
type: CardResponse::class,
data: $this->http->request('POST', $this->environment->getVposAskUrl(), [
'json' => $request->getPayload(),
])->toArray()
Expand All @@ -111,11 +111,11 @@ public function vpos(VposRequest $request): VposResponse
/**
* @throws NetworkException
*/
public function pay(Request $request): PaymentResponse|VposResponse
public function pay(Request $request): PaymentResponse|CardResponse
{
return match (true) {
$request instanceof MobileRequest => $this->mobile($request),
$request instanceof VposRequest => $this->vpos($request),
$request instanceof CardRequest => $this->card($request),
default => throw new \RuntimeException('Unsupported request')
};
}
Expand Down
26 changes: 18 additions & 8 deletions src/Request/VposRequest.php → src/Request/CardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,45 @@
use Webmozart\Assert\Assert;

/**
* Class VposRequest.
* Class CardRequest.
*
* @author bernard-ng <[email protected]>
*/
final class VposRequest extends Request
final class CardRequest extends Request
{
public function __construct(
float $amount,
string $reference,
Currency $currency,
string $description,
string $callbackUrl,
public readonly ?string $homeUrl,
?string $description = null,
?string $approveUrl = null,
?string $cancelUrl = null,
?string $declineUrl = null
string $approveUrl,
string $cancelUrl,
string $declineUrl,
public string $homeUrl,
) {
Assert::notEmpty($description, 'The description must be provided');
Assert::notEmpty($approveUrl, 'The approve url must be provided');
Assert::notEmpty($cancelUrl, 'The cancel url must be provided');
Assert::notEmpty($declineUrl, 'The decline url must be provided');
Assert::notEmpty($homeUrl, 'The home url must be provided');
Assert::lengthBetween($reference, 1, 25, 'The reference must be between 1 and 25 characters');

parent::__construct($amount, $reference, $currency, $callbackUrl, $approveUrl, $description, $cancelUrl, $declineUrl);
}

/**
* Yeah, I know this is weird
* But I'm not responsible for the API design.
* so don't blame :D
*/
#[\Override]
public function getPayload(): array
{
return [
'amount' => $this->amount,
'merchant' => $this->merchant,
'authorization' => $this->authorization,
'authorization' => sprintf('Bearer %s', $this->authorization),
'reference' => $this->reference,
'currency' => $this->currency->value,
'callback_url' => $this->callbackUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use Devscast\Flexpay\Data\Status;

/**
* Class VposResponse.
* Class CardResponse.
*
* @author bernard-ng <[email protected]>
*/
final class VposResponse extends FlexpayResponse
final class CardResponse extends FlexpayResponse
{
public function __construct(
public Status $code,
Expand Down
23 changes: 13 additions & 10 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Devscast\Flexpay\Credential;
use Devscast\Flexpay\Data\Currency;
use Devscast\Flexpay\Data\Transaction;
use Devscast\Flexpay\Request\VposRequest;
use Devscast\Flexpay\Request\CardRequest;
use Devscast\Flexpay\Request\MobileRequest;
use Devscast\Flexpay\Response\VposResponse;
use Devscast\Flexpay\Response\CardResponse;
use Devscast\Flexpay\Response\CheckResponse;
use Devscast\Flexpay\Response\PaymentResponse;
use Symfony\Component\HttpClient\MockHttpClient;
Expand Down Expand Up @@ -41,23 +41,26 @@ private function getResponse(string $file): MockResponse
return new MockResponse((string) file_get_contents(__DIR__ . '/fixtures/' . $file));
}

public function testVpos(): void
public function testCard(): void
{
$flexpay = $this->getFlexpay($this->getResponse('vpos_ask.json'));
$request = new VposRequest(
$flexpay = $this->getFlexpay($this->getResponse('card_success.json'));
$request = new CardRequest(
amount: 1,
reference: 'ref',
currency: Currency::USD,
description: 'test',
callbackUrl: 'http://localhost:8000/callback',
approveUrl: 'http://localhost:8000/approve',
cancelUrl: 'http://localhost:8000/cancel',
declineUrl: 'http://localhost:8000/decline',
homeUrl: 'http://localhost:8000/home',
approveUrl: 'http://localhost:8000/approve'
);
$response = $flexpay->vpos($request);
$response = $flexpay->card($request);

$this->assertInstanceOf(VposResponse::class, $response);
$this->assertInstanceOf(CardResponse::class, $response);
$this->assertTrue($response->isSuccessful());
$this->assertEquals('6708a4708d470_1728619632', $response->orderNumber);
$this->assertEquals('https://beta-cardpayment.flexpay.cd/vpos/pay/6708a4708d470_1728619632', $response->url);
$this->assertEquals('O42iABI27568020268434827', $response->orderNumber);
$this->assertEquals('https://gwvisa.flexpay.cd/checkout/bbba6b699af8a70e9cfa010d6d12dba5_670d206b7defb', $response->url);
}

public function testSuccessCheck(): void
Expand Down
14 changes: 1 addition & 13 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testEnvironment(): void
$this->assertEquals(Environment::SANDBOX, Environment::from('dev'));
}

public function testGetVposAskUrl(): void
public function testGetCardPaymentUrl(): void
{
$this->assertEquals(
'https://cardpayment.flexpay.cd/v1.1/pay',
Expand All @@ -55,18 +55,6 @@ public function testGetMobilePaymentUrl(): void
);
}

public function testGetVposPaymentUrl(): void
{
$this->assertEquals(
'https://cardpayment.flexpay.cd/vpos/pay/123456',
$this->prod->getVposPaymentUrl('123456')
);
$this->assertEquals(
'https://beta-cardpayment.flexpay.cd/vpos/pay/123456',
$this->dev->getVposPaymentUrl('123456')
);
}

public function testGetCheckStatusUrl(): void
{
$this->assertEquals(
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/card_success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"code": 0,
"message": "Redirection en cours",
"orderNumber": "O42iABI27568020268434827",
"url": "https://gwvisa.flexpay.cd/checkout/bbba6b699af8a70e9cfa010d6d12dba5_670d206b7defb"
}
6 changes: 0 additions & 6 deletions tests/fixtures/vpos_ask.json

This file was deleted.

0 comments on commit 3abdc21

Please sign in to comment.