Refactor common code WePay

This commit is contained in:
David Bomba 2021-06-20 16:24:37 +10:00
parent 2075c4e8c1
commit 4eda6b1190
3 changed files with 121 additions and 142 deletions

View File

@ -15,13 +15,17 @@ namespace App\PaymentDrivers\WePay;
use App\Exceptions\PaymentFailed;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\PaymentDrivers\WePayPaymentDriver;
use App\PaymentDrivers\WePay\WePayCommon;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ACH
{
use MakesHash;
use WePayCommon;
public $wepay_payment_driver;
@ -193,102 +197,50 @@ class ACH
if($token_meta->state != "authorized")
return redirect()->route('client.payment_methods.verification', ['payment_method' => $token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
$response = $this->wepay_payment_driver->wepay->request('checkout/create', array(
'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' => 'A vacation home rental',
'type' => 'goods',
'payment_method' => array(
'type' => 'payment_bank',
'payment_bank' => array(
'id' => $token->token
)
)
));
/* Merge all data and store in the payment hash*/
$state = [
'server_response' => $response,
'payment_hash' => $request->payment_hash,
];
$state = array_merge($state, $request->all());
$this->wepay_payment_driver->payment_hash->data = array_merge((array) $this->wepay_payment_driver->payment_hash->data, $state);
$this->wepay_payment_driver->payment_hash->save();
if(in_array($response->state, ['authorized', 'captured'])){
//success
nlog("success");
$payment_status = $response->state == 'authorized' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING;
return $this->processSuccessfulPayment($response, $payment_status, GatewayType::BANK_TRANSFER);
}
// response = wepay.call('/checkout/create', {
// 'account_id': account_id,
// 'amount': '25.50',
// 'short_description': 'A vacation home rental',
// 'type': 'GOODS',
// 'payment_method': {
// 'type': 'payment_bank',
// 'payment_bank': {
// 'id': 20000061
// }
// }
// })
if(in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])){
//some type of failure
nlog("failure");
$payment_status = $response->state == 'cancelled' ? Payment::STATUS_CANCELLED : Payment::STATUS_FAILED;
$this->processUnSuccessfulPayment($response, $payment_status);
}
// $this->stripe->init();
// $source = ClientGatewayToken::query()
// ->where('id', $this->decodePrimaryKey($request->source))
// ->where('company_id', auth('contact')->user()->client->company->id)
// ->first();
// if (!$source) {
// throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
// }
// $state = [
// 'payment_method' => $request->payment_method_id,
// 'gateway_type_id' => $request->company_gateway_id,
// 'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision),
// 'currency' => $request->currency,
// 'customer' => $request->customer,
// ];
// $state = array_merge($state, $request->all());
// $state['source'] = $source->token;
// $this->stripe->payment_hash->data = array_merge((array)$this->stripe->payment_hash->data, $state);
// $this->stripe->payment_hash->save();
// try {
// $state['charge'] = \Stripe\Charge::create([
// 'amount' => $state['amount'],
// 'currency' => $state['currency'],
// 'customer' => $state['customer'],
// 'source' => $state['source'],
// ], $this->stripe->stripe_connect_auth);
// $state = array_merge($state, $request->all());
// $this->stripe->payment_hash->data = array_merge((array)$this->stripe->payment_hash->data, $state);
// $this->stripe->payment_hash->save();
// if ($state['charge']->status === 'pending' && is_null($state['charge']->failure_message)) {
// return $this->processPendingPayment($state);
// }
// return $this->processUnsuccessfulPayment($state);
// } catch (Exception $e) {
// if ($e instanceof CardException) {
// return redirect()->route('client.payment_methods.verification', ['id' => ClientGatewayToken::first()->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
// }
// throw new PaymentFailed($e->getMessage(), $e->getCode());
// }
}
private function storePaymentMethod($response, $payment_method_id)
{

View File

@ -21,9 +21,13 @@ use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\WePayPaymentDriver;
use App\PaymentDrivers\WePay\WePayCommon;
use Illuminate\Support\Str;
class CreditCard
{
use WePayCommon;
public $wepay_payment_driver;
public function __construct(WePayPaymentDriver $wepay_payment_driver)
@ -98,6 +102,7 @@ class CreditCard
public function paymentView(array $data)
{
$data['gateway'] = $this->wepay_payment_driver;
$data['description'] = ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number');
return render('gateways.wepay.credit_card.pay', $data);
}
@ -105,7 +110,6 @@ class CreditCard
public function paymentResponse(PaymentResponseRequest $request)
{
nlog("payment response");
$this->wepay_payment_driver->init();
//it could be an existing token or a new credit_card_id that needs to be converted into a wepay token
if($request->has('credit_card_id') && $request->input('credit_card_id'))
@ -120,9 +124,6 @@ class CreditCard
$credit_card_id = (int)$response->credit_card_id;
nlog($response->state);
nlog(boolval($request->input('store_card')));
if(in_array($response->state, ['new', 'authorized']) && boolval($request->input('store_card'))){
$this->storePaymentMethod($response, GatewayType::CREDIT_CARD);
@ -140,6 +141,7 @@ nlog(boolval($request->input('store_card')));
nlog($request->all());
// charge the credit card
$response = $this->wepay_payment_driver->wepay->request('checkout/create', array(
'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(),
@ -169,7 +171,7 @@ nlog(boolval($request->input('store_card')));
nlog("success");
$payment_status = $response->state == 'authorized' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING;
return $this->processSuccessfulPayment($response, $payment_status);
return $this->processSuccessfulPayment($response, $payment_status, GatewayType::CREDIT_CARD);
}
if(in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])){
@ -250,57 +252,6 @@ https://developer.wepay.com/api/api-calls/checkout
}
*/
private function processSuccessfulPayment($response, $payment_status)
{
$data = [
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
'amount' => $response->amount,
'transaction_reference' => $response->checkout_id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
$payment = $this->wepay_payment_driver->createPayment($data, $payment_status);
SystemLogger::dispatch(
['response' => $this->wepay_payment_driver->payment_hash->data->server_response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->wepay_payment_driver->encodePrimaryKey($payment->id)]);
}
private function processUnSuccessfulPayment($response, $payment_status)
{
PaymentFailureMailer::dispatch($this->wepay_payment_driver->client, $response->state, $this->wepay_payment_driver->client->company, $response->gross);
PaymentFailureMailer::dispatch(
$this->wepay_payment_driver->client,
$response,
$this->wepay_payment_driver->client->company,
$response->gross
);
$message = [
'server_response' => $response,
'data' => $this->wepay_payment_driver->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
private function storePaymentMethod($response, $payment_method_id)
{

View File

@ -0,0 +1,76 @@
<?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\PaymentDrivers\WePay;
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\PaymentType;
use App\Models\SystemLog;
trait WePayCommon
{
private function processSuccessfulPayment($response, $payment_status, $gateway_type)
{
$data = [
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
'amount' => $response->amount,
'transaction_reference' => $response->checkout_id,
'gateway_type_id' => $gateway_type,
];
$payment = $this->wepay_payment_driver->createPayment($data, $payment_status);
SystemLogger::dispatch(
['response' => $this->wepay_payment_driver->payment_hash->data->server_response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->wepay_payment_driver->encodePrimaryKey($payment->id)]);
}
private function processUnSuccessfulPayment($response, $payment_status)
{
PaymentFailureMailer::dispatch($this->wepay_payment_driver->client, $response->state, $this->wepay_payment_driver->client->company, $response->amount);
PaymentFailureMailer::dispatch(
$this->wepay_payment_driver->client,
$response,
$this->wepay_payment_driver->client->company,
$response->gross
);
$message = [
'server_response' => $response,
'data' => $this->wepay_payment_driver->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
}