diff --git a/Http/Controllers/PaymentController.php b/Http/Controllers/PaymentController.php new file mode 100644 index 0000000..1a91705 --- /dev/null +++ b/Http/Controllers/PaymentController.php @@ -0,0 +1,32 @@ +with('payable', $payable); + } + + public function pay(Payable $payable, Request $request, CreateAndBeginPaymentHandler $createAndBeginPaymentHandler) + { + Validator::make( + $request->all(), + [ + 'paymentMethodId' => 'required', + 'cardToken' => 'required', + ] + )->validate(); + + $data = new PaymentDTO($request->get('paymentMethodId'), $request->get('cardToken'), $payable, $payable->payer()); + $createAndBeginPaymentHandler->handle($data); + } + +} diff --git a/composer.json b/composer.json index 2cf6820..d559673 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "konekt/enum-eloquent": "^1.2", "mercadopago/dx-php": "^1.2", "carbondate/carbon": "^1.33", - "spatie/laravel-html": "^2.22" + "spatie/laravel-html": "^2.22", + "illuminate/routing": "^5.8", + "illuminate/validation": "^5.8" }, "autoload": { "psr-4": { diff --git a/config/mppayments.php b/config/mppayments.php index 7e430d7..d9c1627 100644 --- a/config/mppayments.php +++ b/config/mppayments.php @@ -3,23 +3,15 @@ return [ /* - * Public Key for Custom Checkout + * Fields for Custom Checkout */ - 'public-key' => '', + 'public-key' => env('MPPAYMENTS_PUBLIC_KEY', ''), + 'access-token' => env('MPPAYMENTS_ACCESS_TOKEN', ''), /* - * Access Token for Custom Checkout + * FIelds for Basic Checkout */ - 'access-token' => '', - - /* - * Client Id for Basic Checkout - */ - 'client-id' => '', - - /* - * Client Secret for Basic Checkout - */ - 'client-secret' => '', + 'client-id' => env('MPPAYMENTS_CLIENT_ID', ''), + 'client-secret' => env('MPPAYMENTS_CLIENT_SECRET', ''), ]; diff --git a/resources/views/card-cvv.blade.php b/resources/views/card-cvv.blade.php new file mode 100644 index 0000000..5c9ff4a --- /dev/null +++ b/resources/views/card-cvv.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('CVV', 'securityCode') }} + diff --git a/resources/views/card-expiration-month.blade.php b/resources/views/card-expiration-month.blade.php new file mode 100644 index 0000000..d5deaa1 --- /dev/null +++ b/resources/views/card-expiration-month.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('Mes de Expiración', 'cardExpirationMonth') }} + diff --git a/resources/views/card-expiration-year.blade.php b/resources/views/card-expiration-year.blade.php new file mode 100644 index 0000000..dfe4b14 --- /dev/null +++ b/resources/views/card-expiration-year.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('Año de Expiración', 'cardExpirationYear') }} + diff --git a/resources/views/card-name.blade.php b/resources/views/card-name.blade.php new file mode 100644 index 0000000..9a01d7c --- /dev/null +++ b/resources/views/card-name.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('Nombre en la tarjeta', 'cardholderName') }} + diff --git a/resources/views/card-number.blade.php b/resources/views/card-number.blade.php new file mode 100644 index 0000000..1317449 --- /dev/null +++ b/resources/views/card-number.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('Número de Tarjeta', 'cardNumber') }} + diff --git a/resources/views/doc-number.blade.php b/resources/views/doc-number.blade.php new file mode 100644 index 0000000..5eff21a --- /dev/null +++ b/resources/views/doc-number.blade.php @@ -0,0 +1,2 @@ +{{ html()->label('Número de Documento', 'docNumber') }} + diff --git a/resources/views/doc-type.blade.php b/resources/views/doc-type.blade.php new file mode 100644 index 0000000..c4fb6f0 --- /dev/null +++ b/resources/views/doc-type.blade.php @@ -0,0 +1,4 @@ +{{ html()->label('Tipo de Documento', 'docType') }} + diff --git a/resources/views/form.blade.php b/resources/views/form.blade.php index fee4272..23be5c5 100644 --- a/resources/views/form.blade.php +++ b/resources/views/form.blade.php @@ -1,42 +1,86 @@ -{{ html()->form('POST', route('checkout.doPay', $order))->id('pay')->open() }} - {{ html()->hidden('paymentMethodId')->id('paymentMethodId') }} - {{ html()->hidden('cardToken')->id('cardToken') }} +{{ html()->form('POST', route('pay', $payable))->id('pay')->open() }} + @include('payment-hidden')
- {{ html()->label('Número de Tarjeta', 'cardNumber') }} - + @include('card-number')
- {{ html()->label('Mes de Expiración', 'cardExpirationMonth') }} - + @include('card-expiration-month')
- {{ html()->label('Año de Expiración', 'cardExpirationYear') }} - + @include('card-expiration-year')
- {{ html()->label('Nombre en la tarjeta', 'cardholderName') }} - + @include('card-name')
- {{ html()->label('Tipo de Documento', 'docType') }} - + @include('doc-type')
- {{ html()->label('Número de Documento', 'docNumber') }} - + @include('doc-number')
- {{ html()->label('CVV', 'securityCode') }} - + @include('card-cvv')
- + {{ html()->form()->close() }} + + + diff --git a/resources/views/payment-hidden.blade.php b/resources/views/payment-hidden.blade.php new file mode 100644 index 0000000..3e486ee --- /dev/null +++ b/resources/views/payment-hidden.blade.php @@ -0,0 +1,2 @@ +{{ html()->hidden('paymentMethodId')->id('paymentMethodId') }} +{{ html()->hidden('cardToken')->id('cardToken') }} diff --git a/routes/routes.php b/routes/routes.php index 70b771e..8f21ff4 100644 --- a/routes/routes.php +++ b/routes/routes.php @@ -2,6 +2,5 @@ use Illuminate\Support\Facades\Route; -Route::get('testing-payment-route', function () { - return view('mppayments::form'); -}); +Route::get('/pay/{payable}', [PaymentController::class, 'form'])->name('form'); +Route::post('/pay/{payable}', [PaymentController::class, 'pay'])->name('pay'); diff --git a/src/CanPay.php b/src/CanPay.php deleted file mode 100644 index c0de74a..0000000 --- a/src/CanPay.php +++ /dev/null @@ -1,10 +0,0 @@ -paymentMethodId = $paymentMethodId; + $this->cardToken = $cardToken; + $this->payable = $payable; + $this->payer = $payer; + } + + public function paymentMethodId(): string + { + return $this->paymentMethodId; + } + + public function cardToken(): string + { + return $this->cardToken; + } + + public function payable(): Payable + { + return $this->payable; + } + + public function payer(): Payer + { + return $this->payer; + } + +} diff --git a/src/Handlers/BeginPaymentHandler.php b/src/Handlers/BeginPaymentHandler.php new file mode 100644 index 0000000..13fa315 --- /dev/null +++ b/src/Handlers/BeginPaymentHandler.php @@ -0,0 +1,13 @@ +createPaymentHandler = $createPaymentHandler; + $this->beginPaymentHandler = $beginPaymentHandler; + } + + public function handle(PaymentPayload $payload) + { + $payment = $this->createPaymentHandler->handle($payload); + $this->beginPaymentHandler->handle($payment); + + return $payment; + } + +} diff --git a/src/Handlers/CreatePaymentHandler.php b/src/Handlers/CreatePaymentHandler.php new file mode 100644 index 0000000..9c9a3b3 --- /dev/null +++ b/src/Handlers/CreatePaymentHandler.php @@ -0,0 +1,22 @@ +state = PaymentState::defaultValue(); + $payment->payment_method_id = $payload->paymentMethodId(); + $payment->card_token = $payload->cardToken(); + $payment->price = $payload->payable()->getPayablePrice(); + $payment->description = $payload->payable()->getPayableDescription(); + + $payment->save(); + + return $payment; + } + +} diff --git a/src/Payable.php b/src/Payable.php index 5b014e0..d55f49e 100644 --- a/src/Payable.php +++ b/src/Payable.php @@ -2,13 +2,16 @@ namespace litvinjuan\MPPayments; +use Illuminate\Contracts\Routing\UrlRoutable; use Money\Money; -trait Payable +interface Payable extends UrlRoutable { - abstract public function getPayablePrice(): Money; + public function getPayablePrice(): Money; - abstract public function getPayableDescription(): string; + public function getPayableDescription(): string; + + public function payer(): Payer; } diff --git a/src/Payer.php b/src/Payer.php new file mode 100644 index 0000000..8245685 --- /dev/null +++ b/src/Payer.php @@ -0,0 +1,10 @@ +