1. Go to this page and download the library: Download nycorp/finance library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function canWithdraw(float $amount, bool $forceBalanceCalculation): bool
{
//EX : Set to true because the account is debited only when the service is consumed
return true;
}
public function getCurrency()
{
// Implement your logic to get currency here the default value is set in the finance config file
return \NYCorp\Finance\Http\Core\ConfigReader::getDefaultCurrency();
}
/**
* Handle the event.
*/
public function handle(FinanceTransactionSuccessEvent $event): void
{
# In case you handle multiple model
match (get_class($event->model)) {
Model1::class => $this->handleModel1($event),
Model1::class => $this->hanleModel2($event),
default => static fn() => Log::warning("FinanceTransactionSuccessEvent Model not handle")
};
}
use NYCorp\Finance\Http\Payment\PaymentProviderGateway;
class CustomPaymentProvider extends PaymentProviderGateway
{
public static function getName(): string
{
return 'CustomProvider';
}
public function deposit(FinanceTransaction $transaction): PaymentProviderGateway
{
#Your custom logic here
//use this url for callback
$callbackUrl = self::depositNotificationUrl(self::getId());
$response = Http::post('https://api-checkout/v2/payment', $formData);
$this->successful = $response->successful();
$this->message = $response->json('description');
$this->response = new FinanceProviderGatewayResponse($transaction, $this->getWallet($transaction)->id, $response->body(), false, $response->json('data.payment_url'));
return $this;
}
private function normalizeAmount($amount, string $id): int
{
return ceil($amount * Cache::get($id, 655));
}
public static function getId(): string
{
return 'MY_PROVIDER_ID';
}
public function withdrawal(FinanceTransaction $transaction): PaymentProviderGateway
{
#Your custom logic here
//use this url for callback
$callbackUrl = self::depositNotificationUrl(self::getId());
$response = Http::post('https://api-checkout/v2/payment', $formData);
$this->successful = $response->successful();
$this->message = $response->json('description');
$this->response = new FinanceProviderGatewayResponse($transaction, $this->getWallet($transaction)->id, $response->body(), false, $response->json('data.payment_url'));
return $this;
}
public function onDepositSuccess(Request $request): PaymentProviderGateway
{
Log::debug("**Payment** | " . self::getId() . ": callback " . $request->cpm_trans_id, $request->all());
if ($this->findTransaction($request, 'cpm_trans_id') === null) {
return $this;
}
return $this;
}
public function onWithdrawalSuccess(Request $request): PaymentProviderGateway
{
return $this;
}
protected function findTransaction(Request $request, string $key): ?FinanceTransaction
{
$transactionId = Arr::get($request->all(), $key);
$this->transaction = FinanceTransaction::find($transactionId);
if (empty($this->transaction)) {
$id = self::getId();
Log::error("**Payment** | $id : order not found $transactionId");
$this->message = "Order not found !";
$this->successful = false;
$this->response = new FinanceProviderGatewayResponse(null, null, $request->all());
}
return $this->transaction;
}
}
$response = \Nycorp\LiteApi\Response\DefResponse::parse(User::first()->withdrawal(DefaultPaymentProvider::getId(), 12, $description));
$response->getBody(); // get the body of the response
$response->isSuccess(); // get the success state as boolean
$response->getMessage(); // get response message