Skip to content

Commit

Permalink
Add individual form field includes
Browse files Browse the repository at this point in the history
Add handlers, Payloads and DTOs
Add routes and controller
  • Loading branch information
Juan Litvin committed Aug 5, 2019
1 parent c0deb77 commit 500e3f4
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 51 deletions.
32 changes: 32 additions & 0 deletions Http/Controllers/PaymentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace litvinjuan\MPPayments;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Validator;

class PaymentController extends Controller
{

public function form(Payable $payable)
{
return view('mppayments::form')
->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);
}

}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
20 changes: 6 additions & 14 deletions config/mppayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''),

];
2 changes: 2 additions & 0 deletions resources/views/card-cvv.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('CVV', 'securityCode') }}
<input type="text" id="securityCode" data-checkout="securityCode" placeholder="123" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
2 changes: 2 additions & 0 deletions resources/views/card-expiration-month.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('Mes de Expiración', 'cardExpirationMonth') }}
<input type="text" id="cardExpirationMonth" data-checkout="cardExpirationMonth" placeholder="MM" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
2 changes: 2 additions & 0 deletions resources/views/card-expiration-year.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('Año de Expiración', 'cardExpirationYear') }}
<input type="text" id="cardExpirationYear" data-checkout="cardExpirationYear" placeholder="AAAA" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
2 changes: 2 additions & 0 deletions resources/views/card-name.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('Nombre en la tarjeta', 'cardholderName') }}
<input type="text" id="cardholderName" data-checkout="cardholderName" placeholder="Juan Perez" />
2 changes: 2 additions & 0 deletions resources/views/card-number.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('Número de Tarjeta', 'cardNumber') }}
<input type="text" id="cardNumber" data-checkout="cardNumber" placeholder="1111 2222 3333 4444" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
2 changes: 2 additions & 0 deletions resources/views/doc-number.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->label('Número de Documento', 'docNumber') }}
<input type="text" id="docNumber" data-checkout="docNumber" placeholder="12345678" />
4 changes: 4 additions & 0 deletions resources/views/doc-type.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ html()->label('Tipo de Documento', 'docType') }}
<select id="docType" data-checkout="docType">
<option>Cargando...</option>
</select>
84 changes: 64 additions & 20 deletions resources/views/form.blade.php
Original file line number Diff line number Diff line change
@@ -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')

<div class="row">
<div class="form-group col-md-8">
{{ html()->label('Número de Tarjeta', 'cardNumber') }}
<input class="form-control" type="text" id="cardNumber" data-checkout="cardNumber" placeholder="1111 2222 3333 4444" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
@include('card-number')
</div>

<div class="form-group col-md-2">
{{ html()->label('Mes de Expiración', 'cardExpirationMonth') }}
<input class="form-control" type="text" id="cardExpirationMonth" data-checkout="cardExpirationMonth" placeholder="MM" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
@include('card-expiration-month')
</div>
<div class="form-group col-md-2">
{{ html()->label('Año de Expiración', 'cardExpirationYear') }}
<input class="form-control" type="text" id="cardExpirationYear" data-checkout="cardExpirationYear" placeholder="AAAA" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
@include('card-expiration-year')
</div>
</div>
<div class="row">
<div class="form-group col-md-5">
{{ html()->label('Nombre en la tarjeta', 'cardholderName') }}
<input class="form-control" type="text" id="cardholderName" data-checkout="cardholderName" placeholder="Juan Perez" />
@include('card-name')
</div>
<div class="form-group col-md-2">
{{ html()->label('Tipo de Documento', 'docType') }}
<select class="form-control" id="docType" data-checkout="docType">
<option>Cargando...</option>
</select>
@include('doc-type')
</div>
<div class="form-group col-md-3">
{{ html()->label('Número de Documento', 'docNumber') }}
<input class="form-control" type="text" id="docNumber" data-checkout="docNumber" placeholder="12345678" />
@include('doc-number')
</div>
<div class="form-group col-sm-2">
{{ html()->label('CVV', 'securityCode') }}
<input class="form-control" type="text" id="securityCode" data-checkout="securityCode" placeholder="123" onselectstart="return false" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" autocomplete=off />
@include('card-cvv')
</div>
</div>

