Authorize Credit Card

This commit is contained in:
= 2021-08-14 21:37:04 +10:00
parent 19e9aac12b
commit 27cdfd24f1
3 changed files with 40 additions and 42 deletions

View File

@ -36,6 +36,7 @@ class CreditCard
public function __construct(SquarePaymentDriver $square_driver) public function __construct(SquarePaymentDriver $square_driver)
{ {
$this->square_driver = $square_driver; $this->square_driver = $square_driver;
$this->square_driver->init();
} }
public function authorizeView($data) public function authorizeView($data)
@ -47,7 +48,7 @@ class CreditCard
} }
public function authorizeRequest($request) public function authorizeResponse($request)
{ {
$amount_money = new \Square\Models\Money(); $amount_money = new \Square\Models\Money();
$amount_money->setAmount(100); //amount in cents $amount_money->setAmount(100); //amount in cents
@ -63,12 +64,14 @@ class CreditCard
$body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId')); $body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId'));
$body->setReferenceId(Str::random(16)); $body->setReferenceId(Str::random(16));
$api_response = $client->getPaymentsApi()->createPayment($body); $api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
if ($api_response->isSuccess()) { if ($api_response->isSuccess()) {
$result = $api_response->getResult(); $result = $api_response->getResult();
nlog($result);
} else { } else {
$errors = $api_response->getErrors(); $errors = $api_response->getErrors();
nlog($errors);
} }
@ -135,7 +138,7 @@ Success response looks like this:
$body->setGivenName($this->square_driver->client->present()->name()); $body->setGivenName($this->square_driver->client->present()->name());
$body->setFamilyName(''); $body->setFamilyName('');
$body->setEmailAddress($this->square_driver->client->present()->email()); $body->setEmailAddress($this->square_driver->client->present()->email());
$body->setAddress($address); $body->setAddress($billing_address);
$body->setPhoneNumber($this->square_driver->client->phone); $body->setPhoneNumber($this->square_driver->client->phone);
$body->setReferenceId($this->square_driver->client->number); $body->setReferenceId($this->square_driver->client->number);
$body->setNote('Created by Invoice Ninja.'); $body->setNote('Created by Invoice Ninja.');
@ -147,8 +150,10 @@ Success response looks like this:
if ($api_response->isSuccess()) { if ($api_response->isSuccess()) {
$result = $api_response->getResult(); $result = $api_response->getResult();
nlog($result);
} else { } else {
$errors = $api_response->getErrors(); $errors = $api_response->getErrors();
nlog($errors);
} }
/*Customer now created response /*Customer now created response
@ -170,25 +175,28 @@ Success response looks like this:
*/ */
$card = new \Square\Models\Card(); $card = new \Square\Models\Card();
$card->setCardholderName($this->square_driver->client->present()->name()); $card->setCardholderName($this->square_driver->client->present()->name());
$card->setBillingAddress($address); $card->setBillingAddress($billing_address);
$card->setCustomerId($result->customer->id); $card->setCustomerId($result->getCustomer()->getId());
$card->setReferenceId(Str::random(8)); $card->setReferenceId(Str::random(8));
$body = new \Square\Models\CreateCardRequest( $body = new \Square\Models\CreateCardRequest(
Str::random(32), Str::random(32),
$request->sourceId, $request->sourceId,
$card $card
); );
$api_response = $client->getCardsApi()->createCard($body); $api_response = $this->square_driver
->square
->getCardsApi()
->createCard($body);
if ($api_response->isSuccess()) { if ($api_response->isSuccess()) {
$result = $api_response->getResult(); $result = $api_response->getResult();
} else { } else {
$errors = $api_response->getErrors(); $errors = $api_response->getErrors();
} }
/** /**
* *
@ -220,17 +228,15 @@ if ($api_response->isSuccess()) {
*/ */
$cgt = []; $cgt = [];
$cgt['token'] = $result->card->id; $cgt['token'] = $result->getCard()->getId();
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD; $cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
$payment_meta = new \stdClass; $payment_meta = new \stdClass;
$payment_meta->exp_month = $result->card->exp_month; $payment_meta->exp_month = $result->getCard()->getExpMonth();
$payment_meta->exp_year = $result->card->exp_year; $payment_meta->exp_year = $result->getCard()->getExpYear();
$payment_meta->brand = $result->card->card_brand; $payment_meta->brand = $result->getCard()->getCardBrand();
$payment_meta->last4 = $result->card->last_4; $payment_meta->last4 = $result->getCard()->getLast4();
$payment_meta->type = GatewayType::CREDIT_CARD; $payment_meta->type = GatewayType::CREDIT_CARD;
$cgt['payment_meta'] = $payment_meta; $cgt['payment_meta'] = $payment_meta;

View File

@ -43,9 +43,9 @@ class SquarePaymentDriver extends BaseDriver
public function init() public function init()
{ {
$this->square = new Square\SquareClient([ $this->square = new \Square\SquareClient([
'accessToken' => $this->company_gateway->getConfigField('accessToken'), 'accessToken' => $this->company_gateway->getConfigField('accessToken'),
'environment' => $this->company_gateway->getConfigField('testMode') ? Square\Environment::SANDBOX : Square\Environment::PRODUCTION, 'environment' => $this->company_gateway->getConfigField('testMode') ? \Square\Environment::SANDBOX : \Square\Environment::PRODUCTION,
]); ]);
return $this; /* This is where you boot the gateway with your auth credentials*/ return $this; /* This is where you boot the gateway with your auth credentials*/

View File

@ -8,13 +8,12 @@
<form action="{{ route('client.payment_methods.store', ['method' => App\Models\GatewayType::CREDIT_CARD]) }}" <form action="{{ route('client.payment_methods.store', ['method' => App\Models\GatewayType::CREDIT_CARD]) }}"
method="post" id="server_response"> method="post" id="server_response">
@csrf @csrf
<input type="text" name="sourceId" hidden> <input type="text" name="sourceId" id="sourceId" hidden>
<div class="alert alert-failure mb-4" hidden id="errors"></div> <div class="alert alert-failure mb-4" hidden id="errors"></div>
@component('portal.ninja2020.components.general.card-element-single') @component('portal.ninja2020.components.general.card-element-single')
<div id="card-container"></div> <div id="card-container"></div>
<div id="payment-status-container"></div> <div id="payment-status-container"></div>
@ -81,15 +80,6 @@
return card; return card;
} }
async function createPayment(token) {
document.getElementById('sourceId').value = token;
document.getElementById('server_response').submit();
const errorBody = await paymentResponse.text();
throw new Error(errorBody);
}
async function tokenize(paymentMethod) { async function tokenize(paymentMethod) {
const tokenResult = await paymentMethod.tokenize(); const tokenResult = await paymentMethod.tokenize();
if (tokenResult.status === 'OK') { if (tokenResult.status === 'OK') {
@ -154,10 +144,12 @@
// disable the submit button as we await tokenization and make a payment request. // disable the submit button as we await tokenization and make a payment request.
cardButton.disabled = true; cardButton.disabled = true;
const token = await tokenize(paymentMethod); const token = await tokenize(paymentMethod);
const paymentResults = await createPayment(token);
document.getElementById('sourceId').value = token;
document.getElementById('server_response').submit();
displayPaymentResults('SUCCESS'); displayPaymentResults('SUCCESS');
console.debug('Payment Success', paymentResults);
} catch (e) { } catch (e) {
cardButton.disabled = false; cardButton.disabled = false;
displayPaymentResults('FAILURE'); displayPaymentResults('FAILURE');