Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/PaymentProvider.php
<?php | <?php | ||||
namespace App\Providers; | namespace App\Providers; | ||||
use App\Transaction; | |||||
use App\Payment; | use App\Payment; | ||||
use App\Wallet; | use App\Wallet; | ||||
abstract class PaymentProvider | abstract class PaymentProvider | ||||
{ | { | ||||
public const STATUS_OPEN = 'open'; | public const STATUS_OPEN = 'open'; | ||||
public const STATUS_CANCELED = 'canceled'; | public const STATUS_CANCELED = 'canceled'; | ||||
public const STATUS_PENDING = 'pending'; | public const STATUS_PENDING = 'pending'; | ||||
public const STATUS_AUTHORIZED = 'authorized'; | public const STATUS_AUTHORIZED = 'authorized'; | ||||
public const STATUS_EXPIRED = 'expired'; | public const STATUS_EXPIRED = 'expired'; | ||||
public const STATUS_FAILED = 'failed'; | public const STATUS_FAILED = 'failed'; | ||||
public const STATUS_PAID = 'paid'; | public const STATUS_PAID = 'paid'; | ||||
public const TYPE_ONEOFF = 'oneoff'; | public const TYPE_ONEOFF = 'oneoff'; | ||||
public const TYPE_RECURRING = 'recurring'; | public const TYPE_RECURRING = 'recurring'; | ||||
public const TYPE_MANDATE = 'mandate'; | public const TYPE_MANDATE = 'mandate'; | ||||
public const TYPE_REFUND = 'refund'; | |||||
public const TYPE_CHARGEBACK = 'chargeback'; | |||||
/** const int Minimum amount of money in a single payment (in cents) */ | /** const int Minimum amount of money in a single payment (in cents) */ | ||||
public const MIN_AMOUNT = 1000; | public const MIN_AMOUNT = 1000; | ||||
/** | /** | ||||
* Factory method | * Factory method | ||||
* | * | ||||
* @param \App\Wallet|string|null $provider_or_wallet | * @param \App\Wallet|string|null $provider_or_wallet | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | protected function storePayment(array $payment, $wallet_id): Payment | ||||
$db_payment->amount = $payment['amount'] ?? 0; | $db_payment->amount = $payment['amount'] ?? 0; | ||||
$db_payment->type = $payment['type']; | $db_payment->type = $payment['type']; | ||||
$db_payment->wallet_id = $wallet_id; | $db_payment->wallet_id = $wallet_id; | ||||
$db_payment->provider = $this->name(); | $db_payment->provider = $this->name(); | ||||
$db_payment->save(); | $db_payment->save(); | ||||
return $db_payment; | return $db_payment; | ||||
} | } | ||||
/** | |||||
* Deduct an amount of pecunia from the wallet. | |||||
* Creates a payment and transaction records for the refund/chargeback operation. | |||||
* | |||||
* @param \App\Wallet $wallet A wallet object | |||||
* @param array $refund A refund or chargeback data (id, type, amount, description) | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function storeRefund(Wallet $wallet, array $refund): void | |||||
{ | |||||
if (empty($refund) || empty($refund['amount'])) { | |||||
return; | |||||
} | |||||
$wallet->balance -= $refund['amount']; | |||||
$wallet->save(); | |||||
if ($refund['type'] == self::TYPE_CHARGEBACK) { | |||||
$transaction_type = Transaction::WALLET_CHARGEBACK; | |||||
} else { | |||||
$transaction_type = Transaction::WALLET_REFUND; | |||||
} | |||||
Transaction::create([ | |||||
'object_id' => $wallet->id, | |||||
'object_type' => Wallet::class, | |||||
'type' => $transaction_type, | |||||
'amount' => $refund['amount'], | |||||
'description' => $refund['description'] ?? '', | |||||
]); | |||||
$refund['status'] = self::STATUS_PAID; | |||||
$refund['amount'] *= -1; | |||||
$this->storePayment($refund, $wallet->id); | |||||
} | |||||
} | } |