diff --git a/app/DataMapper/PaymentMethodMeta.php b/app/DataMapper/PaymentMethodMeta.php index 607f1c339d53..b97eca6bb860 100644 --- a/app/DataMapper/PaymentMethodMeta.php +++ b/app/DataMapper/PaymentMethodMeta.php @@ -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; } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 6ecdc15ac08b..6e7006918e4f 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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; + } } diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index 8b7ce1135918..7d554e1bd6c3 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -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')); + } + } } diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 9209d5731534..1312ad8693ad 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -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) diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index c5ee6460babd..3768e6b82ea1 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -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.', ];