1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/PaymentDrivers/WePay/ACH.php

357 lines
14 KiB
PHP
Raw Normal View History

2021-06-11 10:30:39 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2021-06-11 10:30:39 +02:00
*
2021-06-21 07:10:20 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-06-11 10:30:39 +02:00
*/
namespace App\PaymentDrivers\WePay;
2021-06-16 08:41:29 +02:00
use App\Exceptions\PaymentFailed;
2021-10-17 12:40:40 +02:00
use App\Jobs\Util\SystemLogger;
2021-06-16 08:41:29 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
2021-06-20 08:24:37 +02:00
use App\Models\Payment;
2021-10-17 12:40:40 +02:00
use App\Models\SystemLog;
2022-04-28 05:09:17 +02:00
use App\Notifications\Ninja\WePayFailureNotification;
2021-06-20 08:24:37 +02:00
use App\PaymentDrivers\WePay\WePayCommon;
use App\PaymentDrivers\WePayPaymentDriver;
2021-06-16 12:12:04 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
2022-04-28 04:40:07 +02:00
use Illuminate\Notifications\Messages\SlackMessage;
2021-06-20 08:24:37 +02:00
use Illuminate\Support\Str;
2021-06-11 10:30:39 +02:00
class ACH
{
2021-06-16 12:12:04 +02:00
use MakesHash;
2021-06-20 08:24:37 +02:00
use WePayCommon;
2021-06-16 12:12:04 +02:00
2021-06-16 08:41:29 +02:00
public $wepay_payment_driver;
2021-06-11 10:30:39 +02:00
2021-06-16 08:41:29 +02:00
public function __construct(WePayPaymentDriver $wepay_payment_driver)
2021-06-11 10:30:39 +02:00
{
2021-06-16 08:41:29 +02:00
$this->wepay_payment_driver = $wepay_payment_driver;
2021-06-11 10:30:39 +02:00
}
public function authorizeView($data)
{
2021-06-16 08:41:29 +02:00
$data['gateway'] = $this->wepay_payment_driver;
2022-11-08 22:31:40 +01:00
$data['country_code'] = $this->wepay_payment_driver?->client?->country ? $this->wepay_payment_driver->client->country->iso_3166_2 : $this->wepay_payment_driver->company_gateway->company->country()->iso_3166_2;
2021-06-11 10:30:39 +02:00
2021-06-16 08:41:29 +02:00
return render('gateways.wepay.authorize.bank_transfer', $data);
2021-06-11 10:30:39 +02:00
}
2021-06-16 08:41:29 +02:00
public function authorizeResponse($request)
{
//https://developer.wepay.com/api/api-calls/credit_card#authorize
$data = $request->all();
// authorize the credit card
2021-11-04 06:00:49 +01:00
//nlog($data);
2021-06-16 08:41:29 +02:00
/*
'_token' => '1Fk5CRj34up5ntKPvrFyMIAJhDdUNF3boqT3iIN3',
'company_gateway_id' => '39',
'payment_method_id' => '1',
'gateway_response' => NULL,
'is_default' => NULL,
'credit_card_id' => '180642154638',
'q' => '/client/payment_methods',
'method' => '1',
*/
try {
2021-10-03 04:36:43 +02:00
$response = $this->wepay_payment_driver->wepay->request('payment_bank/persist', [
'client_id' => config('ninja.wepay.client_id'),
'client_secret' => config('ninja.wepay.client_secret'),
'payment_bank_id' => (int) $data['bank_account_id'],
2021-10-03 04:36:43 +02:00
]);
} catch (\Exception $e) {
2021-10-17 12:40:40 +02:00
$this->wepay_payment_driver->sendFailureMail($e->getMessage());
$message = [
'server_response' => $e->getMessage(),
];
SystemLogger::dispatch(
$e->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
if (config('ninja.notification.slack')) {
$this->wepay_payment_driver->company_gateway->company->notification(new WePayFailureNotification($this->wepay_payment_driver->company_gateway->company))->ninja();
}
2022-04-28 04:40:07 +02:00
2021-10-03 04:36:43 +02:00
throw new PaymentFailed($e->getMessage(), 400);
}
2021-06-16 08:41:29 +02:00
// display the response
// nlog($response);
if (in_array($response->state, ['new', 'pending', 'authorized'])) {
2021-06-16 08:41:29 +02:00
$this->storePaymentMethod($response, GatewayType::BANK_TRANSFER);
return redirect()->route('client.payment_methods.index');
}
throw new PaymentFailed('There was a problem adding this payment method.', 400);
2021-06-16 08:41:29 +02:00
/*
{
"payment_bank_id": 12345,
"bank_name": "Wells Fargo",
"account_last_four": "6789",
"state": "authorized"
}
state options: new, pending, authorized, disabled.
*/
}
2021-06-16 08:41:29 +02:00
/* If the bank transfer token is PENDING - we need to verify!! */
2021-06-16 08:41:29 +02:00
//
public function verificationView(ClientGatewayToken $token)
{
$this->wepay_payment_driver->init();
$data = [
'token' => $token,
'gateway' => $this->wepay_payment_driver,
];
return render('gateways.wepay.authorize.verify', $data);
}
/**
{
"client_id": 1234,
"client_secret": "b1fc2f68-4d1f-4a",
"payment_bank_id": 12345,
"type": "microdeposits",
"microdeposits": [
8,
12
]
}
*/
public function processVerification(Request $request, ClientGatewayToken $token)
{
2021-06-16 12:12:04 +02:00
$transactions = $request->input('transactions');
2021-06-16 08:41:29 +02:00
2021-06-16 12:12:04 +02:00
$transformed_transactions = [];
foreach ($transactions as $transaction) {
$transformed_transactions[] = (int) $transaction;
}
2021-06-16 12:12:04 +02:00
try {
$response = $this->wepay_payment_driver->wepay->request('payment_bank/verify', [
'client_id' => config('ninja.wepay.client_id'),
'client_secret' => config('ninja.wepay.client_secret'),
'payment_bank_id' => $token->token,
'type' => 'microdeposits',
'microdeposits' => $transformed_transactions,
]);
} catch (\Exception $e) {
nlog('we pay exception');
2021-07-28 05:54:11 +02:00
nlog($e->getMessage());
2021-06-16 12:12:04 +02:00
return redirect()->route('client.payment_methods.verification', ['payment_method' => $token->hashed_id, 'method' => GatewayType::BANK_TRANSFER])
->with('error', $e->getMessage());
}
2021-06-16 08:41:29 +02:00
/*
{
"payment_bank_id": 12345,
"bank_name": "Wells Fargo",
"account_last_four": "6789",
"state": "authorized"
}
*/
nlog($response);
2021-06-16 12:12:04 +02:00
//$meta = $token->meta;
if ($response->state == 'authorized') {
2021-06-16 12:12:04 +02:00
$meta = $token->meta;
$meta->state = $response->state;
2021-06-23 14:41:29 +02:00
$token->meta = $meta;
2021-06-16 08:41:29 +02:00
$token->save();
2021-06-16 12:12:04 +02:00
return redirect()->route('client.payment_methods.index');
} else {
return redirect()->route('client.payment_methods.verification', ['payment_method' => $token->hashed_id, 'method' => GatewayType::BANK_TRANSFER])
->with('error', ctrans('texts.verification_failed'));
2021-06-16 08:41:29 +02:00
}
}
///////////////////////////////////////////////////////////////////////////////////////
2021-06-16 08:41:29 +02:00
public function paymentView(array $data)
{
$data['gateway'] = $this->wepay_payment_driver;
$data['currency'] = $this->wepay_payment_driver->client->getCurrencyCode();
$data['payment_method_id'] = GatewayType::BANK_TRANSFER;
$data['amount'] = $data['total']['amount_with_fee'];
return render('gateways.wepay.bank_transfer', $data);
}
public function paymentResponse($request)
{
2021-06-16 12:12:04 +02:00
$token = ClientGatewayToken::find($this->decodePrimaryKey($request->input('source')));
$token_meta = $token->meta;
if (! property_exists($token_meta, 'state') || $token_meta->state != 'authorized') {
$response = $this->wepay_payment_driver->wepay->request('/payment_bank', [
'client_id' => config('ninja.wepay.client_id'),
'client_secret' => config('ninja.wepay.client_secret'),
'payment_bank_id' => $token->token,
]);
if ($response->state == 'authorized') {
$meta = $token->meta;
$meta->state = $response->state;
$token->meta = $meta;
$token->save();
} else {
return redirect()->route('client.payment_methods.verification', ['payment_method' => $token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
}
}
2021-06-16 12:12:04 +02:00
2021-06-26 13:17:12 +02:00
$app_fee = (config('ninja.wepay.fee_ach_multiplier') * $this->wepay_payment_driver->payment_hash->data->amount_with_fee) + config('ninja.wepay.fee_fixed');
try {
$response = $this->wepay_payment_driver->wepay->request('checkout/create', [
2021-10-03 04:36:43 +02:00
// 'callback_uri' => route('payment_webhook', ['company_key' => $this->wepay_payment_driver->company_gateway->company->company_key, 'company_gateway_id' => $this->wepay_payment_driver->company_gateway->hashed_id]),
'unique_id' => Str::random(40),
'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'),
'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee,
'currency' => $this->wepay_payment_driver->client->getCurrencyCode(),
'short_description' => 'Goods and Services',
'type' => 'goods',
'fee' => [
'fee_payer' => config('ninja.wepay.fee_payer'),
'app_fee' => $app_fee,
],
'payment_method' => [
2021-10-03 04:36:43 +02:00
'type' => 'payment_bank',
'payment_bank' => [
'id' => $token->token,
],
],
]);
} catch (\Exception $e) {
2021-10-03 04:36:43 +02:00
throw new PaymentFailed($e->getMessage(), 500);
}
/* Merge all data and store in the payment hash*/
2021-06-20 08:24:37 +02:00
$state = [
'server_response' => $response,
'payment_hash' => $request->payment_hash,
];
2021-06-16 12:12:04 +02:00
2021-06-20 08:24:37 +02:00
$state = array_merge($state, $request->all());
$this->wepay_payment_driver->payment_hash->data = array_merge((array) $this->wepay_payment_driver->payment_hash->data, $state);
2021-06-20 08:24:37 +02:00
$this->wepay_payment_driver->payment_hash->save();
2021-06-16 12:12:04 +02:00
if (in_array($response->state, ['authorized', 'captured'])) {
2021-06-20 08:24:37 +02:00
//success
nlog('success');
2021-06-20 08:24:37 +02:00
$payment_status = $response->state == 'authorized' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING;
2021-06-16 12:12:04 +02:00
2021-06-20 08:24:37 +02:00
return $this->processSuccessfulPayment($response, $payment_status, GatewayType::BANK_TRANSFER);
}
2021-06-16 12:12:04 +02:00
if (in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])) {
2021-06-20 08:24:37 +02:00
//some type of failure
nlog('failure');
2021-06-16 12:12:04 +02:00
2021-06-20 08:24:37 +02:00
$payment_status = $response->state == 'cancelled' ? Payment::STATUS_CANCELLED : Payment::STATUS_FAILED;
2021-06-16 08:41:29 +02:00
2021-06-20 08:24:37 +02:00
$this->processUnSuccessfulPayment($response, $payment_status);
}
2021-06-16 08:41:29 +02:00
}
private function storePaymentMethod($response, $payment_method_id)
{
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string) '';
$payment_meta->exp_year = (string) '';
$payment_meta->brand = (string) $response->bank_name;
$payment_meta->last4 = (string) $response->account_last_four;
$payment_meta->type = GatewayType::BANK_TRANSFER;
2021-06-16 12:12:04 +02:00
$payment_meta->state = $response->state;
2021-06-16 08:41:29 +02:00
$data = [
'payment_meta' => $payment_meta,
'token' => $response->payment_bank_id,
'payment_method_id' => $payment_method_id,
];
$this->wepay_payment_driver->storeGatewayToken($data);
}
2021-08-30 05:20:29 +02:00
public function tokenBilling($token, $payment_hash)
{
$token_meta = $token->meta;
if (! property_exists($token_meta, 'state') || $token_meta->state != 'authorized') {
2021-08-30 05:20:29 +02:00
return redirect()->route('client.payment_methods.verification', ['payment_method' => $token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
}
2021-08-30 05:20:29 +02:00
2021-08-30 08:00:21 +02:00
$amount = array_sum(array_column($this->wepay_payment_driver->payment_hash->invoices(), 'amount')) + $this->wepay_payment_driver->payment_hash->fee_total;
$app_fee = (config('ninja.wepay.fee_cc_multiplier') * $amount) + config('ninja.wepay.fee_fixed');
2021-08-30 05:20:29 +02:00
$response = $this->wepay_payment_driver->wepay->request('checkout/create', [
2021-08-30 05:20:29 +02:00
'unique_id' => Str::random(40),
'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'),
2021-08-30 08:00:21 +02:00
'amount' => $amount,
2021-08-30 05:20:29 +02:00
'currency' => $this->wepay_payment_driver->client->getCurrencyCode(),
'short_description' => 'Goods and Services',
'type' => 'goods',
'fee' => [
'fee_payer' => config('ninja.wepay.fee_payer'),
'app_fee' => $app_fee,
],
'payment_method' => [
2021-08-30 05:20:29 +02:00
'type' => 'payment_bank',
'payment_bank' => [
'id' => $token->token,
],
],
]);
2021-08-30 05:20:29 +02:00
/* Merge all data and store in the payment hash*/
2021-08-30 05:20:29 +02:00
$state = [
'server_response' => $response,
'payment_hash' => $this->wepay_payment_driver->payment_hash,
];
$this->wepay_payment_driver->payment_hash->data = array_merge((array) $this->wepay_payment_driver->payment_hash->data, $state);
2021-08-30 05:20:29 +02:00
$this->wepay_payment_driver->payment_hash->save();
if (in_array($response->state, ['authorized', 'captured'])) {
2021-08-30 05:20:29 +02:00
//success
nlog('success');
2021-08-30 05:20:29 +02:00
$payment_status = $response->state == 'authorized' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING;
return $this->processSuccessfulPayment($response, $payment_status, GatewayType::BANK_TRANSFER, true);
}
if (in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])) {
2021-08-30 05:20:29 +02:00
//some type of failure
nlog('failure');
2021-08-30 05:20:29 +02:00
$payment_status = $response->state == 'cancelled' ? Payment::STATUS_CANCELLED : Payment::STATUS_FAILED;
$this->processUnSuccessfulPayment($response, $payment_status);
}
}
2021-06-11 10:30:39 +02:00
}