diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index 7501bfd1..1503f6ee 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -2,6 +2,8 @@ namespace App\Classes\Modules\Billplzs\ControllersLogic; +use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; +use App\Classes\Modules\Wallets\Services\UpdatesWallet; use App\Classes\Exceptions\ResourceNotFoundException; use App\Http\Resources\TransactionResource; @@ -32,6 +34,8 @@ class CallbackBillplzLogic /** @var UpdatesTransactionStatus */ private $updatesTransactionStatus; + /** @var UpdatesWallet */ + private $updatesWallet; /** * CreateBookingLogic constructor. @@ -39,11 +43,12 @@ class CallbackBillplzLogic * @param FetchesTransaction $fetchesTransaction * @param UpdatesTransactionStatus $updatesTransactionStatus */ - public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus) + public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWallet $updatesWallet) { $this->getBillplzBill = $getBillplzBill; $this->fetchesTransaction = $fetchesTransaction; $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->updatesWallet = $updatesWallet; } @@ -55,7 +60,6 @@ class CallbackBillplzLogic */ public function execute(Request $request) { - $billplzXSignatureObject = new BillplzXSignatureObject($request); if(!$billplzXSignatureObject->isValidSignature()){ @@ -70,6 +74,16 @@ class CallbackBillplzLogic if($billPlz->state === 'paid') { $status = ApprovalStatus::APPROVED; + if ( + $transaction->owner_type == 'App\Models\Wallet' && + $transaction->status == ApprovalStatus::PENDING_VERIFICATION + ) { + $wallet = $transaction->owner; + $updateWalletAmount = $wallet->amount + $transaction->amount; + $walletOject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + $wallet = $this->updatesWallet->execute($wallet, $walletOject); + } + } if($billPlz->state === 'due') { diff --git a/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php new file mode 100644 index 00000000..e2941c3c --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/CreditWalletLogic.php @@ -0,0 +1,107 @@ + 'Credit into Company Wallet', + 'message' => 'You have successfully credit company wallet' + ]; + } + + + /** @var FetchesWallet */ + private $fetchesWallet; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** @var UpdatesWallet */ + private $updatesWallet; + + /** + * CreateWalletLogic constructor. + * @param FetchesWallet $fetchesWallet + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + * @param UpdatesWallet $updatesWallet + */ + public function __construct( + FetchesWallet $fetchesWallet, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesTransaction $createsTransaction, + UpdatesWallet $updatesWallet + ) + { + $this->fetchesWallet = $fetchesWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->updatesWallet = $updatesWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); + + $billNumber = $this->generatesTransactionBillNumber->execute('DEBIT-NOTE-'); + + $transaction_object = new TransactionObject( + $billNumber, + TransactionType::CREDIT_NOTE, + 1, + $wallet->company->id, + 1, + PaymentMethodType::WALLET, + $request->input('amount'), + $request->input('amount'), + 1, + 1, + 1, + 0, + 0, + null, + ApprovalStatus::PENDING_SUBMISSION, + [] + ); + + $transaction = $this->createsTransaction->execute($wallet, $transaction_object); + $updateWalletAmount = $wallet->amount + $transaction->amount; + + $walletOject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + + $wallet = $this->updatesWallet->execute($wallet, $walletOject); + + return $this->resourceResponse(new WalletResource($wallet)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php new file mode 100644 index 00000000..9437b320 --- /dev/null +++ b/app/Classes/Modules/Wallets/ControllersLogic/DebitWalletLogic.php @@ -0,0 +1,107 @@ + 'Debit into Company Wallet', + 'message' => 'You have successfully debit company wallet' + ]; + } + + + /** @var FetchesWallet */ + private $fetchesWallet; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesTransaction */ + private $createsTransaction; + + /** @var UpdatesWallet */ + private $updatesWallet; + + /** + * CreateWalletLogic constructor. + * @param FetchesWallet $fetchesWallet + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + * @param UpdatesWallet $updatesWallet + */ + public function __construct( + FetchesWallet $fetchesWallet, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesTransaction $createsTransaction, + UpdatesWallet $updatesWallet + ) + { + $this->fetchesWallet = $fetchesWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->updatesWallet = $updatesWallet; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws ErrorException + */ + public function logic(Request $request) : JsonResponse + { + $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); + + $billNumber = $this->generatesTransactionBillNumber->execute('DEBIT-NOTE-'); + + $transaction_object = new TransactionObject( + $billNumber, + TransactionType::DEBIT_NOTE, + 1, + $wallet->company->id, + 1, + PaymentMethodType::WALLET, + $request->input('amount'), + $request->input('amount'), + 1, + 1, + 1, + 0, + 0, + null, + ApprovalStatus::PENDING_SUBMISSION, + [] + ); + + $transaction = $this->createsTransaction->execute($wallet, $transaction_object); + $updateWalletAmount = $wallet->amount - $transaction->amount; + + $walletOject = new WalletObject($wallet->company->id, $wallet->currency_id, $wallet->code, $updateWalletAmount); + + $wallet = $this->updatesWallet->execute($wallet, $walletOject); + + return $this->resourceResponse(new WalletResource($wallet)); + } +} diff --git a/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php index 8497ec32..0c51118a 100644 --- a/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php +++ b/app/Classes/Modules/Wallets/ControllersLogic/TopUpWalletLogic.php @@ -3,15 +3,18 @@ namespace App\Classes\Modules\Wallets\ControllersLogic; use App\Classes\General\Abstracts\AbstractControllerLogic; - use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject; -use App\Classes\Modules\Wallets\Services\ListsWallet; -use App\Classes\Modules\Wallets\Services\FetchesWallet; +use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; +use App\Classes\Modules\Companies\Services\FetchesCompany; +use App\Classes\Modules\Wallets\Services\CreatesWallet; +use App\Classes\Modules\Wallets\Services\GeneratesWalletCode; +use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; +use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill; +use App\Classes\Modules\Transactions\Services\CreatesTransaction; use App\Classes\ValueObjects\Constants\TransactionType; - -use App\Classes\Modules\Wallets\Standards\Rules\CanTopUpWallet; +use App\Classes\ValueObjects\Constants\PaymentMethodType; +use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Http\Resources\WalletResource; -use App\Classes\Modules\Transactions\Processors\CreateWalletTransactionProcessor; use ErrorException; use Illuminate\Http\JsonResponse; @@ -30,26 +33,50 @@ class TopUpWalletLogic extends AbstractControllerLogic ]; } - /** @var FetchesWallet */ - private $fetchesWallet; - /** @var CanTopUpWallet */ - private $canTopUpWallet; + /** @var FetchesCompany */ + private $fetchesCompany; - /** @var CreateWalletTransactionProcessor */ - private $createWalletTransactionProcessor; + /** @var GeneratesWalletCode */ + private $generatesWalletCode; + + /** @var CreatesWallet */ + private $createsWallet; + + /** @var GeneratesTransactionBillNumber */ + private $generatesTransactionBillNumber; + + /** @var CreatesWalletTransaction */ + private $createsWalletTransaction; + + /** @var CreatesBillplzBill */ + private $createsBillplzBill; + + /** @var CreatesTransaction */ + private $createsTransaction; /** * CreateWalletLogic constructor. - * @param CreatesWallet $createsWallet + * @param FetchesCompany $fetchesCompany * @param GeneratesWalletCode $generatesWalletCode - * @param CanCreateCompanyWallet $canCreateCompanyWallet + * @param CreatesWallet $createsWallet + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber */ - public function __construct(CanTopUpWallet $canTopUpWallet, FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor) + public function __construct( + FetchesCompany $fetchesCompany, + GeneratesWalletCode $generatesWalletCode, + CreatesWallet $createsWallet, + GeneratesTransactionBillNumber $generatesTransactionBillNumber, + CreatesBillplzBill $createsBillplzBill, + CreatesTransaction $createsTransaction + ) { - $this->canTopUpWallet = $canTopUpWallet; - $this->fetchesWallet = $fetchesWallet; - $this->createWalletTransactionProcessor = $createWalletTransactionProcessor; + $this->fetchesCompany = $fetchesCompany; + $this->generatesWalletCode = $generatesWalletCode; + $this->createsWallet = $createsWallet; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsBillplzBill = $createsBillplzBill; + $this->createsTransaction = $createsTransaction; } /** @@ -59,13 +86,45 @@ class TopUpWalletLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]); + $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); + if (!$company->wallets()->first()) { + $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute()); + $wallet = $this->createsWallet->execute($object, $company); + } - $walletOject = new WalletObject( $wallet->company->id, $wallet->currency_id, $wallet->code, $request->input('amount')); + $user = $company->employees()->first(); + $billNumber = $this->generatesTransactionBillNumber->execute('TOPUP-'); + + $billPlzBill = $this->createsBillplzBill->execute( + $user->name, + $user->email, + 'This payment is made for wallet topup. ' . $company->reference, + $request->input('amount'), + $billNumber, + $request->input('bank_code') + ); - $this->canTopUpWallet->passes($walletOject); + $transaction_object = new TransactionObject( + $billNumber, + TransactionType::TOP_UP, + 1, + $company->id, + 1, + PaymentMethodType::PAYMENT_GATEWAY, + $request->input('amount'), + $request->input('amount'), + 1, + 1, + 1, + 0, + 0, + null, + ApprovalStatus::PENDING_SUBMISSION, + [], + $billPlzBill->id + ); - $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::TOP_UP); + $transaction = $this->createsTransaction->execute($company->wallets()->first(), $transaction_object); return $this->resourceResponse(new WalletResource($wallet)); } diff --git a/app/Classes/Modules/Wallets/Services/CreatesWallet.php b/app/Classes/Modules/Wallets/Services/CreatesWallet.php index c114b9de..2a927e6d 100644 --- a/app/Classes/Modules/Wallets/Services/CreatesWallet.php +++ b/app/Classes/Modules/Wallets/Services/CreatesWallet.php @@ -21,8 +21,6 @@ class CreatesWallet extends AbstractUpdateRelationshipRecord $model->code = $object->getCode(); $model->currency_id = $object->getCurrency(); - return $this->handler($company->wallets(), $model); - } } diff --git a/app/Classes/ValueObjects/Constants/TransactionType.php b/app/Classes/ValueObjects/Constants/TransactionType.php index 698abd76..040f63fa 100644 --- a/app/Classes/ValueObjects/Constants/TransactionType.php +++ b/app/Classes/ValueObjects/Constants/TransactionType.php @@ -24,5 +24,7 @@ final class TransactionType { public const CREDIT_NOTE = 9; + public const DEBIT_NOTE = 11; + public const WITHDRAW = 10; } diff --git a/app/Http/Controllers/Wallets/CreditWalletController.php b/app/Http/Controllers/Wallets/CreditWalletController.php new file mode 100644 index 00000000..b3143588 --- /dev/null +++ b/app/Http/Controllers/Wallets/CreditWalletController.php @@ -0,0 +1,15 @@ +execute($request); + } +} diff --git a/app/Http/Controllers/Wallets/DebitWalletController.php b/app/Http/Controllers/Wallets/DebitWalletController.php new file mode 100644 index 00000000..7feafb39 --- /dev/null +++ b/app/Http/Controllers/Wallets/DebitWalletController.php @@ -0,0 +1,15 @@ +execute($request); + } +} diff --git a/app/Http/Resources/CompanyResource.php b/app/Http/Resources/CompanyResource.php index 54ab047d..5488df0e 100644 --- a/app/Http/Resources/CompanyResource.php +++ b/app/Http/Resources/CompanyResource.php @@ -60,10 +60,8 @@ class CompanyResource extends JsonResource $segment = SegmentConstant::where('reference', SegmentConstants::SUPPLIER_CURRENCIES)->where('detail->id', $this->id)->first(); return $segment ? CurrencyResource::collection(Currency::whereIn('id', $segment->detail->currencies)->get()) : []; }), + 'wallet' => WalletResource::collection($this->wallets), 'created_at' => $this->created_at->format('d-m-Y') - - ]; - } } diff --git a/app/Http/Resources/WalletResource.php b/app/Http/Resources/WalletResource.php index 37ff5dbd..39c634eb 100644 --- a/app/Http/Resources/WalletResource.php +++ b/app/Http/Resources/WalletResource.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Classes\ValueObjects\Constants\TransactionType; use Illuminate\Http\Resources\Json\JsonResource; class WalletResource extends JsonResource @@ -19,7 +20,17 @@ class WalletResource extends JsonResource 'code' => $this->code, 'currency_id' => $this->currency_id, 'amount' => (double) $this->amount, - 'company_id' => (int) $this->company_id + 'company_id' => (int) $this->owner->id, + 'transactions' => $this->transactions()->orderBy('id', 'DESC')->get(), + 'topup_transactions' => $this->transactions()->where('type', TransactionType::TOP_UP)->orderBy('id', 'DESC')->get(), + + // 'transaction' => $this->whenLoaded('transactions', function() { + // return [ + // 'topup' => $this->transactions + // ]; + // }), + // 'transaction' // all + // 'top_transaction' // only topup ]; } } diff --git a/app/Models/Wallet.php b/app/Models/Wallet.php index 52d8f718..29c3ddc9 100644 --- a/app/Models/Wallet.php +++ b/app/Models/Wallet.php @@ -2,13 +2,14 @@ namespace App\Models; +use App\Classes\General\Interfaces\Transactionable; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; -class Wallet extends AbstractModel +class Wallet extends AbstractModel implements Transactionable { use SoftDeletes; diff --git a/routes/wallet.php b/routes/wallet.php index 7ad2a479..80290c52 100644 --- a/routes/wallet.php +++ b/routes/wallet.php @@ -5,8 +5,10 @@ use Illuminate\Support\Facades\Route; Route::group(['prefix' => 'wallets', 'namespace' => 'Wallets', 'as' => 'wallet.'], function () { Route::get('/', 'ListWalletController@list')->name('list'); Route::post('/create', 'CreateWalletController@create')->name('create'); - Route::post('/topup', 'TopUpWalletController@topUp')->name('topup'); - Route::post('/withdraw', 'WithdrawWalletController@withdraw')->name('withdraw'); - Route::put('/{transaction_id}/update-status/{status}', 'UpdateStatusWalletController@updateStatus')->where('status', 'approve|reject')->name('approval'); + Route::post('/topup', 'TopUpWalletController@topUp')->name('topup'); // user + Route::post('/debit', 'DebitWalletController@debit')->name('debit'); //admin - + Route::post('/credit', 'CreditWalletController@credit')->name('credit'); // admin + + + Route::put('/{transaction_id}/update-status/{status}', 'UpdateStatusWalletController@updateStatus')->where('status', 'approve|reject')->name('approval'); });