Move saving methods into BaseDriver

This commit is contained in:
Benjamin Beganović 2020-10-26 14:40:50 +01:00
parent 6e6e73825c
commit 488b8d859d
5 changed files with 59 additions and 47 deletions

View File

@ -13,13 +13,18 @@ namespace App\DataMapper;
class PaymentMethodMeta
{
/** @var string */
public $exp_month;
/** @var string */
public $exp_year;
/** @var string */
public $brand;
/** @var string */
public $last4;
/** @var int */
public $type;
}

View File

@ -247,4 +247,31 @@ class BaseDriver extends AbstractPaymentDriver
}
}
/**
* Store payment method as company gateway token.
*
* @param array $data
* @return null|\App\Models\ClientGatewayToken
*/
public function storeGatewayToken(array $data): ?ClientGatewayToken
{
$company_gateway_token = new ClientGatewayToken();
$company_gateway_token->company_id = $this->client->company->id;
$company_gateway_token->client_id = $this->client->id;
$company_gateway_token->token = $data['token'];
$company_gateway_token->company_gateway_id = $this->company_gateway->id;
$company_gateway_token->gateway_type_id = $data['payment_method_id'];
$company_gateway_token->meta = $data['payment_meta'];
$company_gateway_token->save();
if ($this->client->gateway_tokens->count() == 1) {
$this->client->gateway_tokens()->update(['is_default' => 0]);
$company_gateway_token->is_default = 1;
$company_gateway_token->save();
}
return $company_gateway_token;
}
}

View File

@ -15,6 +15,7 @@ namespace App\PaymentDrivers\CheckoutCom;
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\PaymentType;
use App\Models\SystemLog;
@ -49,7 +50,7 @@ trait Utilities
private function processSuccessfulPayment(\Checkout\Models\Payments\Payment $_payment)
{
if ($this->checkout->payment_hash->data->store_card) {
// $this->saveCreditCard();
$this->storePaymentMethod($_payment);
}
$data = [
@ -148,4 +149,26 @@ trait Utilities
throw new PaymentFailed($error, $e->getCode());
}
private function storePaymentMethod(\Checkout\Models\Payments\Payment $response)
{
try {
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string) $response->source['expiry_month'];
$payment_meta->exp_year = (string) $response->source['expiry_year'];
$payment_meta->brand = (string) $response->source['scheme'];
$payment_meta->last4 = (string) $response->source['last4'];
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $response->id,
'payment_method_id' => $this->checkout->payment_hash->data->payment_method_id,
];
return $this->checokut->saveCard($data);
} catch (\Exception $e) {
session()->flash('message', ctrans('texts.payment_method_saving_failed'));
}
}
}

View File

@ -145,53 +145,9 @@ class CheckoutComPaymentDriver extends BaseDriver
return $this->payment_method->paymentResponse($request);
}
public function saveCard($state)
public function storePaymentMethod(array $data)
{
//some cards just can't be tokenized....
if (!$state['payment_response']->source['id'])
return;
// [id] => src_hck5nsv3fljehbam2cvdm7fioa
// [type] => card
// [expiry_month] => 10
// [expiry_year] => 2022
// [scheme] => Visa
// [last4] => 4242
// [fingerprint] => 688192847DB9AE8A26C53776D036D5B8AD2CEAF1D5A8F5475F542B021041EFA1
// [bin] => 424242
// [card_type] => Credit
// [card_category] => Consumer
// [issuer] => JPMORGAN CHASE BANK NA
// [issuer_country] => US
// [product_id] => A
// [product_type] => Visa Traditional
// [avs_check] => S
// [cvv_check] => Y
// [payouts] => 1
// [fast_funds] => d
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string)$state['payment_response']->source['expiry_month'];
$payment_meta->exp_year = (string)$state['payment_response']->source['expiry_year'];
$payment_meta->brand = (string)$state['payment_response']->source['scheme'];
$payment_meta->last4 = (string)$state['payment_response']->source['last4'];
$payment_meta->type = $this->payment_method;
$company_gateway_token = new ClientGatewayToken();
$company_gateway_token->company_id = $this->client->company->id;
$company_gateway_token->client_id = $this->client->id;
$company_gateway_token->token = $state['payment_response']->source['id'];
$company_gateway_token->company_gateway_id = $this->company_gateway->id;
$company_gateway_token->gateway_type_id = $state['payment_method_id'];
$company_gateway_token->meta = $payment_meta;
$company_gateway_token->save();
if ($this->client->gateway_tokens->count() == 1) {
$this->client->gateway_tokens()->update(['is_default' => 0]);
$company_gateway_token->is_default = 1;
$company_gateway_token->save();
}
return $this->storeGatewayToken($data);
}
public function refund(Payment $payment, $amount, $return_client_response = false)

View File

@ -3286,4 +3286,5 @@ return [
'credit_subject' => 'New credit :number from :account',
'credit_message' => 'To view your credit for :amount, click the link below.',
'payment_method_saving_failed' => 'Payment method can\'t be saved for future use.',
];