1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Livewire/WepaySignup.php

149 lines
4.9 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)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Livewire;
use App\Models\Company;
2021-05-05 12:50:36 +02:00
use App\Models\CompanyGateway;
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-05 06:29:58 +02:00
];
public function mount()
{
$user = User::find($this->user_id);
2021-05-06 06:39:18 +02:00
$this->company = Company::where('company_key', $this->company_key)->firstOrFail();
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-06 07:38:39 +02:00
//need to create or get a new WePay CompanyGateway
2021-05-06 11:39:24 +02:00
$cg = CompanyGateway::where('id', 49)
->where('company_id', $this->company->id)
->firstOrNew();
if(!$cg->id) {
}
2021-05-05 06:29:58 +02:00
$data = $this->validate($this->rules);
2021-05-05 12:50:36 +02:00
$this->saved = ctrans('texts.processing');
$wepay_driver = new WePayPaymentDriver(new CompanyGateway, null, null);
$wepay_driver->init();
$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(),
'redirect_uri' => route('wepay.process_signup'),
'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money',
];
$wepay_user = $wepay_driver->request('user/register/', $user_details);
$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-06 06:39:18 +02:00
'rbits' => $this->company->present()->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-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 {
throw $ex;
}
}
2021-05-05 06:29:58 +02:00
}
}