Merge pull request #4052 from turbo124/v2

Fixes for Checkout.com
This commit is contained in:
David Bomba 2020-09-10 14:42:26 +10:00 committed by GitHub
commit 9beafcd73b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 131 additions and 36 deletions

View File

@ -70,6 +70,7 @@ class BaseController extends Controller
'company.clients.contacts', 'company.clients.contacts',
'company.clients.gateway_tokens', 'company.clients.gateway_tokens',
'company.products', 'company.products',
'company.recurring_invoices',
'company.invoices.invitations.contact', 'company.invoices.invitations.contact',
'company.invoices.invitations.company', 'company.invoices.invitations.company',
'company.invoices.documents', 'company.invoices.documents',
@ -213,6 +214,9 @@ class BaseController extends Controller
'company.products' => function ($query) use ($updated_at) { 'company.products' => function ($query) use ($updated_at) {
$query->where('updated_at', '>=', $updated_at); $query->where('updated_at', '>=', $updated_at);
}, },
'company.recurring_invoices'=> function ($query) use ($updated_at) {
$query->where('updated_at', '>=', $updated_at)->with('company');
},
'company.invoices'=> function ($query) use ($updated_at) { 'company.invoices'=> function ($query) use ($updated_at) {
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'company', 'documents'); $query->where('updated_at', '>=', $updated_at)->with('invitations', 'company', 'documents');
}, },

View File

@ -29,6 +29,7 @@ use App\Models\Language;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentType; use App\Models\PaymentType;
use App\Models\Product; use App\Models\Product;
use App\Models\RecurringInvoice;
use App\Models\TaxRate; use App\Models\TaxRate;
use App\Models\Timezone; use App\Models\Timezone;
use App\Models\Traits\AccountTrait; use App\Models\Traits\AccountTrait;
@ -240,6 +241,14 @@ class Company extends BaseModel
return $this->hasMany(Invoice::class)->withTrashed(); return $this->hasMany(Invoice::class)->withTrashed();
} }
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function recurring_invoices()
{
return $this->hasMany(RecurringInvoice::class)->withTrashed();
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */

View File

@ -104,6 +104,9 @@ class AuthorizeCreditCard
$payment = $this->createPaymentRecord($data, $amount); $payment = $this->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta; $payment->meta = $cgt->meta;
$payment->save(); $payment->save();
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->authorize->attachInvoices($payment, $payment_hash); $this->authorize->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash); $payment->service()->updateInvoicePayment($payment_hash);
@ -144,6 +147,9 @@ class AuthorizeCreditCard
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$payment = $this->createPaymentRecord($data, $amount); $payment = $this->createPaymentRecord($data, $amount);
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->authorize->attachInvoices($payment, $payment_hash); $this->authorize->attachInvoices($payment, $payment_hash);

View File

