Skip to content

Commit

Permalink
Add Payout support for merchants (#1)
Browse files Browse the repository at this point in the history
* Merchant Pay out

* Merchant Pay out

* Refactor the PayoutResponse and PayoutRequest

* Refactor Transaction Type

* Refactor Transaction Type

* Payout response

* Refactor transaction type

* Add test Payout

* Add test Payout

---------

Co-authored-by: Bernard Ngandu <[email protected]>
  • Loading branch information
rooneyi and bernard-ng authored Oct 16, 2024
1 parent ff21017 commit 1c5c744
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
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\Response\CardResponse;
use Devscast\Flexpay\Response\CheckResponse;
use Devscast\Flexpay\Response\FlexpayResponse;
use Devscast\Flexpay\Response\PaymentResponse;
use Devscast\Flexpay\Response\PayoutResponse;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
use Symfony\Component\HttpClient\RetryableHttpClient;
Expand Down Expand Up @@ -58,6 +60,7 @@ public function __construct(
);
}


/**
* Permet d'envoyer une directement intention de paiement sur le mobile money du client
*
Expand Down Expand Up @@ -143,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
17 changes: 17 additions & 0 deletions src/Data/TransactionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Devscast\Flexpay\Data;

enum TransactionType: int
{
/**
* 0 : pour les transactions mobile money
*/
case MOBILE = 0;

/**
* 1 : pour les transactions bancaires
*/
case CARD = 1;

}
12 changes: 10 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ public function getCheckStatusUrl(string $orderNumber): string
};
}

public function getPayoutUrl(): string
{
return match ($this) {
self::LIVE => sprintf('%s/merchantPayOutService', $this->getBaseUrl()),
self::SANDBOX => sprintf('%s/merchantPayOutService', $this->getBaseUrl()),
};
}

private function getBaseUrl(): string
{
return match ($this) {
self::LIVE => 'https://backend.flexpay.cd/api/rest/v1',
self::SANDBOX => 'https://beta-backend.flexpay.cd/api/rest/v1',
self::LIVE => sprintf('%s/merchantPayOutService', $this->getBaseUrl()),
self::SANDBOX => sprintf('%s/merchantPayOutService', $this->getBaseUrl())
};
}
}
38 changes: 38 additions & 0 deletions src/Request/PayoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Devscast\Flexpay\Request;

use Devscast\Flexpay\Data\Currency;
use Webmozart\Assert\Assert;

final class PayoutRequest extends Request
{
public function __construct(
float $amount,
string $reference,
Currency $currency,
string $callbackUrl,
public string $phone,
public int $type = 1,
) {

Assert::length($this->phone, 12, 'The phone number should be 12 characters long, eg: 243123456789');

parent::__construct($amount, $reference, $currency, $callbackUrl);
}

public function getPayload(): array
{
return [
'authorization' => $this->authorization,
'type' => $this->type,
'reference' => $this->reference,
'phone' => $this->phone,
'amount' => $this->amount,
'currency' => $this->currency->value,
'callbackUrl' => $this->callbackUrl,
];
}
}
17 changes: 17 additions & 0 deletions src/Response/PayoutResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Devscast\Flexpay\Response;

use Devscast\Flexpay\Data\Status;

final class PayoutResponse extends FlexpayResponse
{
public function __construct(
public Status $code,
public string $message = '',
public ?string $orderNumber = null,
) {
}
}
29 changes: 29 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Devscast\Flexpay\Tests;

use Devscast\Flexpay\Client;
use Devscast\Flexpay\Data\TransactionType;
use Devscast\Flexpay\Exception\NetworkException;
use Devscast\Flexpay\Request\PayoutRequest;
use Devscast\Flexpay\Response\PayoutResponse;
use PHPUnit\Framework\TestCase;
use Devscast\Flexpay\Credential;
use Devscast\Flexpay\Data\Currency;
Expand Down Expand Up @@ -63,6 +67,29 @@ public function testCard(): void
$this->assertEquals('https://gwvisa.flexpay.cd/checkout/bbba6b699af8a70e9cfa010d6d12dba5_670d206b7defb', $response->url);
}

/**
* @throws NetworkException
*/
public function testPayout(): void
{
$flexpay = $this->getFlexpay($this->getResponse('payout.json'));

$request = new PayoutRequest(
amount: 10,
reference: 'ref',
currency: Currency::USD,
callbackUrl: 'http://localhost:8000/callback',
phone: '243123456789',
type: 1
);

$response = $flexpay->payout($request);

$this->assertInstanceOf(PayoutResponse::class,$response);
$this->assertTrue($response->isSuccessful());
$this->assertEquals('6708a4708d470_1728619632', $response->orderNumber);

}
public function testSuccessCheck(): void
{
$flexpay = $this->getFlexpay($this->getResponse('check_success.json'));
Expand Down Expand Up @@ -102,6 +129,8 @@ public function testMobile(): void
$this->assertEquals('DtX9SmCYojWW243123456789', $response->orderNumber);
}



public function testHandleCallback(): void
{
/** @var array $data */
Expand Down
14 changes: 14 additions & 0 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function testGetCardPaymentUrl(): void
);
}


public function testGetMobilePaymentUrl(): void
{
$this->assertEquals(
Expand All @@ -55,6 +56,19 @@ public function testGetMobilePaymentUrl(): void
);
}

public function testGetPayoutUrl(): void
{
$this->assertEquals(
'https://backend.flexpay.cd/api/rest/v1/merchantPayOutService',
$this->prod->getPayoutUrl()
);

$this->assertEquals(
'https://beta-backend.flexpay.cd/api/rest/v1/merchantPayOutService',
$this->dev->getPayoutUrl()
);
}

public function testGetCheckStatusUrl(): void
{
$this->assertEquals(
Expand Down

0 comments on commit 1c5c744

Please sign in to comment.