1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Support app fees and Canadian users

This commit is contained in:
Joshua Dwire 2016-05-17 11:32:17 -04:00
parent f6942a199f
commit 43fade2339
8 changed files with 153 additions and 89 deletions

View File

@ -74,5 +74,9 @@ WEPAY_CLIENT_SECRET=
WEPAY_AUTO_UPDATE=true # Requires permission from WePay WEPAY_AUTO_UPDATE=true # Requires permission from WePay
WEPAY_ENVIRONMENT=production # production or stage 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 # 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"}')); WEPAY_THEME={"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":"0b4d78","background_color":"f8f8f8","button_color":"33b753"}'));

View File

@ -369,14 +369,20 @@ class AccountGatewayController extends BaseController
$user = Auth::user(); $user = Auth::user();
$account = $user->account; $account = $user->account;
$validator = Validator::make(Input::all(), array( $rules = array(
'company_name' => 'required', 'company_name' => 'required',
'description' => 'required', 'description' => 'required',
'tos_agree' => 'required', 'tos_agree' => 'required',
'first_name' => 'required', 'first_name' => 'required',
'last_name' => 'required', 'last_name' => 'required',
'email' => 'required', 'email' => 'required',
)); );
if (WEPAY_ENABLE_CANADA) {
$rules['country'] = 'required|in:US,CA';
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) { if ($validator->fails()) {
return Redirect::to('gateways/create') return Redirect::to('gateways/create')
@ -387,7 +393,7 @@ class AccountGatewayController extends BaseController
try{ try{
$wepay = Utils::setupWePay(); $wepay = Utils::setupWePay();
$wepayUser = $wepay->request('user/register/', array( $userDetails = array(
'client_id' => WEPAY_CLIENT_ID, 'client_id' => WEPAY_CLIENT_ID,
'client_secret' => WEPAY_CLIENT_SECRET, 'client_secret' => WEPAY_CLIENT_SECRET,
'email' => Input::get('email'), 'email' => Input::get('email'),
@ -399,18 +405,31 @@ class AccountGatewayController extends BaseController
'redirect_uri' => URL::to('gateways'), 'redirect_uri' => URL::to('gateways'),
'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.GATEWAY_WEPAY), 'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.GATEWAY_WEPAY),
'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money', 'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money',
)); );
$wepayUser = $wepay->request('user/register/', $userDetails);
$accessToken = $wepayUser->access_token; $accessToken = $wepayUser->access_token;
$accessTokenExpires = $wepayUser->expires_in ? (time() + $wepayUser->expires_in) : null; $accessTokenExpires = $wepayUser->expires_in ? (time() + $wepayUser->expires_in) : null;
$wepay = new WePay($accessToken); $wepay = new WePay($accessToken);
$wepayAccount = $wepay->request('account/create/', array( $accountDetails = array(
'name' => Input::get('company_name'), 'name' => Input::get('company_name'),
'description' => Input::get('description'), 'description' => Input::get('description'),
'theme_object' => json_decode(WEPAY_THEME), '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 { try {
$wepay->request('user/send_confirmation/', []); $wepay->request('user/send_confirmation/', []);
@ -435,6 +454,7 @@ class AccountGatewayController extends BaseController
'tokenExpires' => $accessTokenExpires, 'tokenExpires' => $accessTokenExpires,
'accountId' => $wepayAccount->account_id, 'accountId' => $wepayAccount->account_id,
'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE, 'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE,
'country' => WEPAY_ENABLE_CANADA ? Input::get('country') : 'US',
)); ));
if ($confirmationRequired) { if ($confirmationRequired) {

View File

@ -328,9 +328,8 @@ class PaymentController extends BaseController
if ($testMode) { if ($testMode) {
$ref = 'TEST_MODE'; $ref = 'TEST_MODE';
} else { } else {
$gateway = $this->paymentService->createGateway($accountGateway);
$details = self::getLicensePaymentDetails(Input::all(), $affiliate); $details = self::getLicensePaymentDetails(Input::all(), $affiliate);
$response = $gateway->purchase($details)->send(); $response = $this->paymentService->purchase($accountGateway, $details);
$ref = $response->getTransactionReference(); $ref = $response->getTransactionReference();
if (!$response->isSuccessful() || !$ref) { 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) { if ($accountGateway->gateway_id == GATEWAY_EWAY) {
$ref = $response->getData()['AccessCode']; $ref = $response->getData()['AccessCode'];

View File

@ -759,8 +759,13 @@ if (!defined('CONTACT_EMAIL')) {
define('WEPAY_CLIENT_SECRET', env('WEPAY_CLIENT_SECRET')); define('WEPAY_CLIENT_SECRET', env('WEPAY_CLIENT_SECRET'));
define('WEPAY_AUTO_UPDATE', env('WEPAY_AUTO_UPDATE', false)); define('WEPAY_AUTO_UPDATE', env('WEPAY_AUTO_UPDATE', false));
define('WEPAY_ENVIRONMENT', env('WEPAY_ENVIRONMENT', WEPAY_PRODUCTION)); 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_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 = [ $creditCards = [
1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'], 1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'],
2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'], 2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'],

View File

@ -602,9 +602,8 @@ class PaymentService extends BaseService
$account = $invoice->account; $account = $invoice->account;
$accountGateway = $account->getGatewayConfig(GATEWAY_CHECKOUT_COM); $accountGateway = $account->getGatewayConfig(GATEWAY_CHECKOUT_COM);
$gateway = $this->createGateway($accountGateway);
$response = $gateway->purchase([ $response = $this->purchase($accountGateway, [
'amount' => $invoice->getRequestedAmount(), 'amount' => $invoice->getRequestedAmount(),
'currency' => $client->currency ? $client->currency->code : ($account->currency ? $account->currency->code : 'USD') 'currency' => $client->currency ? $client->currency->code : ($account->currency ? $account->currency->code : 'USD')
])->send(); ])->send();
@ -836,7 +835,6 @@ class PaymentService extends BaseService
} }
// setup the gateway/payment info // setup the gateway/payment info
$gateway = $this->createGateway($accountGateway);
$details = $this->getPaymentDetails($invitation, $accountGateway); $details = $this->getPaymentDetails($invitation, $accountGateway);
$details['customerReference'] = $token; $details['customerReference'] = $token;
@ -846,7 +844,7 @@ class PaymentService extends BaseService
} }
// submit purchase/get response // submit purchase/get response
$response = $gateway->purchase($details)->send(); $response = $this->purchase($accountGateway, $details);
if ($response->isSuccessful()) { if ($response->isSuccessful()) {
$ref = $response->getTransactionReference(); $ref = $response->getTransactionReference();
@ -994,7 +992,7 @@ class PaymentService extends BaseService
$amount = !empty($params['amount']) ? floatval($params['amount']) : null; $amount = !empty($params['amount']) ? floatval($params['amount']) : null;
if ($this->refund($payment, $amount)) { if ($this->refund($payment, $amount)) {
$successful++; $successful++;
} }
} }
} }
@ -1033,11 +1031,9 @@ class PaymentService extends BaseService
} }
public function refund($payment, $amount = null) { public function refund($payment, $amount = null) {
if (!$amount) { if ($amount) {
$amount = $payment->amount; $amount = min($amount, $payment->amount - $payment->refunded);
} }
$amount = min($amount, $payment->amount - $payment->refunded);
$accountGateway = $payment->account_gateway; $accountGateway = $payment->account_gateway;
@ -1052,75 +1048,56 @@ class PaymentService extends BaseService
if ($payment->payment_type_id != PAYMENT_TYPE_CREDIT) { if ($payment->payment_type_id != PAYMENT_TYPE_CREDIT) {
$gateway = $this->createGateway($accountGateway); $gateway = $this->createGateway($accountGateway);
if ($accountGateway->gateway_id != GATEWAY_WEPAY) { $details = array(
$refund = $gateway->refund(array( 'transactionReference' => $payment->transaction_reference,
'transactionReference' => $payment->transaction_reference, );
'amount' => $amount,
));
$response = $refund->send();
if ($response->isSuccessful()) { if ($amount != ($payment->amount - $payment->refunded)) {
$payment->recordRefund($amount); $details['amount'] = $amount;
} else { }
$data = $response->getData();
if ($data instanceof \Braintree\Result\Error) { if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
$error = $data->errors->deepAll()[0]; $details['refund_reason'] = 'Refund issued by merchant.';
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 ($response->isSuccessful()) { $refund = $gateway->refund($details);
$payment->markVoided(); $response = $refund->send();
}
} else { if ($response->isSuccessful()) {
$this->error('Unknown', 'Partial refund not allowed for unsettled transactions.', $accountGateway); $payment->recordRefund($amount);
return false; } 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()) { if (!empty($tryVoid)) {
$this->error('Unknown', $response->getMessage(), $accountGateway); 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; return false;
} }
} }
} else {
$wepay = \Utils::setupWePay($accountGateway);
try { if (!$response->isSuccessful()) {
$wepay->request('checkout/refund', array( $this->error('Unknown', $response->getMessage(), $accountGateway);
'checkout_id' => intval($payment->transaction_reference), return false;
'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);
}
} }
} }
} else { } else {
$payment->recordRefund($amount); $payment->recordRefund($amount);
@ -1215,4 +1192,27 @@ class PaymentService extends BaseService
return $e->getMessage(); 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;
}
} }

View File

@ -63,7 +63,6 @@
"meebio/omnipay-secure-trading": "dev-master", "meebio/omnipay-secure-trading": "dev-master",
"justinbusschau/omnipay-secpay": "~2.0", "justinbusschau/omnipay-secpay": "~2.0",
"labs7in0/omnipay-wechat": "dev-master", "labs7in0/omnipay-wechat": "dev-master",
"collizo4sky/omnipay-wepay": "~1.0",
"laracasts/presenter": "dev-master", "laracasts/presenter": "dev-master",
"jlapp/swaggervel": "master-dev", "jlapp/swaggervel": "master-dev",
"maatwebsite/excel": "~2.0", "maatwebsite/excel": "~2.0",
@ -75,7 +74,8 @@
"barracudanetworks/archivestream-php": "^1.0", "barracudanetworks/archivestream-php": "^1.0",
"omnipay/braintree": "~2.0@dev", "omnipay/braintree": "~2.0@dev",
"gatepay/FedACHdir": "dev-master@dev", "gatepay/FedACHdir": "dev-master@dev",
"wepay/php-sdk": "^0.2" "wepay/php-sdk": "^0.2",
"collizo4sky/omnipay-wepay": "dev-additional-calls"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.0", "phpunit/phpunit": "~4.0",
@ -141,6 +141,10 @@
"reference": "origin/master" "reference": "origin/master"
} }
} }
},
{
"type": "vcs",
"url": "https://github.com/sometechie/omnipay-wepay"
} }
] ]
} }