@ -67,13 +67,20 @@ class CheckoutComPaymentDriver extends BaseDriver
]; ];
} }
/** Since with Checkout.com we handle only credit cards, this method should be empty. */ /**
public function setPaymentMethod($string = null) * Since with Checkout.com we handle only credit cards, this method should be empty.
* @param $string payment_method string
*/
public function setPaymentMethod($payment_method = null)
{ {
return $this; return $this;
} }
public function init() /**
* Initialize the checkout payment driver
* @return $this
*/
public function init()
{ {
$config = [ $config = [
'secret' => $this->company_gateway->getConfigField('secretApiKey'), 'secret' => $this->company_gateway->getConfigField('secretApiKey'),
@ -82,8 +89,15 @@ class CheckoutComPaymentDriver extends BaseDriver
]; ];
$this->gateway = new CheckoutApi($config['secret'], $config['sandbox'], $config['public']); $this->gateway = new CheckoutApi($config['secret'], $config['sandbox'], $config['public']);
return $this;
} }
/**
* Process different view depending on payment type
* @param int $gateway_type_id The gateway type
* @return string The view string
*/
public function viewForType($gateway_type_id) public function viewForType($gateway_type_id)
{ {
if ($gateway_type_id == GatewayType::CREDIT_CARD) { if ($gateway_type_id == GatewayType::CREDIT_CARD) {
@ -95,11 +109,23 @@ class CheckoutComPaymentDriver extends BaseDriver
} }
} }
/**
* Authorization view
*
* @param array $data Payment data array
* @return view Authorization View
*/
public function authorizeView($data) public function authorizeView($data)
{ {
return render('gateways.checkout.authorize'); return render('gateways.checkout.authorize');
} }
/**
* Payment View
*
* @param array $data Payment data array
* @return view The payment view
*/
public function processPaymentView(array $data) public function processPaymentView(array $data)
{ {
$data['gateway'] = $this; $data['gateway'] = $this;
@ -113,6 +139,12 @@ class CheckoutComPaymentDriver extends BaseDriver
return render($this->viewForType($data['payment_method_id']), $data); return render($this->viewForType($data['payment_method_id']), $data);
} }
/**
* Process the payment response
*
* @param Request $request The payment request
* @return view The payment response view
*/
public function processPaymentResponse($request) public function processPaymentResponse($request)
{ {
$this->init(); $this->init();
@ -123,6 +155,7 @@ class CheckoutComPaymentDriver extends BaseDriver
'raw_value' => $request->raw_value, 'raw_value' => $request->raw_value,
'currency' => $request->currency, 'currency' => $request->currency,
'payment_hash' =>$request->payment_hash, 'payment_hash' =>$request->payment_hash,
'reference' => $request->payment_hash,
]; ];
$state = array_merge($state, $request->all()); $state = array_merge($state, $request->all());
@ -132,10 +165,12 @@ class CheckoutComPaymentDriver extends BaseDriver
$method = new IdSource($state['token']); $method = new IdSource($state['token']);
$payment = new CheckoutPayment($method, $state['currency']); $payment = new CheckoutPayment($method, $state['currency']);
$payment->amount = $state['value']; $payment->amount = $state['value'];
$payment->reference = $state['reference'];
} else { } else {
$method = new TokenSource($state['server_response']->cardToken); $method = new TokenSource($state['server_response']->cardToken);
$payment = new CheckoutPayment($method, $state['currency']); $payment = new CheckoutPayment($method, $state['currency']);
$payment->amount = $state['value']; $payment->amount = $state['value'];
$payment->reference = $state['reference'];
if ($this->client->currency()->code === 'EUR') { if ($this->client->currency()->code === 'EUR') {
$payment->{'3ds'} = ['enabled' => true]; $payment->{'3ds'} = ['enabled' => true];
@ -162,6 +197,12 @@ class CheckoutComPaymentDriver extends BaseDriver
} }
} }
/**
* Process a successful payment response
*
* @param array $state The state array
* @return view The response
*/
public function processSuccessfulPayment($state) public function processSuccessfulPayment($state)
{ {
$state['charge_id'] = $state['payment_response']->id; $state['charge_id'] = $state['payment_response']->id;
@ -178,6 +219,9 @@ class CheckoutComPaymentDriver extends BaseDriver
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED); $payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$state['payment_hash']])->firstOrFail(); $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$state['payment_hash']])->firstOrFail();
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->attachInvoices($payment, $payment_hash); $this->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash); $payment->service()->updateInvoicePayment($payment_hash);
@ -251,17 +295,19 @@ class CheckoutComPaymentDriver extends BaseDriver
public function processInternallyFailedPayment($e, $state) public function processInternallyFailedPayment($e, $state)
{ {
$message = json_decode($e->getBody()); $error_message = json_decode($e->getBody());
PaymentFailureMailer::dispatch($this->client, $message->error_type, $this->client->company, $state['value']); PaymentFailureMailer::dispatch($this->client, $error_message->message, $this->client->company, $state['value']);
$message = [ $message = [
'server_response' => $state['server_response'], 'server_response' => $state['server_response'],
'data' => $message, 'data' => $e->getBody(),
]; ];
SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client); SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
//todo push to a error page with the exception message.
throw new \Exception('Failed to process the payment.', 1); throw new \Exception('Failed to process the payment.', 1);
} }

