From 168bb7cfb068b009a2e828c59809342ad16c3b64 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Fri, 17 Apr 2015 14:57:17 +0300 Subject: [PATCH] Added support for partial payments --- app/Http/Controllers/PaymentController.php | 38 +++++++++++++------ app/Models/Gateway.php | 2 + readme.md | 4 +- resources/lang/da/texts.php | 2 +- resources/lang/de/texts.php | 2 +- resources/lang/en/texts.php | 2 +- resources/lang/es/texts.php | 2 +- resources/lang/es_ES/texts.php | 2 +- resources/lang/fr/texts.php | 2 +- resources/lang/it/texts.php | 2 +- resources/lang/lt/texts.php | 2 +- resources/lang/nb_NO/texts.php | 2 +- resources/lang/nl/texts.php | 2 +- resources/lang/pt_BR/texts.php | 2 +- resources/lang/sv/texts.php | 2 +- .../views/accounts/account_gateway.blade.php | 2 +- resources/views/invoices/edit.blade.php | 2 +- 17 files changed, 44 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 6bc4b70089..cfa492463b 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -246,8 +246,7 @@ class PaymentController extends BaseController private function getPaymentDetails($invitation, $input = null) { $invoice = $invitation->invoice; - $key = $invoice->invoice_number.'_details'; - $gateway = $invoice->client->account->getGatewayByType(Session::get('payment_type'))->gateway; + $key = $invoice->account_id.'-'.$invoice->invoice_number; $currencyCode = $invoice->client->currency ? $invoice->client->currency->code : ($invoice->account->currency ? $invoice->account->currency->code : 'USD'); if ($input) { @@ -568,6 +567,7 @@ class PaymentController extends BaseController $invitation->transaction_reference = $ref; $invitation->save(); + Session::put('transaction_reference', $ref); Session::save(); $response->redirect(); } else { @@ -630,6 +630,14 @@ class PaymentController extends BaseController $payerId = Request::query('PayerID'); $token = Request::query('token'); + if (!$token) { + $token = Session::pull('transaction_reference'); + } + + if (!$token) { + return redirect(NINJA_WEB_URL); + } + $invitation = Invitation::with('invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('transaction_reference', '=', $token)->firstOrFail(); $invoice = $invitation->invoice; @@ -637,20 +645,26 @@ class PaymentController extends BaseController $gateway = self::createGateway($accountGateway); try { - $details = self::getPaymentDetails($invitation); - $response = $gateway->completePurchase($details)->send(); - $ref = $response->getTransactionReference(); + if (method_exists($gateway, 'completePurchase')) { + $details = self::getPaymentDetails($invitation); + $response = $gateway->completePurchase($details)->send(); + $ref = $response->getTransactionReference(); - if ($response->isSuccessful()) { - $payment = self::createPayment($invitation, $ref, $payerId); + if ($response->isSuccessful()) { + $payment = self::createPayment($invitation, $ref, $payerId); + Session::flash('message', trans('texts.applied_payment')); - Session::flash('message', trans('texts.applied_payment')); + return Redirect::to('view/'.$invitation->invitation_key); + } else { + $errorMessage = trans('texts.payment_error')."\n\n".$response->getMessage(); + Session::flash('error', $errorMessage); + Utils::logError($errorMessage); - return Redirect::to('view/'.$invitation->invitation_key); + return Redirect::to('view/'.$invitation->invitation_key); + } } else { - $errorMessage = trans('texts.payment_error')."\n\n".$response->getMessage(); - Session::flash('error', $errorMessage); - Utils::logError($errorMessage); + $payment = self::createPayment($invitation, $token, $payerId); + Session::flash('message', trans('texts.applied_payment')); return Redirect::to('view/'.$invitation->invitation_key); } diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index d0b4bd8479..5af3974f62 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -22,6 +22,8 @@ class Gateway extends Eloquent $link = 'https://www.paypal.com/us/cgi-bin/webscr?cmd=_login-api-run'; } elseif ($this->id == GATEWAY_TWO_CHECKOUT) { $link = 'https://www.2checkout.com/referral?r=2c37ac2298'; + } elseif ($this->id == GATEWAY_BITPAY) { + $link = 'https://bitpay.com/dashboard/signup'; } $key = 'texts.gateway_help_'.$this->id; diff --git a/readme.md b/readme.md index 37236a85d6..c74e58eb54 100644 --- a/readme.md +++ b/readme.md @@ -46,8 +46,8 @@ Developed by [@hillelcoren](https://twitter.com/hillelcoren) | Designed by [kant ### Steps to setup from GitHub If you plan on submitting changes it's best to [fork the repo](https://help.github.com/articles/fork-a-repo), otherwise you can just checkout the code. - - git clone https://github.com/hillelcoren/invoice-ninja.git ninja + + git clone git@github.com:hillelcoren/invoice-ninja.git ninja cd ninja Install Laravel packages using Composer diff --git a/resources/lang/da/texts.php b/resources/lang/da/texts.php index 59c05e5635..3eef8bc21c 100644 --- a/resources/lang/da/texts.php +++ b/resources/lang/da/texts.php @@ -587,7 +587,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/de/texts.php b/resources/lang/de/texts.php index 646ebf4af9..38bc210a37 100644 --- a/resources/lang/de/texts.php +++ b/resources/lang/de/texts.php @@ -578,7 +578,7 @@ return array( 'resend_confirmation' => 'Bestätigungsmail erneut senden', 'confirmation_resent' => 'Bestätigungsemail wurde erneut gesendet', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 3ab0a35d27..2417bc5fef 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -585,7 +585,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/es/texts.php b/resources/lang/es/texts.php index e00057bcbb..1008331520 100644 --- a/resources/lang/es/texts.php +++ b/resources/lang/es/texts.php @@ -557,7 +557,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/es_ES/texts.php b/resources/lang/es_ES/texts.php index 0fbf403023..1ffb5addd9 100644 --- a/resources/lang/es_ES/texts.php +++ b/resources/lang/es_ES/texts.php @@ -586,7 +586,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/fr/texts.php b/resources/lang/fr/texts.php index 13c9ccb882..c7eb81adaa 100644 --- a/resources/lang/fr/texts.php +++ b/resources/lang/fr/texts.php @@ -578,7 +578,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/it/texts.php b/resources/lang/it/texts.php index ad40d5014e..a750f07f2a 100644 --- a/resources/lang/it/texts.php +++ b/resources/lang/it/texts.php @@ -580,7 +580,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/lt/texts.php b/resources/lang/lt/texts.php index 3bca31dd23..5c4915bb41 100644 --- a/resources/lang/lt/texts.php +++ b/resources/lang/lt/texts.php @@ -588,7 +588,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/nb_NO/texts.php b/resources/lang/nb_NO/texts.php index 20cbada5e5..724200d007 100644 --- a/resources/lang/nb_NO/texts.php +++ b/resources/lang/nb_NO/texts.php @@ -586,7 +586,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/nl/texts.php b/resources/lang/nl/texts.php index 0c9d11a851..be8838eb39 100644 --- a/resources/lang/nl/texts.php +++ b/resources/lang/nl/texts.php @@ -581,7 +581,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/pt_BR/texts.php b/resources/lang/pt_BR/texts.php index ab1709120f..4827bf0a99 100644 --- a/resources/lang/pt_BR/texts.php +++ b/resources/lang/pt_BR/texts.php @@ -581,7 +581,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/lang/sv/texts.php b/resources/lang/sv/texts.php index 936fc232bf..3e9edf79d0 100644 --- a/resources/lang/sv/texts.php +++ b/resources/lang/sv/texts.php @@ -584,7 +584,7 @@ return array( 'resend_confirmation' => 'Resend confirmation email', 'confirmation_resent' => 'The confirmation email was resent', - 'gateway_help_42' => 'Note: use a BitPay Legacy API Key, not an API token.', + 'gateway_help_42' => ':link to sign up for BitPay.
Note: use a Legacy API Key, not an API token.', 'payment_type_credit_card' => 'Credit card', 'payment_type_paypal' => 'PayPal', 'payment_type_bitcoin' => 'Bitcoin', diff --git a/resources/views/accounts/account_gateway.blade.php b/resources/views/accounts/account_gateway.blade.php index 92f55fac21..6158cc5e43 100644 --- a/resources/views/accounts/account_gateway.blade.php +++ b/resources/views/accounts/account_gateway.blade.php @@ -22,7 +22,7 @@ @endforeach @endif @else - {!! Former::populateField('gateway_id', GATEWAY_AUTHORIZE_NET) !!} + {!! Former::populateField('gateway_id', GATEWAY_STRIPE) !!} @endif {!! Former::select('payment_type_id') diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 6e68b18e9b..2f700e9b10 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -1655,7 +1655,7 @@ } @if ($data) - window.model = new ViewModel({{ $data }}); + window.model = new ViewModel({!! $data !!}); @else window.model = new ViewModel(); model.addTaxRate();