Skip to content

Commit

Permalink
Merge pull request #5 from lepresk/doc
Browse files Browse the repository at this point in the history
bug fixes
  • Loading branch information
lepresk authored Apr 22, 2024
2 parents 9c5a89e + b088543 commit fb9ee2e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Transaction
* @param string|null $payerMessage
* @param string|null $payeeNote
* @param string $status
* @param string|null $reason
*/
public function __construct(?string $financialTransactionId, ?string $externalId, ?string $amount, string $currency, array $payer, ?string $payerMessage, ?string $payeeNote, string $status, ?string $reason)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Products/CollectionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CollectionApi extends ApiProduct
* ### Sample usage
*
* ```
* $request = new \Lepresk\MomoApi\Collection\PaymentRequest(
* $request = new Lepresk\MomoApi\Models\PaymentRequest(
* 2500,
* 'EUR',
* 'ORDER-10',
Expand All @@ -53,10 +53,8 @@ public function requestToPay(PaymentRequest $paymentRequest): string

$token = $this->getAccessToken();

$data = $paymentRequest->toArray();

$response = $this->client->request('POST', '/collection/v1_0/requesttopay', [
'json' => $data,
'json' => $paymentRequest->toArray(),
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->getSubscriptionKey(),
'X-Reference-Id' => $xReferenceId,
Expand Down
108 changes: 103 additions & 5 deletions src/Products/DisbursementApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

use Lepresk\MomoApi\ApiProduct;
use Lepresk\MomoApi\ApiToken;
use Lepresk\MomoApi\Config;
use Lepresk\MomoApi\Exceptions\ExceptionFactory;
use Lepresk\MomoApi\Exceptions\MomoException;
use Lepresk\MomoApi\Models\AccountBalance;
use Lepresk\MomoApi\Models\PaymentRequest;
use Lepresk\MomoApi\Models\Transaction;
use Lepresk\MomoApi\Utilities;
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;

class DisbursementApi extends ApiProduct
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public function getAccessToken(): ApiToken
$response = $this->client->request('POST', "/disbursement/token/", [
'auth_basic' => [$this->config->getApiUser(), $this->config->getApiKey()],
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey,
'Ocp-Apim-Subscription-Key' => $this->getSubscriptionKey(),
],
]);

Expand All @@ -65,13 +66,13 @@ public function getAccessToken(): ApiToken
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function getAccountBalance()
public function getAccountBalance(): AccountBalance
{
$token = $this->getAccessToken();

$response = $this->client->request('GET', '/disbursement/v1_0/account/balance', [
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey,
'Ocp-Apim-Subscription-Key' => $this->getSubscriptionKey(),
'X-Target-Environment' => $this->environment,
'Authorization' => 'Bearer ' . $token->getAccessToken(),
]
Expand All @@ -83,4 +84,101 @@ public function getAccountBalance()

throw ExceptionFactory::create($response);
}

/**
* Deposit an amount from the owner’s account to a payee account.
*
* ### Sample usage
*
* ```
* $request = new \Lepresk\MomoApi\Models\PaymentRequest(
* 5000,
* 'XAF',
* 'ORDER-10',
* '46733123454',
* 'Payment message',
* 'A note',
* );
* $paymentId = $momo->disbursement()->requestToPay($request);
* ```
* @param PaymentRequest $paymentRequest
* @return string payment reference id
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws MomoException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function getDepositV1(PaymentRequest $paymentRequest): string
{
$token = $this->getAccessToken();

$xReferenceId = Utilities::guidv4();

$response = $this->client->request('POST', '/disbursement/v1_0/account/deposit', [
'json' => $paymentRequest->toArray(),
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->getSubscriptionKey(),
'X-Reference-Id' => $xReferenceId,
'X-Target-Environment' => $this->environment,
'Authorization' => 'Bearer ' . $token->getAccessToken(),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
]
]);

$responseCode = $response->getStatusCode();
if ($responseCode === 202) {
return $xReferenceId;
}

throw ExceptionFactory::create($response);
}

/**
* Get the status of a deposit. X-Reference-Id that was passed in the post is used as reference to the request.
*
* ### Sample usage
*
* ```
* $depositId = "07a461a4-e721-462b-81c6-b9aa2f8abf06";
* try {
* $result = $momo->disbursement()->checkPayment($paymentId);
* if($result->isSuccessful()) {
* echo "Payment successful";
* $result->getAmount(); // 1500
* $result->getPayer(); // 46733123454
* }
* } catch (BadRessourceExeption|InternalServerErrorException|RessourceNotFoundException $e) {
* // Request failed, do something else
* }
* ```
*
* @param string $depositId UUID of transaction to get result. Reference id used when creating the request to pay.
* @return Transaction
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws MomoException
*/
public function getDepositStatus(string $depositId): Transaction
{
$token = $this->getAccessToken();
$response = $this->client->request('GET', '/disbursement/v1_0/deposit/' . $depositId, [
'headers' => [
'Ocp-Apim-Subscription-Key' => $this->getSubscriptionKey(),
'X-Target-Environment' => $this->environment,
'Authorization' => 'Bearer ' . $token->getAccessToken(),
]
]);

if ($response->getStatusCode() === 200) {
return Transaction::parse($response->toArray());
}

throw ExceptionFactory::create($response);
}
}

0 comments on commit fb9ee2e

Please sign in to comment.