-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Flexpay\Request; | ||
|
||
use Devscast\Flexpay\Credential; | ||
use Devscast\Flexpay\Data\Currency; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class MerchantPayOutRequest extends Request | ||
{ | ||
private HttpClientInterface $client; | ||
|
||
private string $apiUrl = 'https://api.flexpay.cd/api/merchantPayOutService'; | ||
|
||
public function __construct( | ||
float $amount, | ||
string $reference, | ||
Currency $currency, | ||
string $callbackUrl, | ||
public string $phone, | ||
public Credential $credentials, | ||
public string $telephone, | ||
public int $type = 1, | ||
) { | ||
$this->client = HttpClient::create(); | ||
|
||
Assert::length($this->telephone, 12, 'The phone number should be 12 characters long, eg: 243123456789'); | ||
|
||
parent::__construct($amount, $reference, $currency, $callbackUrl); | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return [ | ||
'merchant' => $this->merchant, | ||
'type' => $this->type, | ||
'reference' => $this->reference, | ||
'phone' => $this->telephone, | ||
'amount' => $this->amount, | ||
'currency' => $this->currency->value, | ||
'callbackUrl' => $this->callbackUrl, | ||
]; | ||
} | ||
|
||
/** | ||
* @throws TransportExceptionInterface | ||
* @throws ServerExceptionInterface | ||
* @throws RedirectionExceptionInterface | ||
* @throws DecodingExceptionInterface | ||
* @throws ClientExceptionInterface | ||
*/ | ||
public function sendRequest(): array | ||
{ | ||
$response = $this->client->request('POST', $this->apiUrl, [ | ||
'json' => $this->getPayload(), | ||
]); | ||
|
||
return $response->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Flexpay\Response; | ||
|
||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class MerchantPayOutResponse extends FlexpayResponse | ||
{ | ||
public function getResponse(ResponseInterface $response): array | ||
{ | ||
try { | ||
$dataResponse = $response->toArray(); | ||
|
||
if (isset($dataResponse['code']) && $dataResponse['code'] === '200') { | ||
return [ | ||
'status' => 'Transaction sent successfully.', | ||
'message' => $dataResponse['message'] ?? 'Transaction succeeded', | ||
'transaction_id' => $dataResponse['transactionId'] ?? null, | ||
]; | ||
} | ||
|
||
return [ | ||
'status' => 'Transaction failed.', | ||
'message' => $dataResponse['message'] ?? 'Transaction failed', | ||
'code' => $dataResponse['code'] ?? 'Unknown error', | ||
]; | ||
|
||
} catch (\Exception $e) { | ||
return [ | ||
'status' => 'Error processing response.', | ||
'message' => 'Error: ' . $e->getMessage(), | ||
]; | ||
} | ||
} | ||
} |