View File

@ -162,6 +162,10 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
$payment = $this->createPayment($response->getData()); $payment = $this->createPayment($response->getData());
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->input('payment_hash')])->firstOrFail(); $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->input('payment_hash')])->firstOrFail();
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->attachInvoices($payment, $payment_hash); $this->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash); $payment->service()->updateInvoicePayment($payment_hash);

View File

@ -167,6 +167,9 @@ class Charge
$payment->meta = $cgt->meta; $payment->meta = $cgt->meta;
$payment->save(); $payment->save();
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->stripe->attachInvoices($payment, $payment_hash); $this->stripe->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash); $payment->service()->updateInvoicePayment($payment_hash);

View File

@ -197,6 +197,11 @@ class CreditCard
$payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED); $payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED);
$payment->meta = $payment_meta; $payment->meta = $payment_meta;
$payment->save();
$payment_hash = $state['payment_hash'];
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$payment = $this->stripe->attachInvoices($payment, $state['payment_hash']); $payment = $this->stripe->attachInvoices($payment, $state['payment_hash']);

View File

@ -75,7 +75,9 @@ class UpdateInvoicePayment
->updateBalance($paid_amount * -1) ->updateBalance($paid_amount * -1)
->save(); ->save();
info("firing invoice was updated event");
event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars())); event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars()));
info("fired invoice was updated event");
}); });
return $this->payment; return $this->payment;

View File