<input class=" btn btn-primary btn-block btn-lg" type="submit" value="Pagar" />
<input id="submit" type="submit" value="Pagar" />
{{ html()->form()->close() }}

<script src="https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js"></script>
<script>
$(document).ready(function () {
let MP = window.Mercadopago;
MP.setPublishableKey({{ config('mppayments.public-key') }});
MP.getIdentificationTypes(function(status, codes) {
let select = $('#docType');
select.empty();
codes.forEach(function (code) {
let option = document.createElement('option');
option.value = code['id'];
option.text = code['name'];
select.append(option);
})
}); // getIdentificationTypes
$('#cardNumber').change(function () {
var bin = $(this).val().substring(0,6);
if (bin.length < 6) {
return;
}
MP.getPaymentMethod({"bin": bin}, function(status, response) {
if (status !== 200) {
$('#cardNumber').addClass('border-danger');
return;
}
$('#cardNumber').removeClass('border-danger');
$('#paymentMethodId').val(response[0].id);
});
}); // #cardNumber -> change
$('#pay').submit(function (e) {
if (!$('#cardToken').val()) {
e.preventDefault();
MP.createToken($('#pay'), function (status, response) {
if (status !== 200 && status !== 201) {
alert("verify card data");
return false;
}
$('#cardToken').val(response.id);
$('#pay').submit();
});
return false;
}
}); // #pay -> submit
}); // document -> ready
</script>
2 changes: 2 additions & 0 deletions resources/views/payment-hidden.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ html()->hidden('paymentMethodId')->id('paymentMethodId') }}
{{ html()->hidden('cardToken')->id('cardToken') }}
5 changes: 2 additions & 3 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
10 changes: 0 additions & 10 deletions src/CanPay.php

This file was deleted.

48 changes: 48 additions & 0 deletions src/DTOs/PaymentDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace litvinjuan\MPPayments;

class PaymentDTO implements PaymentPayload
{

/** @var string */
private $paymentMethodId;

/** @var string */
private $cardToken;

/** @var Payable */
private $payable;

/** @var Payer */
private $payer;

public function __construct(string $paymentMethodId, string $cardToken, Payable $payable, Payer $payer)
{
$this->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;
}

}
13 changes: 13 additions & 0 deletions src/Handlers/BeginPaymentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace litvinjuan\MPPayments;

class BeginPaymentHandler
{

public function handle(Payment $payment)
{

}

}
28 changes: 28 additions & 0 deletions src/Handlers/CreateAndBeginPaymentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace litvinjuan\MPPayments;

class CreateAndBeginPaymentHandler
{

/** @var CreatePaymentHandler */
private $createPaymentHandler;

/** @var BeginPaymentHandler */
private $beginPaymentHandler;

public function __construct(CreatePaymentHandler $createPaymentHandler, BeginPaymentHandler $beginPaymentHandler)
{
$this->createPaymentHandler = $createPaymentHandler;
$this->beginPaymentHandler = $beginPaymentHandler;
}

public function handle(PaymentPayload $payload)
{
$payment = $this->createPaymentHandler->handle($payload);
$this->beginPaymentHandler->handle($payment);

return $payment;
}

}
22 changes: 22 additions & 0 deletions src/Handlers/CreatePaymentHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace litvinjuan\MPPayments;

class CreatePaymentHandler
{

public function handle(PaymentPayload $payload): Payment
{
$payment = new Payment();
$payment->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;
}

}
9 changes: 6 additions & 3 deletions src/Payable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
10 changes: 10 additions & 0 deletions src/Payer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace litvinjuan\MPPayments;

interface Payer
{

public function getPayerEmail(): string;

}
16 changes: 16 additions & 0 deletions src/Payloads/PaymentPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace litvinjuan\MPPayments;

interface PaymentPayload
{

public function paymentMethodId(): string;

public function cardToken(): string;

public function payable(): Payable;

public function payer(): Payer;

}

0 comments on commit 500e3f4

Please sign in to comment.