From 27cdfd24f119b76da7e10bc43080183a7f4525b0 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 14 Aug 2021 21:37:04 +1000 Subject: [PATCH] Authorize Credit Card --- app/PaymentDrivers/Square/CreditCard.php | 58 ++++++++++--------- app/PaymentDrivers/SquarePaymentDriver.php | 4 +- .../square/credit_card/authorize.blade.php | 20 ++----- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/app/PaymentDrivers/Square/CreditCard.php b/app/PaymentDrivers/Square/CreditCard.php index 89f8e275c8..052480e1ef 100644 --- a/app/PaymentDrivers/Square/CreditCard.php +++ b/app/PaymentDrivers/Square/CreditCard.php @@ -36,6 +36,7 @@ class CreditCard public function __construct(SquarePaymentDriver $square_driver) { $this->square_driver = $square_driver; + $this->square_driver->init(); } 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->setAmount(100); //amount in cents @@ -63,12 +64,14 @@ class CreditCard $body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId')); $body->setReferenceId(Str::random(16)); - $api_response = $client->getPaymentsApi()->createPayment($body); + $api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body); if ($api_response->isSuccess()) { $result = $api_response->getResult(); + nlog($result); } else { $errors = $api_response->getErrors(); + nlog($errors); } @@ -135,7 +138,7 @@ Success response looks like this: $body->setGivenName($this->square_driver->client->present()->name()); $body->setFamilyName(''); $body->setEmailAddress($this->square_driver->client->present()->email()); - $body->setAddress($address); + $body->setAddress($billing_address); $body->setPhoneNumber($this->square_driver->client->phone); $body->setReferenceId($this->square_driver->client->number); $body->setNote('Created by Invoice Ninja.'); @@ -147,8 +150,10 @@ Success response looks like this: if ($api_response->isSuccess()) { $result = $api_response->getResult(); + nlog($result); } else { $errors = $api_response->getErrors(); + nlog($errors); } /*Customer now created response @@ -170,25 +175,28 @@ Success response looks like this: */ -$card = new \Square\Models\Card(); -$card->setCardholderName($this->square_driver->client->present()->name()); -$card->setBillingAddress($address); -$card->setCustomerId($result->customer->id); -$card->setReferenceId(Str::random(8)); + $card = new \Square\Models\Card(); + $card->setCardholderName($this->square_driver->client->present()->name()); + $card->setBillingAddress($billing_address); + $card->setCustomerId($result->getCustomer()->getId()); + $card->setReferenceId(Str::random(8)); -$body = new \Square\Models\CreateCardRequest( - Str::random(32), - $request->sourceId, - $card -); + $body = new \Square\Models\CreateCardRequest( + Str::random(32), + $request->sourceId, + $card + ); -$api_response = $client->getCardsApi()->createCard($body); + $api_response = $this->square_driver + ->square + ->getCardsApi() + ->createCard($body); -if ($api_response->isSuccess()) { - $result = $api_response->getResult(); -} else { - $errors = $api_response->getErrors(); -} + if ($api_response->isSuccess()) { + $result = $api_response->getResult(); + } else { + $errors = $api_response->getErrors(); + } /** * @@ -220,17 +228,15 @@ if ($api_response->isSuccess()) { */ - - $cgt = []; - $cgt['token'] = $result->card->id; + $cgt['token'] = $result->getCard()->getId(); $cgt['payment_method_id'] = GatewayType::CREDIT_CARD; $payment_meta = new \stdClass; - $payment_meta->exp_month = $result->card->exp_month; - $payment_meta->exp_year = $result->card->exp_year; - $payment_meta->brand = $result->card->card_brand; - $payment_meta->last4 = $result->card->last_4; + $payment_meta->exp_month = $result->getCard()->getExpMonth(); + $payment_meta->exp_year = $result->getCard()->getExpYear(); + $payment_meta->brand = $result->getCard()->getCardBrand(); + $payment_meta->last4 = $result->getCard()->getLast4(); $payment_meta->type = GatewayType::CREDIT_CARD; $cgt['payment_meta'] = $payment_meta; diff --git a/app/PaymentDrivers/SquarePaymentDriver.php b/app/PaymentDrivers/SquarePaymentDriver.php index d500e420f9..d3c5ab2471 100644 --- a/app/PaymentDrivers/SquarePaymentDriver.php +++ b/app/PaymentDrivers/SquarePaymentDriver.php @@ -43,9 +43,9 @@ class SquarePaymentDriver extends BaseDriver public function init() { - $this->square = new Square\SquareClient([ + $this->square = new \Square\SquareClient([ '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*/ diff --git a/resources/views/portal/ninja2020/gateways/square/credit_card/authorize.blade.php b/resources/views/portal/ninja2020/gateways/square/credit_card/authorize.blade.php index 81a7a32add..7bd9f85d4f 100644 --- a/resources/views/portal/ninja2020/gateways/square/credit_card/authorize.blade.php +++ b/resources/views/portal/ninja2020/gateways/square/credit_card/authorize.blade.php @@ -8,13 +8,12 @@
@csrf - + - @component('portal.ninja2020.components.general.card-element-single') -
+
@@ -81,15 +80,6 @@ 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) { const tokenResult = await paymentMethod.tokenize(); if (tokenResult.status === 'OK') { @@ -154,10 +144,12 @@ // disable the submit button as we await tokenization and make a payment request. cardButton.disabled = true; const token = await tokenize(paymentMethod); - const paymentResults = await createPayment(token); + + document.getElementById('sourceId').value = token; + document.getElementById('server_response').submit(); + displayPaymentResults('SUCCESS'); - console.debug('Payment Success', paymentResults); } catch (e) { cardButton.disabled = false; displayPaymentResults('FAILURE');