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:
parent
f6942a199f
commit
43fade2339
@ -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"}'));
|
@ -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) {
|
||||
|
@ -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'];
|
||||
|
@ -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'],
|
||||
|
@ -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();
|
||||
@ -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);
|
||||
}
|
||||
|
||||
$accountGateway = $payment->account_gateway;
|
||||
|
||||
@ -1052,11 +1048,19 @@ 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(
|
||||
$details = array(
|
||||
'transactionReference' => $payment->transaction_reference,
|
||||
'amount' => $amount,
|
||||
));
|
||||
);
|
||||
|
||||
if ($amount != ($payment->amount - $payment->refunded)) {
|
||||
$details['amount'] = $amount;
|
||||
}
|
||||
|
||||
if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
|
||||
$details['refund_reason'] = 'Refund issued by merchant.';
|
||||
}
|
||||
|
||||
$refund = $gateway->refund($details);
|
||||
$response = $refund->send();
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
@ -1067,6 +1071,13 @@ class PaymentService extends BaseService
|
||||
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 (!empty($tryVoid)) {
|
||||
if ($amount == $payment->amount) {
|
||||
// This is an unsettled transaction; try to void it
|
||||
$void = $gateway->void(array(
|
||||
@ -1082,46 +1093,12 @@ class PaymentService extends BaseService
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$response->isSuccessful()) {
|
||||
$this->error('Unknown', $response->getMessage(), $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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
<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')
|
||||
->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'=>'<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') !!}
|
||||
<center>
|
||||
{!! Button::primary(trans('texts.sign_up_with_wepay'))
|
||||
@ -54,6 +67,21 @@
|
||||
</center>
|
||||
</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 }}">
|
||||
{!! Former::close() !!}
|
Loading…
Reference in New Issue
Block a user