diff --git a/README.md b/README.md index 9b588ee..41057c5 100644 --- a/README.md +++ b/README.md @@ -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( @@ -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", diff --git a/src/Client.php b/src/Client.php index a1e04df..77afe16 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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() @@ -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') }; } diff --git a/src/Request/VposRequest.php b/src/Request/CardRequest.php similarity index 58% rename from src/Request/VposRequest.php rename to src/Request/CardRequest.php index d697052..44fb49a 100644 --- a/src/Request/VposRequest.php +++ b/src/Request/CardRequest.php @@ -8,35 +8,45 @@ use Webmozart\Assert\Assert; /** - * Class VposRequest. + * Class CardRequest. * * @author bernard-ng */ -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, diff --git a/src/Response/VposResponse.php b/src/Response/CardResponse.php similarity index 83% rename from src/Response/VposResponse.php rename to src/Response/CardResponse.php index cd05b3f..b6bc691 100644 --- a/src/Response/VposResponse.php +++ b/src/Response/CardResponse.php @@ -7,11 +7,11 @@ use Devscast\Flexpay\Data\Status; /** - * Class VposResponse. + * Class CardResponse. * * @author bernard-ng */ -final class VposResponse extends FlexpayResponse +final class CardResponse extends FlexpayResponse { public function __construct( public Status $code, diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e27dd7f..bbcb411 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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; @@ -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 diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index fcf97b5..9746bea 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -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', @@ -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( diff --git a/tests/fixtures/card_success.json b/tests/fixtures/card_success.json new file mode 100644 index 0000000..1d9f4b2 --- /dev/null +++ b/tests/fixtures/card_success.json @@ -0,0 +1,6 @@ +{ + "code": 0, + "message": "Redirection en cours", + "orderNumber": "O42iABI27568020268434827", + "url": "https://gwvisa.flexpay.cd/checkout/bbba6b699af8a70e9cfa010d6d12dba5_670d206b7defb" +} diff --git a/tests/fixtures/vpos_ask.json b/tests/fixtures/vpos_ask.json deleted file mode 100644 index 83bd6ae..0000000 --- a/tests/fixtures/vpos_ask.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "code": 0, - "message": "Ok", - "orderNumber": "6708a4708d470_1728619632", - "url": "https://beta-cardpayment.flexpay.cd/vpos/pay/6708a4708d470_1728619632" -}