2021-05-05 06:29:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-05-05 06:29:58 +02:00
|
|
|
*
|
2021-06-21 07:10:20 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-05-05 06:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2023-12-13 17:52:49 +01:00
|
|
|
namespace App\Livewire;
|
2021-05-05 06:29:58 +02:00
|
|
|
|
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 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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $user_id;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $company_key;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $first_name;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $last_name;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $email;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 12:50:36 +02:00
|
|
|
public $company_name;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 12:50:36 +02:00
|
|
|
public $country;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 12:50:36 +02:00
|
|
|
public $ach;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 12:50:36 +02:00
|
|
|
public $wepay_payment_tos_agree;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-06 11:39:24 +02:00
|
|
|
public $debit_cards;
|
2021-05-05 06:29:58 +02:00
|
|
|
|
|
|
|
public $terms;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-05 06:29:58 +02:00
|
|
|
public $privacy_policy;
|
|
|
|
|
|
|
|
public $saved;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-08-07 00:23:13 +02:00
|
|
|
public Company $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);
|
2023-08-07 00:23:13 +02:00
|
|
|
$this->company = Company::query()->where('company_key', $this->company->company_key)->first();
|
2022-06-21 11:57:17 +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()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
return render('gateways.wepay.signup.wepay-signup');
|
2021-05-05 06:29:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function submit()
|
|
|
|
{
|
2021-11-18 04:51:12 +01:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
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
|
2023-08-07 00:23:13 +02:00
|
|
|
$cg = CompanyGateway::query()->where('gateway_key', '8fdeed552015b3c7b44ed6c8ebd9e992')
|
2021-05-06 11:39:24 +02:00
|
|
|
->where('company_id', $this->company->id)
|
|
|
|
->firstOrNew();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $cg->id) {
|
2024-01-14 05:05:00 +01:00
|
|
|
$fees_and_limits = new \stdClass();
|
|
|
|
$fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits();
|
|
|
|
$fees_and_limits->{GatewayType::BANK_TRANSFER} = new FeesAndLimits();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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;
|
2021-06-22 00:01:50 +02:00
|
|
|
$cg->token_billing = 'always';
|
2021-05-07 07:39:45 +02:00
|
|
|
$cg->save();
|
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);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$account_details = [
|
|
|
|
'name' => $data['company_name'],
|
|
|
|
'description' => ctrans('texts.wepay_account_description'),
|
|
|
|
'theme_object' => json_decode('{"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":"0b4d78","background_color":"f8f8f8","button_color":"33b753"}'),
|
|
|
|
'callback_uri' => route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $cg->hashed_id]),
|
|
|
|
'rbits' => $this->company->rBits(),
|
|
|
|
'country' => $data['country'],
|
|
|
|
];
|
2021-05-05 06:29:58 +02:00
|
|
|
|
2022-06-21 11:57:17 +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-06-11 09:39:51 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$wepay_account = $wepay->request('account/create/', $account_details);
|
2023-08-07 06:33:47 +02:00
|
|
|
$confirmation_required = false;
|
2021-06-11 09:39:51 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
|
|
|
$wepay->request('user/send_confirmation/', []);
|
|
|
|
$confirmation_required = true;
|
|
|
|
} catch (\WePayException $ex) {
|
|
|
|
if ($ex->getMessage() == 'This access_token is already approved.') {
|
|
|
|
$confirmation_required = false;
|
|
|
|
} else {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-07 06:33:47 +02:00
|
|
|
/** @phpstan-ignore-next-line */
|
2022-06-21 11:57:17 +02:00
|
|
|
request()->session()->flash('message', $ex->getMessage());
|
2021-06-11 09:39:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('failed in try catch ');
|
|
|
|
nlog($ex->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
if ($confirmation_required) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-07 06:50:08 +02:00
|
|
|
/** @phpstan-ignore-next-line **/
|
2022-06-21 11:57:17 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-06-11 09:39:51 +02:00
|
|
|
|
|
|
|
return redirect()->to('/wepay/finished');
|
2021-05-05 06:29:58 +02:00
|
|
|
}
|
|
|
|
}
|