@ -29,6 +29,7 @@ use App\Models\PaymentTerm;
use App\Models\Product; use App\Models\Product;
use App\Models\Project; use App\Models\Project;
use App\Models\Quote; use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\Task; use App\Models\Task;
use App\Models\TaxRate; use App\Models\TaxRate;
@ -40,6 +41,7 @@ use App\Transformers\CompanyTokenTransformer;
use App\Transformers\CreditTransformer; use App\Transformers\CreditTransformer;
use App\Transformers\DocumentTransformer; use App\Transformers\DocumentTransformer;
use App\Transformers\PaymentTermTransformer; use App\Transformers\PaymentTermTransformer;
use App\Transformers\RecurringInvoiceTransformer;
use App\Transformers\SystemLogTransformer; use App\Transformers\SystemLogTransformer;
use App\Transformers\TaskTransformer; use App\Transformers\TaskTransformer;
use App\Transformers\WebhookTransformer; use App\Transformers\WebhookTransformer;
@ -70,6 +72,7 @@ class CompanyTransformer extends EntityTransformer
'clients', 'clients',
'contacts', 'contacts',
'invoices', 'invoices',
'recurring_invoices',
'tax_rates', 'tax_rates',
'products', 'products',
'country', 'country',
@ -247,6 +250,13 @@ class CompanyTransformer extends EntityTransformer
return $this->includeCollection($company->invoices, $transformer, Invoice::class); return $this->includeCollection($company->invoices, $transformer, Invoice::class);
} }
public function includeRecurringInvoices(Company $company)
{
$transformer = new RecurringInvoiceTransformer($this->serializer);
return $this->includeCollection($company->recurring_invoices, $transformer, RecurringInvoice::class);
}
public function includeQuotes(Company $company) public function includeQuotes(Company $company)
{ {
$transformer = new QuoteTransformer($this->serializer); $transformer = new QuoteTransformer($this->serializer);

View File

@ -115,10 +115,10 @@ class RecurringInvoiceTransformer extends EntityTransformer
'footer' => $invoice->footer ?: '', 'footer' => $invoice->footer ?: '',
'partial' => (float) ($invoice->partial ?: 0.0), 'partial' => (float) ($invoice->partial ?: 0.0),
'partial_due_date' => $invoice->partial_due_date ?: '', 'partial_due_date' => $invoice->partial_due_date ?: '',
'custom_value1' => (float) $invoice->custom_value1 ?: '', 'custom_value1' => (string) $invoice->custom_value1 ?: '',
'custom_value2' => (float) $invoice->custom_value2 ?: '', 'custom_value2' => (string) $invoice->custom_value2 ?: '',
'custom_value3' => (bool) $invoice->custom_value3 ?: '', 'custom_value3' => (string) $invoice->custom_value3 ?: '',
'custom_value4' => (bool) $invoice->custom_value4 ?: '', 'custom_value4' => (string) $invoice->custom_value4 ?: '',
'has_tasks' => (bool) $invoice->has_tasks, 'has_tasks' => (bool) $invoice->has_tasks,
'has_expenses' => (bool) $invoice->has_expenses, 'has_expenses' => (bool) $invoice->has_expenses,
'custom_surcharge1' => (float) $invoice->custom_surcharge1, 'custom_surcharge1' => (float) $invoice->custom_surcharge1,

50
composer.lock generated
View File

@ -160,16 +160,16 @@
}, },
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "3.152.1", "version": "3.153.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "65e10d45a3ecfdc112b7efe9f839343179a86879" "reference": "3045bc6c8f7d2521b3e0d4a7b407194af8725c8f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/65e10d45a3ecfdc112b7efe9f839343179a86879", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/3045bc6c8f7d2521b3e0d4a7b407194af8725c8f",
"reference": "65e10d45a3ecfdc112b7efe9f839343179a86879", "reference": "3045bc6c8f7d2521b3e0d4a7b407194af8725c8f",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -241,7 +241,7 @@
"s3", "s3",
"sdk" "sdk"
], ],
"time": "2020-09-08T18:13:04+00:00" "time": "2020-09-09T18:12:35+00:00"
}, },
{ {
"name": "brick/math", "name": "brick/math",
@ -529,16 +529,16 @@
}, },
{ {
"name": "composer/composer", "name": "composer/composer",
"version": "1.10.12", "version": "1.10.13",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/composer/composer.git", "url": "https://github.com/composer/composer.git",
"reference": "a6cc92b39c796ec3fb9a360f9c0e578afc2fc96d" "reference": "47c841ba3b2d3fc0b4b13282cf029ea18b66d78b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/composer/composer/zipball/a6cc92b39c796ec3fb9a360f9c0e578afc2fc96d", "url": "https://api.github.com/repos/composer/composer/zipball/47c841ba3b2d3fc0b4b13282cf029ea18b66d78b",
"reference": "a6cc92b39c796ec3fb9a360f9c0e578afc2fc96d", "reference": "47c841ba3b2d3fc0b4b13282cf029ea18b66d78b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -619,7 +619,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2020-09-08T20:58:51+00:00" "time": "2020-09-09T09:46:34+00:00"
}, },
{ {
"name": "composer/package-versions-deprecated", "name": "composer/package-versions-deprecated",
@ -692,16 +692,16 @@
}, },
{ {
"name": "composer/semver", "name": "composer/semver",
"version": "1.6.0", "version": "1.7.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/composer/semver.git", "url": "https://github.com/composer/semver.git",
"reference": "9787c20e39dfeea673665abee0679c73ba67105d" "reference": "114f819054a2ea7db03287f5efb757e2af6e4079"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/composer/semver/zipball/9787c20e39dfeea673665abee0679c73ba67105d", "url": "https://api.github.com/repos/composer/semver/zipball/114f819054a2ea7db03287f5efb757e2af6e4079",
"reference": "9787c20e39dfeea673665abee0679c73ba67105d", "reference": "114f819054a2ea7db03287f5efb757e2af6e4079",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -763,7 +763,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2020-09-08T20:42:08+00:00" "time": "2020-09-09T09:34:06+00:00"
}, },
{ {
"name": "composer/spdx-licenses", "name": "composer/spdx-licenses",
@ -2585,16 +2585,16 @@
}, },
{ {
"name": "laravel/framework", "name": "laravel/framework",
"version": "v7.28.0", "version": "v7.28.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/framework.git", "url": "https://github.com/laravel/framework.git",
"reference": "5d3b4e3e62294716e3c810be3d2eb1375685b87d" "reference": "f7493ab717ca2a9598b1db2d6a3bae8ac8c755e8"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/5d3b4e3e62294716e3c810be3d2eb1375685b87d", "url": "https://api.github.com/repos/laravel/framework/zipball/f7493ab717ca2a9598b1db2d6a3bae8ac8c755e8",
"reference": "5d3b4e3e62294716e3c810be3d2eb1375685b87d", "reference": "f7493ab717ca2a9598b1db2d6a3bae8ac8c755e8",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2739,7 +2739,7 @@
"framework", "framework",
"laravel" "laravel"
], ],
"time": "2020-09-08T15:10:40+00:00" "time": "2020-09-09T15:02:46+00:00"
}, },
{ {
"name": "laravel/slack-notification-channel", "name": "laravel/slack-notification-channel",
@ -2929,16 +2929,16 @@
}, },
{ {
"name": "laravel/ui", "name": "laravel/ui",
"version": "v2.2.1", "version": "v2.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/ui.git", "url": "https://github.com/laravel/ui.git",
"reference": "456daa330a32483b0fa9794334e60af6b2db3bf6" "reference": "2ccaa3b821ea8ac7e05393b946d0578bdb46099b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/ui/zipball/456daa330a32483b0fa9794334e60af6b2db3bf6", "url": "https://api.github.com/repos/laravel/ui/zipball/2ccaa3b821ea8ac7e05393b946d0578bdb46099b",
"reference": "456daa330a32483b0fa9794334e60af6b2db3bf6", "reference": "2ccaa3b821ea8ac7e05393b946d0578bdb46099b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2980,7 +2980,7 @@
"laravel", "laravel",
"ui" "ui"
], ],
"time": "2020-09-08T13:09:39+00:00" "time": "2020-09-09T12:07:59+00:00"
}, },
{ {
"name": "league/commonmark", "name": "league/commonmark",

View File

@ -40,7 +40,11 @@ class AddIsPublicToDocumentsTable extends Migration
$table->decimal('fee_total', 16, 4); $table->decimal('fee_total', 16, 4);
$table->unsignedInteger('fee_invoice_id')->nullable(); $table->unsignedInteger('fee_invoice_id')->nullable();
$table->mediumText('data'); $table->mediumText('data');
$table->unsignedInteger('payment_id')->nullable();
$table->timestamps(6); $table->timestamps(6);
$table->foreign('payment_id')->references('id')->on('payments')->onDelete('cascade')->onUpdate('cascade');
}); });
Schema::table('recurring_invoices', function ($table) { Schema::table('recurring_invoices', function ($table) {

View File

@ -6,6 +6,7 @@
<meta name="customer-email" content="{{ $customer_email }}"> <meta name="customer-email" content="{{ $customer_email }}">
<meta name="value" content="{{ $value }}"> <meta name="value" content="{{ $value }}">
<meta name="currency" content="{{ $currency }}"> <meta name="currency" content="{{ $currency }}">
<meta name="reference" content="{{ $payment_hash }}">
<script src="{{ asset('js/clients/payments/checkout.com.js') }}"></script> <script src="{{ asset('js/clients/payments/checkout.com.js') }}"></script>
@endpush @endpush
@ -15,6 +16,7 @@
@csrf @csrf
<input type="hidden" name="gateway_response"> <input type="hidden" name="gateway_response">
<input type="hidden" name="store_card"> <input type="hidden" name="store_card">
<input type="hidden" name="reference" value="{{ $payment_hash }}">
<input type="hidden" name="payment_hash" value="{{ $payment_hash }}"> <input type="hidden" name="payment_hash" value="{{ $payment_hash }}">
<input type="hidden" name="company_gateway_id" value="{{ $company_gateway->id }}"> <input type="hidden" name="company_gateway_id" value="{{ $company_gateway->id }}">
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}"> <input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}">