View File

@ -1313,6 +1313,10 @@ $LANG = array(
'switch' => 'Switch', 'switch' => 'Switch',
'restore_account_gateway' => 'Restore Gateway', 'restore_account_gateway' => 'Restore Gateway',
'restored_account_gateway' => 'Successfully restored 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; return $LANG;

View File

@ -5,8 +5,12 @@
'description' => 'required', 'description' => 'required',
'company_name' => 'required', 'company_name' => 'required',
'tos_agree' => 'required', 'tos_agree' => 'required',
'country' => 'required',
))->addClass('warn-on-exit') !!} ))->addClass('warn-on-exit') !!}
{!! Former::populateField('company_name', $account->getDisplayName()) !!} {!! 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('first_name', $user->first_name) !!}
{!! Former::populateField('last_name', $user->last_name) !!} {!! Former::populateField('last_name', $user->last_name) !!}
{!! Former::populateField('email', $user->email) !!} {!! Former::populateField('email', $user->email) !!}
@ -23,25 +27,34 @@
{!! Former::text('email') !!} {!! Former::text('email') !!}
{!! Former::text('company_name')->help('wepay_company_name_help')->maxlength(255) !!} {!! Former::text('company_name')->help('wepay_company_name_help')->maxlength(255) !!}
{!! Former::text('description')->help('wepay_description_help') !!} {!! Former::text('description')->help('wepay_description_help') !!}
@if (WEPAY_ENABLE_CANADA)
<div id="wepay-country">
{!! Former::radios('country')
->radios([
trans('texts.united_states') => ['value' => 'US'],
trans('texts.canada') => ['value' => 'CA'],
]) !!}
</div>
<div id="wepay-accept-debit">
{!! Former::checkbox('debit_cards')
->text(trans('texts.accept_debit_cards')) !!}
</div>
@endif
{!! Former::select('token_billing_type_id') {!! Former::select('token_billing_type_id')
->options($tokenBillingOptions) ->options($tokenBillingOptions)
->help(trans('texts.token_billing_help')) !!} ->help(trans('texts.token_billing_help')) !!}
{!! Former::checkbox('show_address') {!! Former::checkbox('show_address')
->label(trans('texts.billing_address')) ->label(trans('texts.billing_address'))
->text(trans('texts.show_address_help')) ->text(trans('texts.show_address_help')) !!}
->addGroupClass('gateway-option') !!}
{!! Former::checkbox('update_address') {!! Former::checkbox('update_address')
->label(' ') ->label(' ')
->text(trans('texts.update_address_help')) ->text(trans('texts.update_address_help')) !!}
->addGroupClass('gateway-option') !!}
{!! Former::checkboxes('creditCardTypes[]') {!! Former::checkboxes('creditCardTypes[]')
->label('Accepted Credit Cards') ->label('Accepted Credit Cards')
->checkboxes($creditCardTypes) ->checkboxes($creditCardTypes)
->class('creditcard-types') ->class('creditcard-types') !!}
->addGroupClass('gateway-option')
!!}
{!! Former::checkbox('tos_agree')->label(' ')->text(trans('texts.wepay_tos_agree', {!! Former::checkbox('tos_agree')->label(' ')->text(trans('texts.wepay_tos_agree',
['link'=>'<a href="https://go.wepay.com/terms-of-service-us" target="_blank">'.trans('texts.wepay_tos_link_text').'</a>'] ['link'=>'<a id="wepay-tos-link" href="https://go.wepay.com/terms-of-service-us" target="_blank">'.trans('texts.wepay_tos_link_text').'</a>']
))->value('true') !!} ))->value('true') !!}
<center> <center>
{!! Button::primary(trans('texts.sign_up_with_wepay')) {!! Button::primary(trans('texts.sign_up_with_wepay'))
@ -54,6 +67,21 @@
</center> </center>
</div> </div>
</div> </div>
<style>#other-providers{display:none}</style> <style>
#other-providers{display:none}
#wepay-country .radio{display:inline-block;padding-right:15px}
#wepay-country .radio label{padding-left:0}
</style>
<script type="text/javascript">
$(function(){
$('#wepay-country input').change(handleCountryChange)
function handleCountryChange(){
var country = $('#wepay-country input:checked').val();
$('#wepay-accept-debit').toggle(country == 'CA');
$('#wepay-tos-link').attr('href', 'https://go.wepay.com/terms-of-service-' + country.toLowerCase());
}
handleCountryChange();
})
</script>
<input type="hidden" name="gateway_id" value="{{ GATEWAY_WEPAY }}"> <input type="hidden" name="gateway_id" value="{{ GATEWAY_WEPAY }}">
{!! Former::close() !!} {!! Former::close() !!}