Skip to content

Commit

Permalink
Merge branch 'master' into MerchantPayOut
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng authored Oct 16, 2024
2 parents d97fba7 + ff21017 commit 464e7e0
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 95 deletions.
10 changes: 4 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

This changelog references the relevant changes (bug and security fixes) done

## 2.0.1
- Fixed: vpos urls for production and test environments in `Environment`

### 2.0.0
### 2.0.x
- Rename: vpos to card
- Fixed: vpos urls for production and test environments in `Environment`
- Added: `isSuccessful` to `Transaction` class
- Breaking Change: `pay` supports both `vpos` and `mobile` request
- Added: `vpos` and `mobile` method to `Client`
Expand All @@ -15,8 +15,6 @@ This changelog references the relevant changes (bug and security fixes) done
- Added: Support for `check`, `vpos`, `mobile` urls in `Environment`,
- Removed: `getPaymentBaseUrl` method in `Environment`

### 1.0.1
### 1.0.x
- Fixed: allow `$message` to be an empty string in `PaymentResponse`

### 1.0.0
- Initial release
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
65 changes: 32 additions & 33 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Devscast\Flexpay;

use Devscast\Flexpay\Exception\NetworkException;
use Devscast\Flexpay\Request\CardRequest;
use Devscast\Flexpay\Request\MobileRequest;
use Devscast\Flexpay\Request\PayoutRequest;
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\PayoutResponse;
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 @@ -60,29 +60,6 @@ public function __construct(
);
}

/**
* Permet d'envoyer une intention de paiement sur le mobile money du client
*
* @throws NetworkException
*/
public function payout(PayoutRequest $request): PayoutResponse
{
$request->setCredential($this->credential);

try {
/** @var PayoutResponse $response */
$response = $this->getMappedData(
type: PayoutResponse::class,
data: $this->http->request('POST', $this->environment->getPayoutUrl(), [
'json' => $request->getPayload(),
])->toArray()
);

return $response;
} catch (\Throwable $e) {
$this->createExceptionFromResponse($e);
}
}

/**
* Permet d'envoyer une directement intention de paiement sur le mobile money du client
Expand Down Expand Up @@ -115,15 +92,15 @@ 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,
data: $this->http->request('POST', $this->environment->getVposAskUrl(), [
type: CardResponse::class,
data: $this->http->request('POST', $this->environment->getCardPaymentUrl(), [
'json' => $request->getPayload(),
])->toArray()
);
Expand All @@ -137,13 +114,11 @@ public function vpos(VposRequest $request): VposResponse
/**
* @throws NetworkException
*/
public function pay(Request $request): PaymentResponse|VposResponse|PayoutResponse
public function pay(Request $request): PaymentResponse|CardResponse
{
return match (true) {
$request instanceof MobileRequest => $this->mobile($request),
$request instanceof VposRequest => $this->vpos($request),
$request instanceof PayoutRequest => $this->payout($request),

$request instanceof CardRequest => $this->card($request),
default => throw new \RuntimeException('Unsupported request')
};
}
Expand Down Expand Up @@ -171,6 +146,30 @@ public function check(string $orderNumber): CheckResponse
$this->createExceptionFromResponse($e);
}
}

/**
* Permet d'envoyer une intention de paiement sur le mobile money du client
*
* @throws NetworkException
*/
public function payout(PayoutRequest $request): PayoutResponse
{
$request->setCredential($this->credential);

try {
/** @var PayoutResponse $response */
$response = $this->getMappedData(
type: PayoutResponse::class,
data: $this->http->request('POST', $this->environment->getPayoutUrl(), [
'json' => $request->getPayload(),
])->toArray()
);

return $response;
} catch (\Throwable $e) {
$this->createExceptionFromResponse($e);
}
}

/**
* Cette interface permet d'obtenir une réponse de paiement provenant de FlexPay
Expand Down
14 changes: 3 additions & 11 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ enum Environment: string
case LIVE = 'prod';
case SANDBOX = 'dev';

public function getVposAskUrl(): string
public function getCardPaymentUrl(): string
{
return match ($this) {
self::LIVE => 'https://cardpayment.flexpay.cd/api/rest/v1/vpos/ask',
self::SANDBOX => 'https://beta-cardpayment.flexpay.cd/api/rest/v1/vpos/ask',
self::LIVE => 'https://cardpayment.flexpay.cd/v1.1/pay',
self::SANDBOX => 'https://beta-cardpayment.flexpay.cd/v1.1/pay',
};
}

Expand All @@ -29,14 +29,6 @@ public function getMobilePaymentUrl(): string
};
}

public function getVposPaymentUrl(string $orderNumber): string
{
return match ($this) {
self::LIVE => sprintf('https://cardpayment.flexpay.cd/vpos/pay/%s', $orderNumber),
self::SANDBOX => sprintf('https://beta-cardpayment.flexpay.cd/vpos/pay/%s', $orderNumber),
};
}

public function getCheckStatusUrl(string $orderNumber): string
{
return match ($this) {
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 @@ -13,9 +13,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 @@ -45,23 +45,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);
}

/**
Expand Down
22 changes: 5 additions & 17 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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/api/rest/v1/vpos/ask',
$this->prod->getVposAskUrl()
'https://cardpayment.flexpay.cd/v1.1/pay',
$this->prod->getCardPaymentUrl()
);
$this->assertEquals(
'https://beta-cardpayment.flexpay.cd/api/rest/v1/vpos/ask',
$this->dev->getVposAskUrl()
'https://beta-cardpayment.flexpay.cd/v1.1/pay',
$this->dev->getCardPaymentUrl()
);
}

Expand Down Expand Up @@ -69,18 +69,6 @@ public function testGetPayoutUrl(): 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 464e7e0

Please sign in to comment.