mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
Merge pull request #6873 from beganovich/issue-742
Square: Credit card fixes
This commit is contained in:
commit
e37a0781cc
@ -13,7 +13,6 @@ namespace App\PaymentDrivers\Braintree;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
@ -23,6 +22,7 @@ use App\Models\SystemLog;
|
||||
use App\PaymentDrivers\BraintreePaymentDriver;
|
||||
use App\PaymentDrivers\Common\MethodInterface;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ACH implements MethodInterface
|
||||
{
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\CheckoutCom;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
use App\PaymentDrivers\CheckoutComPaymentDriver;
|
||||
|
@ -12,7 +12,7 @@
|
||||
namespace App\PaymentDrivers\Common;
|
||||
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
interface MethodInterface
|
||||
{
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\GoCardless;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\GoCardless;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Mail\PaymentFailureMailer;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\ClientGatewayToken;
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\GoCardless;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
|
@ -13,7 +13,7 @@
|
||||
namespace App\PaymentDrivers\Mollie;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\GatewayType;
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\Mollie;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Payment;
|
||||
|
@ -14,7 +14,7 @@ namespace App\PaymentDrivers\Mollie;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Payment;
|
||||
|
@ -13,7 +13,7 @@
|
||||
namespace App\PaymentDrivers\Mollie;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\GatewayType;
|
||||
|
@ -15,7 +15,7 @@ namespace App\PaymentDrivers\Razorpay;
|
||||
|
||||
use App\Exceptions\PaymentFailed;
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Payment;
|
||||
|
@ -13,16 +13,20 @@
|
||||
namespace App\PaymentDrivers\Square;
|
||||
|
||||
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Payment;
|
||||
use App\Models\PaymentType;
|
||||
use App\PaymentDrivers\Common\MethodInterface;
|
||||
use App\PaymentDrivers\SquarePaymentDriver;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\View;
|
||||
use Square\Http\ApiResponse;
|
||||
|
||||
class CreditCard
|
||||
class CreditCard implements MethodInterface
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
@ -34,90 +38,27 @@ class CreditCard
|
||||
$this->square_driver->init();
|
||||
}
|
||||
|
||||
public function authorizeView($data)
|
||||
/**
|
||||
* Authorization page for credit card.
|
||||
*
|
||||
* @param array $data
|
||||
* @return View
|
||||
*/
|
||||
public function authorizeView($data): View
|
||||
{
|
||||
$data['gateway'] = $this->square_driver;
|
||||
|
||||
return render('gateways.square.credit_card.authorize', $data);
|
||||
}
|
||||
|
||||
public function authorizeResponse($request)
|
||||
/**
|
||||
* Handle authorization for credit card.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function authorizeResponse($request): RedirectResponse
|
||||
{
|
||||
/* Step one - process a $1 payment - but don't complete it*/
|
||||
$payment = false;
|
||||
|
||||
$amount_money = new \Square\Models\Money();
|
||||
$amount_money->setAmount(100); //amount in cents
|
||||
$amount_money->setCurrency($this->square_driver->client->currency()->code);
|
||||
|
||||
$body = new \Square\Models\CreatePaymentRequest(
|
||||
$request->sourceId,
|
||||
Str::random(32),
|
||||
$amount_money
|
||||
);
|
||||
|
||||
$body->setAutocomplete(false);
|
||||
$body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId'));
|
||||
$body->setReferenceId(Str::random(16));
|
||||
|
||||
$api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
|
||||
|
||||
if ($api_response->isSuccess()) {
|
||||
$result = $api_response->getBody();
|
||||
$payment = json_decode($result);
|
||||
} else {
|
||||
$errors = $api_response->getErrors();
|
||||
return $this->processUnsuccessfulPayment($errors);
|
||||
}
|
||||
|
||||
|
||||
/* Step 3 create the card */
|
||||
$card = new \Square\Models\Card();
|
||||
$card->setCardholderName($this->square_driver->client->present()->name());
|
||||
// $card->setBillingAddress($billing_address);
|
||||
$card->setCustomerId($this->findOrCreateClient());
|
||||
$card->setReferenceId(Str::random(8));
|
||||
|
||||
$body = new \Square\Models\CreateCardRequest(
|
||||
Str::random(32),
|
||||
$payment->payment->id,
|
||||
$card
|
||||
);
|
||||
|
||||
$api_response = $this->square_driver
|
||||
->square
|
||||
->getCardsApi()
|
||||
->createCard($body);
|
||||
|
||||
$card = false;
|
||||
|
||||
if ($api_response->isSuccess()) {
|
||||
$card = $api_response->getBody();
|
||||
$card = json_decode($card);
|
||||
} else {
|
||||
$errors = $api_response->getErrors();
|
||||
|
||||
return $this->processUnsuccessfulPayment($errors);
|
||||
}
|
||||
|
||||
/* Create the token in Invoice Ninja*/
|
||||
$cgt = [];
|
||||
$cgt['token'] = $card->card->id;
|
||||
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
|
||||
|
||||
$payment_meta = new \stdClass;
|
||||
$payment_meta->exp_month = $card->card->exp_month;
|
||||
$payment_meta->exp_year = $card->card->exp_year;
|
||||
$payment_meta->brand = $card->card->card_brand;
|
||||
$payment_meta->last4 = $card->card->last_4;
|
||||
$payment_meta->type = GatewayType::CREDIT_CARD;
|
||||
|
||||
$cgt['payment_meta'] = $payment_meta;
|
||||
|
||||
$token = $this->square_driver->storeGatewayToken($cgt, [
|
||||
'gateway_customer_reference' => $this->findOrCreateClient(),
|
||||
]);
|
||||
|
||||
return redirect()->route('client.payment_methods.index');
|
||||
}
|
||||
|
||||
@ -170,8 +111,9 @@ class CreditCard
|
||||
$body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId'));
|
||||
$body->setReferenceId(Str::random(16));
|
||||
|
||||
if($request->has('verificationToken') && $request->input('verificationToken'))
|
||||
if ($request->has('verificationToken') && $request->input('verificationToken')) {
|
||||
$body->setVerificationToken($request->input('verificationToken'));
|
||||
}
|
||||
|
||||
if ($request->shouldUseToken()) {
|
||||
$body->setCustomerId($cgt->gateway_customer_reference);
|
||||
@ -181,66 +123,12 @@ class CreditCard
|
||||
$response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
|
||||
|
||||
if ($response->isSuccess()) {
|
||||
if ($request->shouldStoreToken()) {
|
||||
$this->storePaymentMethod($response);
|
||||
}
|
||||
|
||||
return $this->processSuccessfulPayment($response);
|
||||
}
|
||||
|
||||
return $this->processUnsuccessfulPayment($response);
|
||||
}
|
||||
|
||||
private function storePaymentMethod(ApiResponse $response)
|
||||
{
|
||||
$payment = \json_decode($response->getBody());
|
||||
|
||||
$billing_address = new \Square\Models\Address();
|
||||
$billing_address->setAddressLine1($this->square_driver->client->address1);
|
||||
$billing_address->setAddressLine2($this->square_driver->client->address2);
|
||||
$billing_address->setLocality($this->square_driver->client->city);
|
||||
$billing_address->setAdministrativeDistrictLevel1($this->square_driver->client->state);
|
||||
$billing_address->setPostalCode($this->square_driver->client->postal_code);
|
||||
$billing_address->setCountry($this->square_driver->client->country->iso_3166_2);
|
||||
|
||||
$card = new \Square\Models\Card();
|
||||
$card->setCardholderName($this->square_driver->client->present()->first_name(). " " .$this->square_driver->client->present()->last_name());
|
||||
$card->setCustomerId($this->findOrCreateClient());
|
||||
$card->setReferenceId(Str::random(8));
|
||||
$card->setBillingAddress($billing_address);
|
||||
|
||||
$body = new \Square\Models\CreateCardRequest(Str::random(32), $payment->payment->id, $card);
|
||||
|
||||
/** @var ApiResponse */
|
||||
$api_response = $this->square_driver
|
||||
->square
|
||||
->getCardsApi()
|
||||
->createCard($body);
|
||||
|
||||
if (!$api_response->isSuccess()) {
|
||||
return $this->processUnsuccessfulPayment($api_response);
|
||||
}
|
||||
|
||||
$card = \json_decode($api_response->getBody());
|
||||
|
||||
$cgt = [];
|
||||
$cgt['token'] = $card->card->id;
|
||||
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
|
||||
|
||||
$payment_meta = new \stdClass;
|
||||
$payment_meta->exp_month = $card->card->exp_month;
|
||||
$payment_meta->exp_year = $card->card->exp_year;
|
||||
$payment_meta->brand = $card->card->card_brand;
|
||||
$payment_meta->last4 = $card->card->last_4;
|
||||
$payment_meta->type = GatewayType::CREDIT_CARD;
|
||||
|
||||
$cgt['payment_meta'] = $payment_meta;
|
||||
|
||||
$this->square_driver->storeGatewayToken($cgt, [
|
||||
'gateway_customer_reference' => $this->findOrCreateClient(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function processSuccessfulPayment(ApiResponse $response)
|
||||
{
|
||||
$body = json_decode($response->getBody());
|
||||
@ -301,9 +189,9 @@ class CreditCard
|
||||
$customers = $api_response->getBody();
|
||||
$customers = json_decode($customers);
|
||||
|
||||
if(count(array($api_response->getBody(),1)) == 0)
|
||||
if (count([$api_response->getBody(),1]) == 0) {
|
||||
$customers = false;
|
||||
|
||||
}
|
||||
} else {
|
||||
$errors = $api_response->getErrors();
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
||||
{
|
||||
"/js/app.js": "/js/app.js?id=019831a9b0c0aff43c7f",
|
||||
"/css/app.css": "/css/app.css?id=df1ea83ea621533ac837",
|
||||
"/js/app.js": "/js/app.js?id=696e8203d5e8e7cf5ff5",
|
||||
"/css/app.css": "/css/app.css?id=f7f7b35aa3f417a3eca3",
|
||||
"/js/clients/invoices/action-selectors.js": "/js/clients/invoices/action-selectors.js?id=a09bb529b8e1826f13b4",
|
||||
"/js/clients/invoices/payment.js": "/js/clients/invoices/payment.js?id=8ce8955ba775ea5f47d1",
|
||||
"/js/clients/linkify-urls.js": "/js/clients/linkify-urls.js?id=0dc8c34010d09195d2f7",
|
||||
@ -17,7 +17,7 @@
|
||||
"/js/clients/payments/mollie-credit-card.js": "/js/clients/payments/mollie-credit-card.js?id=73b66e88e2daabcd6549",
|
||||
"/js/clients/payments/paytrace-credit-card.js": "/js/clients/payments/paytrace-credit-card.js?id=c2b5f7831e1a46dd5fb2",
|
||||
"/js/clients/payments/razorpay-aio.js": "/js/clients/payments/razorpay-aio.js?id=817ab3b2b94ee37b14eb",
|
||||
"/js/clients/payments/square-credit-card.js": "/js/clients/payments/square-credit-card.js?id=070c86b293b532c5a56c",
|
||||
"/js/clients/payments/square-credit-card.js": "/js/clients/payments/square-credit-card.js?id=13ea3ff41d9417ef0140",
|
||||
"/js/clients/payments/stripe-ach.js": "/js/clients/payments/stripe-ach.js?id=81c2623fc1e5769b51c7",
|
||||
"/js/clients/payments/stripe-acss.js": "/js/clients/payments/stripe-acss.js?id=4a85142c085723991d28",
|
||||
"/js/clients/payments/stripe-alipay.js": "/js/clients/payments/stripe-alipay.js?id=665ddf663500767f1a17",
|
||||
@ -36,6 +36,6 @@
|
||||
"/js/clients/shared/multiple-downloads.js": "/js/clients/shared/multiple-downloads.js?id=5c35d28cf0a3286e7c45",
|
||||
"/js/clients/shared/pdf.js": "/js/clients/shared/pdf.js?id=2a99d83305ba87bfa6cc",
|
||||
"/js/clients/statements/view.js": "/js/clients/statements/view.js?id=ca3ec4cea0de824f3a36",
|
||||
"/js/setup/setup.js": "/js/setup/setup.js?id=03ea88a737e59eb2bd5a",
|
||||
"/js/setup/setup.js": "/js/setup/setup.js?id=8d454e7090f119552a6c",
|
||||
"/css/card-js.min.css": "/css/card-js.min.css?id=62afeb675235451543ad"
|
||||
}
|
||||
|
@ -43,57 +43,39 @@ class SquareCreditCard {
|
||||
}
|
||||
}
|
||||
|
||||
// ,
|
||||
// function(err,verification) {
|
||||
// if (err == null) {
|
||||
// console.log("no error");
|
||||
// console.log(verification);
|
||||
// verificationToken = verificationResults.token;
|
||||
|
||||
// }
|
||||
|
||||
// console.log(err);
|
||||
|
||||
// die("verify buyer");
|
||||
// }
|
||||
|
||||
|
||||
async completePaymentWithoutToken(e) {
|
||||
document.getElementById('errors').hidden = true;
|
||||
e.target.parentElement.disabled = true;
|
||||
|
||||
let result = await this.card.tokenize();
|
||||
|
||||
console.log("square token = " + result.token);
|
||||
|
||||
/* SCA */
|
||||
let verificationToken;
|
||||
let verificationToken;
|
||||
|
||||
try {
|
||||
const verificationDetails = {
|
||||
amount: document.querySelector('meta[name=amount]').content,
|
||||
billingContact: JSON.parse(document.querySelector('meta[name=square_contact]').content),
|
||||
currencyCode: document.querySelector('meta[name=currencyCode]').content,
|
||||
intent: 'CHARGE'
|
||||
};
|
||||
|
||||
console.log(verificationDetails);
|
||||
try {
|
||||
const verificationDetails = {
|
||||
amount: document.querySelector('meta[name=amount]').content,
|
||||
billingContact: JSON.parse(
|
||||
document.querySelector('meta[name=square_contact]').content
|
||||
),
|
||||
currencyCode: document.querySelector('meta[name=currencyCode]')
|
||||
.content,
|
||||
intent: 'CHARGE',
|
||||
};
|
||||
|
||||
const verificationResults = await this.payments.verifyBuyer(
|
||||
result.token,
|
||||
verificationDetails
|
||||
result.token,
|
||||
verificationDetails
|
||||
);
|
||||
|
||||
verificationToken = verificationResults.token;
|
||||
}
|
||||
catch(typeError){
|
||||
console.log(typeError);
|
||||
} catch (typeError) {
|
||||
e.target.parentElement.disabled = true
|
||||
}
|
||||
|
||||
console.debug('Verification Token:', verificationToken);
|
||||
|
||||
document.querySelector('input[name="verificationToken"]').value =
|
||||
verificationToken;
|
||||
document.querySelector(
|
||||
'input[name="verificationToken"]'
|
||||
).value = verificationToken;
|
||||
|
||||
if (result.status === 'OK') {
|
||||
document.getElementById('sourceId').value = result.token;
|
||||
@ -125,23 +107,20 @@ class SquareCreditCard {
|
||||
|
||||
/* SCA */
|
||||
async verifyBuyer(token) {
|
||||
|
||||
console.log("in verify buyer");
|
||||
|
||||
const verificationDetails = {
|
||||
amount: document.querySelector('meta[name=amount]').content,
|
||||
billingContact: document.querySelector('meta[name=square_contact]').content,
|
||||
currencyCode: document.querySelector('meta[name=currencyCode]').content,
|
||||
intent: 'CHARGE'
|
||||
amount: document.querySelector('meta[name=amount]').content,
|
||||
billingContact: document.querySelector('meta[name=square_contact]')
|
||||
.content,
|
||||
currencyCode: document.querySelector('meta[name=currencyCode]')
|
||||
.content,
|
||||
intent: 'CHARGE',
|
||||
};
|
||||
|
||||
const verificationResults = await this.payments.verifyBuyer(
|
||||
token,
|
||||
verificationDetails
|
||||
token,
|
||||
verificationDetails
|
||||
);
|
||||
|
||||
console.log(" verification toke = " + verificationResults.token);
|
||||
|
||||
return verificationResults.token;
|
||||
}
|
||||
|
||||
|
@ -1,37 +1,7 @@
|
||||
@extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.payment_type_credit_card'), 'card_title'
|
||||
=> ctrans('texts.payment_type_credit_card')])
|
||||
|
||||
@section('gateway_head')
|
||||
<meta name="square-appId" content="{{ $gateway->company_gateway->getConfigField('applicationId') }}">
|
||||
<meta name="square-locationId" content="{{ $gateway->company_gateway->getConfigField('locationId') }}">
|
||||
<meta name="square-authorize" content="true">
|
||||
@endsection
|
||||
@extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.credit_card'), 'card_title' => ctrans('texts.credit_card')])
|
||||
|
||||
@section('gateway_content')
|
||||
<form action="{{ route('client.payment_methods.store', ['method' => App\Models\GatewayType::CREDIT_CARD]) }}"
|
||||
method="post" id="server_response">
|
||||
@csrf
|
||||
<input type="text" name="sourceId" id="sourceId" hidden>
|
||||
</form>
|
||||
|
||||
<div class="alert alert-failure mb-4" hidden id="errors"></div>
|
||||
|
||||
@component('portal.ninja2020.components.general.card-element-single')
|
||||
<div id="card-container"></div>
|
||||
<div id="payment-status-container"></div>
|
||||
@endcomponent
|
||||
|
||||
@component('portal.ninja2020.gateways.includes.pay_now', ['id' => 'authorize-card'])
|
||||
{{ ctrans('texts.add_payment_method') }}
|
||||
{{ __('texts.payment_method_cannot_be_preauthorized') }}
|
||||
@endcomponent
|
||||
@endsection
|
||||
|
||||
@section('gateway_footer')
|
||||
@if ($gateway->company_gateway->getConfigField('testMode'))
|
||||
<script type="text/javascript" src="https://sandbox.web.squarecdn.com/v1/square.js"></script>
|
||||
@else
|
||||
<script type="text/javascript" src="https://web.squarecdn.com/v1/square.js"></script>
|
||||
@endif
|
||||
|
||||
<script src="{{ asset('js/clients/payments/square-credit-card.js') }}"></script>
|
||||
@endsection
|
||||
|
Loading…
Reference in New Issue
Block a user