diff --git a/.env.example b/.env.example index 6367c9b8..f3d4fd56 100644 --- a/.env.example +++ b/.env.example @@ -56,5 +56,3 @@ BILLPLZ_BASE_URL="https://www.billplz-sandbox.com" BILLPLZ_API_KEY="0fa4c710-761b-4a7a-a501-c2c2d02643d5" BILLPLZ_X_SIGNATURE_KEY="S-pbNVthVRsvnPfZlgLwqqOg" BILLPLZ_COLLECTION_ID="hev2wdjy" -BILLPLZ_REDIRECT_URL="http://localhost:9003/bookings/billplz" -BILLPLZ_CALLBACK_URL="localhost:9003/api/v1/billplz/callback" \ No newline at end of file diff --git a/app/Classes/General/Abstracts/AbstractControllerLogic.php b/app/Classes/General/Abstracts/AbstractControllerLogic.php index 2fdd0dab..d68c9db8 100644 --- a/app/Classes/General/Abstracts/AbstractControllerLogic.php +++ b/app/Classes/General/Abstracts/AbstractControllerLogic.php @@ -61,6 +61,7 @@ abstract class AbstractControllerLogic return $response; } catch (ErrorException|GeneralExceptions $exception){ + dd($exception); return (new ApiResponseObject($this->getNotificationTitle().' failed', $exception->getMessage(), $exception->getCode() ? $exception->getCode() : HttpStatus::SERVER_ERROR))->handler(); diff --git a/app/Classes/General/Eloquent/AbstractUpdateRecord.php b/app/Classes/General/Eloquent/AbstractUpdateRecord.php index 44129c92..17690ce4 100644 --- a/app/Classes/General/Eloquent/AbstractUpdateRecord.php +++ b/app/Classes/General/Eloquent/AbstractUpdateRecord.php @@ -17,7 +17,6 @@ abstract class AbstractUpdateRecord */ public function handler(Model $model){ try{ - if($model->save()){ return $model; } } catch (QueryException $exception){ diff --git a/app/Classes/General/Eloquent/Filters/IsPublished.php b/app/Classes/General/Eloquent/Filters/IsPublished.php index 9b5b23c1..3e9c8ca4 100644 --- a/app/Classes/General/Eloquent/Filters/IsPublished.php +++ b/app/Classes/General/Eloquent/Filters/IsPublished.php @@ -5,7 +5,7 @@ namespace App\Classes\General\Eloquent\Filters; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; -class isPublished implements Filter +class IsPublished implements Filter { /** diff --git a/app/Classes/General/Eloquent/Filters/PaymentReference.php b/app/Classes/General/Eloquent/Filters/PaymentReference.php index 10c1fa49..5f5d53f5 100644 --- a/app/Classes/General/Eloquent/Filters/PaymentReference.php +++ b/app/Classes/General/Eloquent/Filters/PaymentReference.php @@ -14,7 +14,7 @@ class PaymentReference implements Filter */ public static function apply(Builder $builder, $value) { - return $builder->where('payment_reference', $value); + return $builder->where('payment_reference', '=', $value); } } \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/DataTransferObjects/FullNameObject.php b/app/Classes/Modules/Accounts/DataTransferObjects/FullNameObject.php index c17a8b72..59396c54 100644 --- a/app/Classes/Modules/Accounts/DataTransferObjects/FullNameObject.php +++ b/app/Classes/Modules/Accounts/DataTransferObjects/FullNameObject.php @@ -4,7 +4,7 @@ namespace App\Classes\Modules\Accounts\DataTransferObjects; use App\Classes\General\Interfaces\DataTransferObject; -class FullnameObject implements DataTransferObject +class FullNameObject implements DataTransferObject { /** @var string */ private $name; @@ -12,11 +12,6 @@ class FullnameObject implements DataTransferObject /** * UserObject constructor. * @param string $name - * @param string $email - * @param string $password - * @param string $passwordConfirmation - * @param int|null $type - * @param int|null $status */ public function __construct(string $name) { diff --git a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php index 8dece746..73f502b4 100644 --- a/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php +++ b/app/Classes/Modules/Billplzs/ControllersLogic/CallbackBillplzLogic.php @@ -2,8 +2,10 @@ namespace App\Classes\Modules\Billplzs\ControllersLogic; -use ErrorException; +use App\Classes\Exceptions\ResourceNotFoundException; +use App\Http\Resources\TransactionResource; +use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; @@ -14,19 +16,11 @@ use App\Classes\Modules\Billplzs\DataTransferObjects\BillplzXSignatureObject; use App\Classes\General\Abstracts\AbstractControllerLogic; use App\Classes\Modules\Transactions\Services\FetchesTransaction; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; +use Illuminate\Support\Facades\Auth; -class CallbackBillplzLogic extends AbstractControllerLogic +class CallbackBillplzLogic { - /** - * @return array - */ - protected function notification():array { - return [ - 'title' => 'Callback Billplz', - 'message' => 'You have successfully receive Billplz callback' - ]; - } /** @var GetBillplzBill */ private $getBillplzBill; @@ -40,7 +34,7 @@ class CallbackBillplzLogic extends AbstractControllerLogic /** * CreateBookingLogic constructor. - * @param CreateGetBillplzBillsBillplzBill $getBillplzBill + * @param GetBillplzBill $getBillplzBill * @param FetchesTransaction $fetchesTransaction * @param UpdatesTransactionStatus $updatesTransactionStatus */ @@ -54,27 +48,29 @@ class CallbackBillplzLogic extends AbstractControllerLogic /** * @param Request $request - * @return JsonResponse - * @throws \App\Classes\Exceptions\AccessForbiddenException - * @throws \App\Classes\Exceptions\MalformedRequestException - * @throws \App\Classes\Exceptions\RequestValidationException + * @return \Illuminate\Http\RedirectResponse + * @throws MalformedRequestException + * @throws ResourceNotFoundException */ - public function logic(Request $request) : JsonResponse + public function execute(Request $request) { $billplzXSignatureObject = new BillplzXSignatureObject($request); - if(!$billplzXSignatureObject->isValidSignature()) throw new MalformedRequestException('Unable to get correct response from billplz server.'); + if(!$billplzXSignatureObject->isValidSignature()) throw new MalformedRequestException('Billplz Payment validation failed.'); $billPlz = $this->getBillplzBill->execute($billplzXSignatureObject->getBillPlzId()); - if(!$billPlz) throw new MalformedRequestException('Unable to get correct response from billplz server.'); + if(!$billPlz) throw new ResourceNotFoundException('Billplz bill not found.'); $transaction = $this->fetchesTransaction->execute(['payment_reference' => $billplzXSignatureObject->getBillPlzId()]); - if($billPlz->state == 'paid') $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED); - - return $this->response(['data' => $billPlz]); + $this->updatesTransactionStatus->execute($transaction, $billPlz->state === 'paid' ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION); + + $token = Auth::fromUser(User::find(1)); + $request->headers->set('Authorization', 'Bearer '.$token); + return view('pages.payments_redirect', ['marking' => $transaction->booking->marking, 'payment_reference' => $transaction->payment_reference, 'status' => $billPlz->state === 'paid' ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION, 'amount' => $transaction->amount]); +// return redirect()->route('booking.details', ['marking' => $transaction->booking->marking, 'payment_status' => $billPlz->state === 'paid' ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION]); } } \ No newline at end of file diff --git a/app/Classes/Modules/Billplzs/DataTransferObjects/BillplzXSignatureObject.php b/app/Classes/Modules/Billplzs/DataTransferObjects/BillplzXSignatureObject.php index db3e3ad6..ec46e24d 100644 --- a/app/Classes/Modules/Billplzs/DataTransferObjects/BillplzXSignatureObject.php +++ b/app/Classes/Modules/Billplzs/DataTransferObjects/BillplzXSignatureObject.php @@ -4,6 +4,7 @@ namespace App\Classes\Modules\Billplzs\DataTransferObjects; use Illuminate\Http\Request; use App\Classes\General\Interfaces\DataTransferObject; +use function PHPSTORM_META\map; class BillplzXSignatureObject implements DataTransferObject { @@ -30,20 +31,19 @@ class BillplzXSignatureObject implements DataTransferObject { $this->request = $request; - $this->billPlzId = $request->id ? $request->id : $request->{'billplz[id]'}; + $this->billPlzId = $request->input('billplz')['id']; - $this->requestXSignature = $request->x_signature ? $request->x_signature : $request->{'billplz[x_signature]'}; + $this->requestXSignature = $request->input('billplz')['x_signature']; $this->_constructBillplzArray()->_natSortBillplzArray()->_constructBillplzString()->_computeBillplzXSignature(); } private function _constructBillplzArray(){ - foreach($this->request->all() as $key => $value){ - if($key != 'x_signature' && $key != 'billplz[x_signature]'){ - $key = str_replace(']', '', str_replace('[', '', $key)); - $this->billPlzConstructArray[] = $key.$value; - } - } + + $this->billPlzConstructArray = collect($this->request->input('billplz'))->forget('x_signature')->map(function($item, $key){ + return 'billplz'.$key.$item; + })->toArray(); + return $this; } @@ -98,7 +98,7 @@ class BillplzXSignatureObject implements DataTransferObject public function isValidSignature(): bool { - return $this->billPlzComputedXSignature == $this->requestXSignature ? true : false; + return $this->billPlzComputedXSignature === $this->requestXSignature ? true : false; } } \ No newline at end of file diff --git a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php index 607b45fa..26b2800b 100644 --- a/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/CreatesBillplzBill.php @@ -7,20 +7,27 @@ use App\Classes\Exceptions\MalformedRequestException; class CreatesBillplzBill { + /** - * @return \Illuminate\Database\Eloquent\Model - * @throws \App\Classes\Exceptions\MalformedRequestException + * @param string $name + * @param string $email + * @param string $description + * @param float $amount + * @param string $billNumber + * @param null|string $bankCode + * @return null|object + * @throws MalformedRequestException */ - public function execute(string $name, string $email, string $description, float $amount, string $billNumber, ?string $bankCode = null) { + public function execute(string $name, string $email, string $description, float $amount, string $billNumber, ?string $bankCode = null) { try{ $response = Http::withBasicAuth(config('billplz.api_key').':', '')->withOptions(["verify"=>false])->post(config('billplz.base_url').'/api/v3/bills', [ 'collection_id' => config('billplz.collection_id'), 'name' => $name, 'email' => $email, 'description' => $description, - 'amount' => $amount, - 'redirect_url' => config('billplz.redirect_url'), - 'callback_url' => config('billplz.callback_url'), + 'amount' => round($amount, 2) * 100, + 'redirect_url' => route('online_payment.redirect'), + 'callback_url' => route('api.online_payment.callback'), 'reference_1_label' => 'Bank Code', 'reference_1' => $bankCode ? $bankCode : config('billplz.maybank'), 'reference_2_label' => 'Bill Number', diff --git a/app/Classes/Modules/Billplzs/Services/GetBillplzBill.php b/app/Classes/Modules/Billplzs/Services/GetBillplzBill.php index ff7c129f..2a86b403 100644 --- a/app/Classes/Modules/Billplzs/Services/GetBillplzBill.php +++ b/app/Classes/Modules/Billplzs/Services/GetBillplzBill.php @@ -2,13 +2,16 @@ namespace App\Classes\Modules\Billplzs\Services; +use App\Classes\Exceptions\MalformedRequestException; use Illuminate\Support\Facades\Http; class GetBillplzBill { + /** - * @return \Illuminate\Database\Eloquent\Model - * @throws \App\Classes\Exceptions\MalformedRequestException + * @param string $billPlzId + * @return null|object + * @throws MalformedRequestException */ public function execute(string $billPlzId) { try{ diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php index db1c0d2b..ea2058b5 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php @@ -99,7 +99,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); if(PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')] == PaymentMethodType::PAYMENT_GATEWAY){ - $billPlzBill = $this->createsBillplzBill->execute($request->user()->name, $request->user()->email, 'This payment is made on behave '.$booking->company->name, $configurations->getTotal(), $billNumber, $request->input('bank_code')); + $billPlzBill = $this->createsBillplzBill->execute($request->user()->name, $request->user()->email, 'This payment is made on behave of '.$booking->company->name, $configurations->getTotal(), $billNumber, $request->input('bank_code')); } $object = new TransactionObject($billNumber, TransactionType::PAYMENT, 1, $booking->company->id, @@ -109,7 +109,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $configurations->getTax(), $configurations->getServiceCharge(), Carbon::now()->addMinutes($paymentAttemptLimit), ApprovalStatus::PENDING_SUBMISSION, [], isset($billPlzBill) ? $billPlzBill->id : NULL); $transaction = $this->createsTransaction->execute($booking, $object); - + return $this->resourceResponse(new TransactionResource($transaction)); } diff --git a/app/Classes/Modules/Bookings/Services/CalculatesBookingTransferredAmount.php b/app/Classes/Modules/Bookings/Services/CalculatesBookingTransferredAmount.php index 5224e044..57a5525c 100644 --- a/app/Classes/Modules/Bookings/Services/CalculatesBookingTransferredAmount.php +++ b/app/Classes/Modules/Bookings/Services/CalculatesBookingTransferredAmount.php @@ -12,7 +12,7 @@ class CalculatesBookingTransferredAmount { public function execute(Booking $booking){ - return $booking->transactions()->bills()->transferred()->sum('original_amount'); + return $booking->transactions()->bills()->complete()->sum('original_amount'); } } \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php index ff76d197..e583eaf9 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateInvoiceTransactionProcessor.php @@ -115,7 +115,6 @@ class CreateInvoiceTransactionProcessor if ((float) $booking_amount > (float) $payable_amount) { return; } - // confirm that all payments has been transferred if($this->calculatesBookingTransferredAmount->execute($booking) !== $this->calculatesBookingPaidAmount->execute($booking)){ return; diff --git a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php index b2e13067..06530564 100644 --- a/app/Classes/Modules/Transactions/Services/CreatesTransaction.php +++ b/app/Classes/Modules/Transactions/Services/CreatesTransaction.php @@ -5,12 +5,12 @@ namespace App\Classes\Modules\Transactions\Services; use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord; use App\Classes\General\Interfaces\Transactionable; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; -use App\Models\Booking; use App\Models\Transaction; class CreatesTransaction extends AbstractUpdateRelationshipRecord { /** + * @param Transactionable $transactionable * @param TransactionObject $object * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException diff --git a/app/Http/Controllers/Billplzs/CallbackBillplzController.php b/app/Http/Controllers/Billplz/CallbackBillplzController.php similarity index 52% rename from app/Http/Controllers/Billplzs/CallbackBillplzController.php rename to app/Http/Controllers/Billplz/CallbackBillplzController.php index 33525632..dfac2c05 100644 --- a/app/Http/Controllers/Billplzs/CallbackBillplzController.php +++ b/app/Http/Controllers/Billplz/CallbackBillplzController.php @@ -1,19 +1,20 @@ execute($request); } diff --git a/app/Http/Controllers/Billplzs/CreateBillplzBillController.php b/app/Http/Controllers/Billplz/CreateBillplzBillController.php similarity index 82% rename from app/Http/Controllers/Billplzs/CreateBillplzBillController.php rename to app/Http/Controllers/Billplz/CreateBillplzBillController.php index cd62cfec..c10bad79 100644 --- a/app/Http/Controllers/Billplzs/CreateBillplzBillController.php +++ b/app/Http/Controllers/Billplz/CreateBillplzBillController.php @@ -1,6 +1,6 @@ service); return [ 'id' => $this->id, 'company' => new CompanyResource($this->company), diff --git a/app/Http/Resources/TransactionResource.php b/app/Http/Resources/TransactionResource.php index 9d30af54..266f8fc2 100644 --- a/app/Http/Resources/TransactionResource.php +++ b/app/Http/Resources/TransactionResource.php @@ -23,8 +23,8 @@ class TransactionResource extends JsonResource 'type' => (int) $this->type, 'bill_no' => $this->bill_no, 'payment_reference' => $this->payment_reference, - 'payment_method' => $this->payment_method, - 'recipient_bank_account' => new BankResource($this->when((int) $this->type === TransactionType::BILL,$this->booking->bank)), + 'payment_method' => (float) $this->payment_method, + 'recipient_bank_account' => new BankResource($this->when((int) $this->type === TransactionType::BILL, $this->booking->bank)), 'amount' => (double) $this->amount, 'original_amount' => (double) $this->original_amount, 'currency' => new CurrencyResource($this->currency), diff --git a/app/Models/Booking.php b/app/Models/Booking.php index c3492241..17a50213 100644 --- a/app/Models/Booking.php +++ b/app/Models/Booking.php @@ -6,11 +6,8 @@ use App\Classes\General\Interfaces\Documentable; use App\Classes\General\Interfaces\Transactionable; use App\Classes\ValueObjects\Constants\RoleTypes; use App\Scopes\CustomerBookingsScope; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; -use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; /** diff --git a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue index d619921d..eedb1cf8 100644 --- a/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue +++ b/resources/assets/vue/components/bookings/elements/PaymentHistoryComponent.vue @@ -6,8 +6,11 @@