diff --git a/.env.example b/.env.example index dd797f50d0..2ab7175fad 100644 --- a/.env.example +++ b/.env.example @@ -74,5 +74,9 @@ WEPAY_CLIENT_SECRET= WEPAY_AUTO_UPDATE=true # Requires permission from WePay WEPAY_ENVIRONMENT=production # production or stage +WEPAY_FEE_PAYER=payee +WEPAY_APP_FEE_MULTIPLIER=0.002 +WEPAY_APP_FEE_FIXED=0 + # See https://www.wepay.com/developer/reference/structures#theme WEPAY_THEME={"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":"0b4d78","background_color":"f8f8f8","button_color":"33b753"}')); \ No newline at end of file diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index fef9a51163..9b02bfe56a 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -369,14 +369,20 @@ class AccountGatewayController extends BaseController $user = Auth::user(); $account = $user->account; - $validator = Validator::make(Input::all(), array( + $rules = array( 'company_name' => 'required', 'description' => 'required', 'tos_agree' => 'required', 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required', - )); + ); + + if (WEPAY_ENABLE_CANADA) { + $rules['country'] = 'required|in:US,CA'; + } + + $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::to('gateways/create') @@ -387,7 +393,7 @@ class AccountGatewayController extends BaseController try{ $wepay = Utils::setupWePay(); - $wepayUser = $wepay->request('user/register/', array( + $userDetails = array( 'client_id' => WEPAY_CLIENT_ID, 'client_secret' => WEPAY_CLIENT_SECRET, 'email' => Input::get('email'), @@ -399,18 +405,31 @@ class AccountGatewayController extends BaseController 'redirect_uri' => URL::to('gateways'), 'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.GATEWAY_WEPAY), 'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money', - )); + ); + + $wepayUser = $wepay->request('user/register/', $userDetails); $accessToken = $wepayUser->access_token; $accessTokenExpires = $wepayUser->expires_in ? (time() + $wepayUser->expires_in) : null; $wepay = new WePay($accessToken); - $wepayAccount = $wepay->request('account/create/', array( + $accountDetails = array( 'name' => Input::get('company_name'), 'description' => Input::get('description'), 'theme_object' => json_decode(WEPAY_THEME), - )); + ); + + if (WEPAY_ENABLE_CANADA) { + $accountDetails['country'] = Input::get('country'); + + if (Input::get('country') == 'CA') { + $accountDetails['currencies'] = ['CAD']; + $accountDetails['country_options'] = ['debit_opt_in' => boolval(Input::get('debit_cards'))]; + } + } + + $wepayAccount = $wepay->request('account/create/', $accountDetails); try { $wepay->request('user/send_confirmation/', []); @@ -435,6 +454,7 @@ class AccountGatewayController extends BaseController 'tokenExpires' => $accessTokenExpires, 'accountId' => $wepayAccount->account_id, 'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE, + 'country' => WEPAY_ENABLE_CANADA ? Input::get('country') : 'US', )); if ($confirmationRequired) { diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 69553f58d5..3e89f30919 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -328,9 +328,8 @@ class PaymentController extends BaseController if ($testMode) { $ref = 'TEST_MODE'; } else { - $gateway = $this->paymentService->createGateway($accountGateway); $details = self::getLicensePaymentDetails(Input::all(), $affiliate); - $response = $gateway->purchase($details)->send(); + $response = $this->paymentService->purchase($accountGateway, $details); $ref = $response->getTransactionReference(); if (!$response->isSuccessful() || !$ref) { @@ -552,7 +551,7 @@ class PaymentController extends BaseController } } - $response = $gateway->purchase($details)->send(); + $response = $this->paymentService->purchase($accountGateway, $details); if ($accountGateway->gateway_id == GATEWAY_EWAY) { $ref = $response->getData()['AccessCode']; diff --git a/app/Http/routes.php b/app/Http/routes.php index 321eeade2a..46fe5cea83 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -759,8 +759,13 @@ if (!defined('CONTACT_EMAIL')) { define('WEPAY_CLIENT_SECRET', env('WEPAY_CLIENT_SECRET')); define('WEPAY_AUTO_UPDATE', env('WEPAY_AUTO_UPDATE', false)); define('WEPAY_ENVIRONMENT', env('WEPAY_ENVIRONMENT', WEPAY_PRODUCTION)); + define('WEPAY_ENABLE_CANADA', env('WEPAY_ENABLE_CANADA', false)); define('WEPAY_THEME', env('WEPAY_THEME','{"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":"0b4d78","background_color":"f8f8f8","button_color":"33b753"}')); - + + define('WEPAY_FEE_PAYER', env('WEPAY_FEE_PAYER', 'payee')); + define('WEPAY_APP_FEE_MULTIPLIER', env('WEPAY_APP_FEE_MULTIPLIER', 0.002)); + define('WEPAY_APP_FEE_FIXED', env('WEPAY_APP_FEE_MULTIPLIER', 0.00)); + $creditCards = [ 1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'], 2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'], diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index d1a0a29a71..ae014577a6 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -602,9 +602,8 @@ class PaymentService extends BaseService $account = $invoice->account; $accountGateway = $account->getGatewayConfig(GATEWAY_CHECKOUT_COM); - $gateway = $this->createGateway($accountGateway); - $response = $gateway->purchase([ + $response = $this->purchase($accountGateway, [ 'amount' => $invoice->getRequestedAmount(), 'currency' => $client->currency ? $client->currency->code : ($account->currency ? $account->currency->code : 'USD') ])->send(); @@ -836,7 +835,6 @@ class PaymentService extends BaseService } // setup the gateway/payment info - $gateway = $this->createGateway($accountGateway); $details = $this->getPaymentDetails($invitation, $accountGateway); $details['customerReference'] = $token; @@ -846,7 +844,7 @@ class PaymentService extends BaseService } // submit purchase/get response - $response = $gateway->purchase($details)->send(); + $response = $this->purchase($accountGateway, $details); if ($response->isSuccessful()) { $ref = $response->getTransactionReference(); @@ -994,7 +992,7 @@ class PaymentService extends BaseService $amount = !empty($params['amount']) ? floatval($params['amount']) : null; if ($this->refund($payment, $amount)) { $successful++; - } + } } } @@ -1033,11 +1031,9 @@ class PaymentService extends BaseService } public function refund($payment, $amount = null) { - if (!$amount) { - $amount = $payment->amount; + if ($amount) { + $amount = min($amount, $payment->amount - $payment->refunded); } - - $amount = min($amount, $payment->amount - $payment->refunded); $accountGateway = $payment->account_gateway; @@ -1052,75 +1048,56 @@ class PaymentService extends BaseService if ($payment->payment_type_id != PAYMENT_TYPE_CREDIT) { $gateway = $this->createGateway($accountGateway); - if ($accountGateway->gateway_id != GATEWAY_WEPAY) { - $refund = $gateway->refund(array( - 'transactionReference' => $payment->transaction_reference, - 'amount' => $amount, - )); - $response = $refund->send(); + $details = array( + 'transactionReference' => $payment->transaction_reference, + ); - if ($response->isSuccessful()) { - $payment->recordRefund($amount); - } else { - $data = $response->getData(); + if ($amount != ($payment->amount - $payment->refunded)) { + $details['amount'] = $amount; + } - if ($data instanceof \Braintree\Result\Error) { - $error = $data->errors->deepAll()[0]; - if ($error && $error->code == 91506) { - if ($amount == $payment->amount) { - // This is an unsettled transaction; try to void it - $void = $gateway->void(array( - 'transactionReference' => $payment->transaction_reference, - )); - $response = $void->send(); + if ($accountGateway->gateway_id == GATEWAY_WEPAY) { + $details['refund_reason'] = 'Refund issued by merchant.'; + } - if ($response->isSuccessful()) { - $payment->markVoided(); - } - } else { - $this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway); - return false; - } - } + $refund = $gateway->refund($details); + $response = $refund->send(); + + if ($response->isSuccessful()) { + $payment->recordRefund($amount); + } else { + $data = $response->getData(); + + if ($data instanceof \Braintree\Result\Error) { + $error = $data->errors->deepAll()[0]; + if ($error && $error->code == 91506) { + $tryVoid = true; } + } elseif ($accountGateway->gateway_id == GATEWAY_WEPAY && $response->getCode() == 4004) { + $tryVoid = true; + } - if (!$response->isSuccessful()) { - $this->error('Unknown', $response->getMessage(), $accountGateway); + if (!empty($tryVoid)) { + if ($amount == $payment->amount) { + // This is an unsettled transaction; try to void it + $void = $gateway->void(array( + 'transactionReference' => $payment->transaction_reference, + )); + $response = $void->send(); + + if ($response->isSuccessful()) { + $payment->markVoided(); + } + } else { + $this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway); return false; } } - } else { - $wepay = \Utils::setupWePay($accountGateway); - try { - $wepay->request('checkout/refund', array( - 'checkout_id' => intval($payment->transaction_reference), - 'refund_reason' => 'Refund issued by merchant.', - 'amount' => $amount, - )); - $payment->recordRefund($amount); - } catch (\WePayException $ex) { - if ($ex->getCode() == 4004) { - if ($amount == $payment->amount) { - try { - // This is an uncaptured transaction; try to cancel it - $wepay->request('checkout/cancel', array( - 'checkout_id' => intval($payment->transaction_reference), - 'cancel_reason' => 'Refund issued by merchant.', - )); - $payment->markVoided(); - } catch (\WePayException $ex) { - $this->error('Unknown', $ex->getMessage(), $accountGateway); - } - } else { - $this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway); - return false; - } - } else { - $this->error('Unknown', $ex->getMessage(), $accountGateway); - } + if (!$response->isSuccessful()) { + $this->error('Unknown', $response->getMessage(), $accountGateway); + return false; } - } } else { $payment->recordRefund($amount); @@ -1215,4 +1192,27 @@ class PaymentService extends BaseService return $e->getMessage(); } } + + public function purchase($accountGateway, $details) { + $gateway = $this->createGateway($accountGateway); + + if ($accountGateway->gateway_id == GATEWAY_WEPAY) { + $details['applicationFee'] = $this->calculateApplicationFee($accountGateway, $details['amount']); + $details['feePayer'] = WEPAY_FEE_PAYER; + } + + $response = $gateway->purchase($details)->send(); + + return $response; + } + + private function calculateApplicationFee($accountGateway, $amount) { + if ($accountGateway->gateway_id = GATEWAY_WEPAY) { + $fee = WEPAY_APP_FEE_MULTIPLIER * $amount + WEPAY_APP_FEE_FIXED; + + return floor(min($fee, $amount * 0.2));// Maximum fee is 20% of the amount. + } + + return 0; + } } diff --git a/composer.json b/composer.json index cff2d85bcb..65d898e585 100644 --- a/composer.json +++ b/composer.json @@ -63,7 +63,6 @@ "meebio/omnipay-secure-trading": "dev-master", "justinbusschau/omnipay-secpay": "~2.0", "labs7in0/omnipay-wechat": "dev-master", - "collizo4sky/omnipay-wepay": "~1.0", "laracasts/presenter": "dev-master", "jlapp/swaggervel": "master-dev", "maatwebsite/excel": "~2.0", @@ -75,7 +74,8 @@ "barracudanetworks/archivestream-php": "^1.0", "omnipay/braintree": "~2.0@dev", "gatepay/FedACHdir": "dev-master@dev", - "wepay/php-sdk": "^0.2" + "wepay/php-sdk": "^0.2", + "collizo4sky/omnipay-wepay": "dev-additional-calls" }, "require-dev": { "phpunit/phpunit": "~4.0", @@ -141,6 +141,10 @@ "reference": "origin/master" } } + }, + { + "type": "vcs", + "url": "https://github.com/sometechie/omnipay-wepay" } ] } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index f1adcabada..e766e13afb 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1313,6 +1313,10 @@ $LANG = array( 'switch' => 'Switch', 'restore_account_gateway' => 'Restore Gateway', 'restored_account_gateway' => 'Successfully restored gateway', + 'united_states' => 'United States', + 'canada' => 'Canada', + 'accept_debit_cards' => 'Accept Debit Cards', + 'debit_cards' => 'Debit Cards', ); return $LANG; diff --git a/resources/views/accounts/partials/account_gateway_wepay.blade.php b/resources/views/accounts/partials/account_gateway_wepay.blade.php index 803fc37ed1..a5ccd7f01a 100644 --- a/resources/views/accounts/partials/account_gateway_wepay.blade.php +++ b/resources/views/accounts/partials/account_gateway_wepay.blade.php @@ -5,8 +5,12 @@ 'description' => 'required', 'company_name' => 'required', 'tos_agree' => 'required', + 'country' => 'required', ))->addClass('warn-on-exit') !!} {!! Former::populateField('company_name', $account->getDisplayName()) !!} +@if($account->country) + {!! Former::populateField('country', $account->country->iso_3166_2) !!} +@endif {!! Former::populateField('first_name', $user->first_name) !!} {!! Former::populateField('last_name', $user->last_name) !!} {!! Former::populateField('email', $user->email) !!} @@ -23,25 +27,34 @@ {!! Former::text('email') !!} {!! Former::text('company_name')->help('wepay_company_name_help')->maxlength(255) !!} {!! Former::text('description')->help('wepay_description_help') !!} + @if (WEPAY_ENABLE_CANADA) +
+ {!! Former::radios('country') + ->radios([ + trans('texts.united_states') => ['value' => 'US'], + trans('texts.canada') => ['value' => 'CA'], + ]) !!} +
+
+ {!! Former::checkbox('debit_cards') + ->text(trans('texts.accept_debit_cards')) !!} +
+ @endif {!! Former::select('token_billing_type_id') ->options($tokenBillingOptions) ->help(trans('texts.token_billing_help')) !!} {!! Former::checkbox('show_address') ->label(trans('texts.billing_address')) - ->text(trans('texts.show_address_help')) - ->addGroupClass('gateway-option') !!} + ->text(trans('texts.show_address_help')) !!} {!! Former::checkbox('update_address') ->label(' ') - ->text(trans('texts.update_address_help')) - ->addGroupClass('gateway-option') !!} + ->text(trans('texts.update_address_help')) !!} {!! Former::checkboxes('creditCardTypes[]') ->label('Accepted Credit Cards') ->checkboxes($creditCardTypes) - ->class('creditcard-types') - ->addGroupClass('gateway-option') - !!} + ->class('creditcard-types') !!} {!! Former::checkbox('tos_agree')->label(' ')->text(trans('texts.wepay_tos_agree', - ['link'=>''.trans('texts.wepay_tos_link_text').''] + ['link'=>''.trans('texts.wepay_tos_link_text').''] ))->value('true') !!}
{!! Button::primary(trans('texts.sign_up_with_wepay')) @@ -54,6 +67,21 @@
- + + {!! Former::close() !!} \ No newline at end of file