Skip to content

Commit

Permalink
Check balance before withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann NGALLE committed Jan 24, 2022
1 parent 6911bbd commit 80f0c7d
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function saved(DefResponse $response)

protected function reply(PaymentProviderGateway $gateway)
{
return $this->liteResponse($gateway->successful() ? config(Finance::FINANCE_CONFIG_NAME . '-code.request.SUCCESS') : config(Finance::FINANCE_CONFIG_NAME . '-code.request.FAILURE'), $gateway->getResponse(), $gateway->getMessage());
return $this->liteResponse($gateway->successful() ? config(Finance::FINANCE_CONFIG_NAME . '-code.request.SUCCESS') : config(Finance::FINANCE_CONFIG_NAME . '-code.request.FAILURE'), $gateway->getResponse()->toArray(), $gateway->getMessage());
}

protected function respondError($exception)
Expand Down
10 changes: 6 additions & 4 deletions src/Http/Payment/DohonePaymentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Exception;
use Illuminate\Http\Request;
use NYCorp\Finance\Http\Core\Finance;
use NYCorp\Finance\Models\FinanceProviderGatewayResponse;
use NYCorp\Finance\Models\FinanceTransaction;
use NYCorp\Finance\Traits\FinanceProviderTrait;
use NYCorp\Finance\Traits\PaymentProviderTrait;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function deposit(FinanceTransaction $transaction): PaymentProviderGateway
$result = $api->get();
$this->successful = $result->isSuccess();
$this->message = $result->getMessage();
$this->response = ["errors" => $result->getErrors(), "sms_verification_required" => $result->shouldVerifySMS(), "payment_url" => $result->getPaymentUrl()];
$this->response = new FinanceProviderGatewayResponse($transaction, $this->getWallet($transaction)->id, $result->getErrors(), $result->shouldVerifySMS(), $result->getPaymentUrl());
return $this;
}

Expand All @@ -65,7 +66,7 @@ public function withdrawal(FinanceTransaction $transaction): PaymentProviderGate
$this->successful = $result->isSuccess();
$this->setTransaction($transaction);
$this->message = $result->getMessage();
$this->response = ["errors" => $result->getErrors(), "sms_verification_required" => $result->shouldVerifySMS(), "payment_url" => $result->getPaymentUrl()];
$this->response = new FinanceProviderGatewayResponse($transaction, $this->getWallet($transaction)->id, $result->getErrors(), $result->shouldVerifySMS(), $result->getPaymentUrl());
if ($this->successful()) {
$this->message = "Well Done";
$this->setExternalId($result->getMessage());
Expand All @@ -79,7 +80,7 @@ public function onDepositSuccess(Request $request): PaymentProviderGateway
if (empty($this->transaction)) {
$this->message = "Order not found !";
$this->successful = false;
$this->response = $request->all();
$this->response = new FinanceProviderGatewayResponse(null, null, $request->all());
return $this;
}

Expand All @@ -90,6 +91,7 @@ public function onDepositSuccess(Request $request): PaymentProviderGateway
$this->message = "Well Done";
$this->setExternalId($data["idReqDoh"]);
}
$this->response = new FinanceProviderGatewayResponse($data["rI"], null, $request->all());
} catch (Exception $exception) {
$this->message = $exception->getMessage();
}
Expand All @@ -109,7 +111,7 @@ public function SMSConfirmation($code, $phone): PaymentProviderGateway
->get();
$this->successful = $result->isSuccess();
$this->message = $result->getMessage();
$this->response = ["errors" => $result->getErrors(), "sms_verification_required" => $result->shouldVerifySMS(), "payment_url" => $result->getPaymentUrl()];
$this->response = new FinanceProviderGatewayResponse(null, null, $result->getErrors(), $result->shouldVerifySMS(), $result->getPaymentUrl());
return $this;
}
}
13 changes: 12 additions & 1 deletion src/Http/Payment/PaymentProviderGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use NYCorp\Finance\Http\Core\Finance;
use NYCorp\Finance\Models\FinanceProvider;
use NYCorp\Finance\Models\FinanceProviderGatewayResponse;
use NYCorp\Finance\Models\FinanceTransaction;
use NYCorp\Finance\Models\FinanceWallet;
use NYCorp\Finance\Scope\InvalidWalletScope;

class PaymentProviderGateway
{
Expand Down Expand Up @@ -119,7 +122,7 @@ public function isWithdrawalRealTime(): bool
/**
* @return mixed
*/
public function getResponse()
public function getResponse(): FinanceProviderGatewayResponse
{
return $this->response;
}
Expand Down Expand Up @@ -155,4 +158,12 @@ protected function setExternalId($externalId): void
{
$this->transaction->external_id = $externalId;
}

protected function getWallet(FinanceTransaction $transaction)
{
$wallet = FinanceWallet::withoutGlobalScope(InvalidWalletScope::class)->where("finance_transaction_id",$transaction->id)->first();
if (empty($wallet))
$wallet = new FinanceWallet();
return $wallet;
}
}
130 changes: 130 additions & 0 deletions src/Models/FinanceProviderGatewayResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php


namespace NYCorp\Finance\Models;


class FinanceProviderGatewayResponse
{
protected $transaction;
protected $wallet;
protected $error;
protected $smsVerificationRequired;
protected $paymentUrl;

/**
* FinanceProviderGatewayResponse constructor.
*
* @param $transaction
* @param $wallet
* @param $error
* @param $smsVerificationRequired
* @param $paymentUrl
*/
public function __construct($transaction = null, $wallet = null, $error = null, $smsVerificationRequired = false, $paymentUrl = null)
{
$this->transaction = $transaction;
$this->wallet = $wallet;
$this->error = $error;
$this->smsVerificationRequired = $smsVerificationRequired;
$this->paymentUrl = $paymentUrl;
}

public static function fromArray($array)
{

}


/**
* @return mixed
*/
public function getTransaction()
{
return $this->transaction;
}

/**
* @param mixed $transaction
*/
public function setTransaction($transaction): void
{
$this->transaction = $transaction;
}

/**
* @return mixed
*/
public function getWallet()
{
return $this->wallet;
}

/**
* @param mixed $wallet
*/
public function setWallet($wallet): void
{
$this->wallet = $wallet;
}

/**
* @return mixed
*/
public function getError()
{
return $this->error;
}

/**
* @param mixed $error
*/
public function setError($error): void
{
$this->error = $error;
}

/**
* @return mixed
*/
public function getSmsVerificationRequired()
{
return $this->smsVerificationRequired;
}

/**
* @param mixed $smsVerificationRequired
*/
public function setSmsVerificationRequired($smsVerificationRequired): void
{
$this->smsVerificationRequired = $smsVerificationRequired;
}

/**
* @return mixed
*/
public function getPaymentUrl()
{
return $this->paymentUrl;
}

/**
* @param mixed $paymentUrl
*/
public function setPaymentUrl($paymentUrl): void
{
$this->paymentUrl = $paymentUrl;
}


public function toArray(): array
{
return [
"transaction" => $this->transaction,
"wallet" => $this->wallet,
"error" => $this->error,
"smsVerificationRequired" => $this->smsVerificationRequired,
"paymentUrl" => $this->paymentUrl,
];
}
}

0 comments on commit 80f0c7d

Please sign in to comment.