1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Livewire/WepaySignup.php

202 lines
7.0 KiB
PHP
Raw Normal View History

2021-05-05 06:29:58 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-21 07:10:20 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-05-05 06:29:58 +02:00
*/
namespace App\Http\Livewire;
2021-06-11 09:39:51 +02:00
use App\DataMapper\FeesAndLimits;
2021-05-07 07:39:45 +02:00
use App\Factory\CompanyGatewayFactory;
2021-06-11 09:39:51 +02:00
use App\Libraries\MultiDB;
2021-05-05 06:29:58 +02:00
use App\Models\Company;
2021-05-05 12:50:36 +02:00
use App\Models\CompanyGateway;
2021-06-11 09:39:51 +02:00
use App\Models\GatewayType;
2021-05-05 06:29:58 +02:00
use App\Models\User;
2021-05-05 12:50:36 +02:00
use App\PaymentDrivers\WePayPaymentDriver;
2021-05-05 06:29:58 +02:00
use Illuminate\Support\Facades\Hash;
use Livewire\Component;
2021-05-05 12:50:36 +02:00
use WePay;
2021-05-05 06:29:58 +02:00
class WepaySignup extends Component
{
public $user;
public $user_id;
public $company_key;
public $first_name;
public $last_name;
public $email;
2021-05-05 12:50:36 +02:00
public $company_name;
public $country;
public $ach;
public $wepay_payment_tos_agree;
2021-05-06 11:39:24 +02:00
public $debit_cards;
2021-05-05 06:29:58 +02:00
public $terms;
public $privacy_policy;
public $saved;
2021-05-06 06:39:18 +02:00
public $company;
2021-05-05 06:29:58 +02:00
protected $rules = [
2021-05-05 12:50:36 +02:00
'first_name' => ['required'],
'last_name' => ['required'],
2021-05-05 06:29:58 +02:00
'email' => ['required', 'email'],
2021-05-05 12:50:36 +02:00
'company_name' => ['required'],
'country' => ['required'],
'ach' => ['sometimes'],
'wepay_payment_tos_agree' => ['accepted'],
2021-05-07 07:39:45 +02:00
'debit_cards' => ['sometimes'],
2021-05-05 06:29:58 +02:00
];
public function mount()
{
2021-06-11 09:39:51 +02:00
MultiDB::setDb($this->company->db);
2021-05-05 06:29:58 +02:00
$user = User::find($this->user_id);
2021-06-11 09:39:51 +02:00
$this->company = Company::where('company_key', $this->company->company_key)->first();
2021-05-06 06:39:18 +02:00
2021-05-05 06:29:58 +02:00
$this->fill([
2021-05-05 12:50:36 +02:00
'wepay_payment_tos_agree' => '',
'ach' => '',
'country' => 'US',
2021-05-05 06:29:58 +02:00
'user' => $user,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
2021-05-06 06:39:18 +02:00
'company_name' => $this->company->present()->name(),
2021-05-05 06:29:58 +02:00
'saved' => ctrans('texts.confirm'),
'terms' => '<a href="https://go.wepay.com/terms-of-service" target="_blank">'.ctrans('texts.terms_of_service').'</a>',
'privacy_policy' => '<a href="https://go.wepay.com/privacy-policy" target="_blank">'.ctrans('texts.privacy_policy').'</a>',
]);
}
public function render()
{
return render('gateways.wepay.signup.wepay-signup');
}
public function submit()
{
2021-05-08 08:46:14 +02:00
$data = $this->validate($this->rules);
2021-05-06 07:38:39 +02:00
//need to create or get a new WePay CompanyGateway
2021-05-07 10:03:37 +02:00
$cg = CompanyGateway::where('gateway_key', '8fdeed552015b3c7b44ed6c8ebd9e992')
2021-05-06 11:39:24 +02:00
->where('company_id', $this->company->id)
->firstOrNew();
if(!$cg->id) {
2021-06-11 09:39:51 +02:00
$fees_and_limits = new \stdClass;
$fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits;
$fees_and_limits->{GatewayType::BANK_TRANSFER} = new FeesAndLimits;
2021-05-07 07:39:45 +02:00
$cg = CompanyGatewayFactory::create($this->company->id, $this->user->id);
$cg->gateway_key = '8fdeed552015b3c7b44ed6c8ebd9e992';
$cg->require_cvv = false;
$cg->require_billing_address = false;
$cg->require_shipping_address = false;
$cg->update_details = false;
$cg->config = encrypt(config('ninja.testvars.checkout'));
2021-06-11 09:39:51 +02:00
$cg->fees_and_limits = $fees_and_limits;
$cg->token_billing = 'always';
2021-05-07 07:39:45 +02:00
$cg->save();
2021-06-11 09:39:51 +02:00
2021-05-06 11:39:24 +02:00
}
2021-05-05 06:29:58 +02:00
2021-05-05 12:50:36 +02:00
$this->saved = ctrans('texts.processing');
2021-05-07 07:39:45 +02:00
$wepay_driver = new WePayPaymentDriver($cg, null, null);
2021-05-05 12:50:36 +02:00
2021-05-07 09:07:49 +02:00
$wepay = $wepay_driver->init()->wepay;
2021-05-05 12:50:36 +02:00
$user_details = [
'client_id' => config('ninja.wepay.client_id'),
'client_secret' => config('ninja.wepay.client_secret'),
'email' => $data['email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'original_ip' => request()->ip(),
'original_device' => request()->server('HTTP_USER_AGENT'),
'tos_acceptance_time' => time(),
2021-06-13 14:49:43 +02:00
'redirect_uri' => route('wepay.finished'),
2021-05-05 12:50:36 +02:00
'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money',
];
2021-05-07 09:07:49 +02:00
$wepay_user = $wepay->request('user/register/', $user_details);
2021-05-05 12:50:36 +02:00
$access_token = $wepay_user->access_token;
$access_token_expires = $wepay_user->expires_in ? (time() + $wepay_user->expires_in) : null;
$wepay = new WePay($access_token);
$account_details = [
2021-05-06 06:39:18 +02:00
'name' => $data['company_name'],
2021-05-05 12:50:36 +02:00
'description' => ctrans('texts.wepay_account_description'),
2021-05-06 06:39:18 +02:00
'theme_object' => json_decode('{"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":"0b4d78","background_color":"f8f8f8","button_color":"33b753"}'),
2021-05-06 11:39:24 +02:00
'callback_uri' => route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $cg->hashed_id]),
2021-05-07 09:07:49 +02:00
'rbits' => $this->company->rBits(),
2021-05-05 12:50:36 +02:00
'country' => $data['country'],
];
2021-05-06 06:39:18 +02:00
if ($data['country'] == 'CA') {
$account_details['currencies'] = ['CAD'];
$account_details['country_options'] = ['debit_opt_in' => boolval($data['debit_cards'])];
} elseif ($data['country'] == 'GB') {
$account_details['currencies'] = ['GBP'];
2021-05-05 12:50:36 +02:00
}
2021-06-11 09:39:51 +02:00
2021-05-06 06:39:18 +02:00
$wepay_account = $wepay->request('account/create/', $account_details);
2021-05-05 12:50:36 +02:00
try {
$wepay->request('user/send_confirmation/', []);
2021-05-06 06:39:18 +02:00
$confirmation_required = true;
2021-05-05 12:50:36 +02:00
} catch (\WePayException $ex) {
if ($ex->getMessage() == 'This access_token is already approved.') {
2021-05-06 06:39:18 +02:00
$confirmation_required = false;
2021-05-05 12:50:36 +02:00
} else {
2021-06-11 09:39:51 +02:00
request()->session()->flash('message', $ex->getMessage());
2021-05-05 12:50:36 +02:00
}
2021-06-11 09:39:51 +02:00
nlog("failed in try catch ");
nlog($ex->getMessage());
2021-05-05 12:50:36 +02:00
}
2021-05-05 06:29:58 +02:00
2021-05-07 10:03:37 +02:00
$config = [
'userId' => $wepay_user->user_id,
'accessToken' => $access_token,
'tokenType' => $wepay_user->token_type,
'tokenExpires' => $access_token_expires,
'accountId' => $wepay_account->account_id,
'state' => $wepay_account->state,
'testMode' => config('ninja.wepay.environment') == 'staging',
'country' => $data['country'],
];
$cg->setConfig($config);
$cg->save();
2021-06-11 09:39:51 +02:00
if ($confirmation_required) {
request()->session()->flash('message', trans('texts.created_wepay_confirmation_required'));
} else {
$update_uri = $wepay->request('/account/get_update_uri', [
'account_id' => $wepay_account->account_id,
'redirect_uri' => config('ninja.app_url'),
]);
return redirect($update_uri->uri);
}
return redirect()->to('/wepay/finished');
2021-05-05 06:29:58 +02:00
}
2021-05-07 10:03:37 +02:00
2021-05-05 06:29:58 +02:00
}