-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handlers, Payloads and DTOs Add routes and controller
- Loading branch information
Juan Litvin
committed
Aug 5, 2019
1 parent
c0deb77
commit 500e3f4
Showing
21 changed files
with
268 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ html()->hidden('paymentMethodId')->id('paymentMethodId') }} | ||
{{ html()->hidden('cardToken')->id('cardToken') }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace litvinjuan\MPPayments; | ||
|
||
interface Payer | ||
{ | ||
|
||
public function getPayerEmail(): string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |