1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Saving & tokenizing the credit card

This commit is contained in:
Benjamin Beganović 2021-04-29 15:05:45 +02:00
parent 6cab52fc9f
commit 77733ffd0a
2 changed files with 42 additions and 13 deletions

View File

@ -84,9 +84,19 @@ class CreditCard
$this->braintree->payment_hash->data = array_merge((array)$this->braintree->payment_hash->data, $state);
$this->braintree->payment_hash->save();
$customer = $this->braintree->findOrCreateCustomer();
$payment_method = $this->braintree->gateway->paymentMethod()->create([
'customerId' => $customer->id,
'paymentMethodNonce' => $state['token'],
'options' => [
'verifyCard' => true,
],
]);
$result = $this->braintree->gateway->transaction()->sale([
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
'paymentMethodNonce' => $state['token'],
'paymentMethodToken' => $payment_method->paymentMethod->token,
'deviceData' => $state['client-data'],
'options' => [
'submitForSettlement' => true
@ -97,7 +107,7 @@ class CreditCard
$this->braintree->logSuccessfulGatewayResponse(['response' => $request->server_response, 'data' => $this->braintree->payment_hash], SystemLog::TYPE_BRAINTREE);
if ($request->store_card) {
$this->storePaymentMethod();
$this->storePaymentMethod($payment_method, $customer->id);
}
return $this->processSuccessfulPayment($result);
@ -160,27 +170,23 @@ class CreditCard
throw new PaymentFailed($response->transaction->additionalProcessorResponse, $response->transaction->processorResponseCode);
}
private function storePaymentMethod()
private function storePaymentMethod($method, $customer_reference)
{
return;
$method = $this->braintree->payment_hash->data->server_response->details;
try {
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string) $method->expirationMonth;
$payment_meta->exp_year = (string) $method->expirationYear;
$payment_meta->brand = (string) $method->cardType;
$payment_meta->last4 = (string) $method->lastFour;
$payment_meta->exp_month = (string)$method->paymentMethod->expirationMonth;
$payment_meta->exp_year = (string)$method->paymentMethod->expirationYear;
$payment_meta->brand = (string)$method->paymentMethod->cardType;
$payment_meta->last4 = (string)$method->paymentMethod->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $method->id,
'token' => $method->paymentMethod->token,
'payment_method_id' => $this->braintree->payment_hash->data->payment_method_id,
];
$this->braintree->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]);
$this->braintree->storeGatewayToken($data, ['gateway_customer_reference' => $customer_reference]);
} catch (\Exception $e) {
return $this->braintree->processInternallyFailedPayment($this->braintree, $e);
}

View File

@ -14,6 +14,7 @@ namespace App\PaymentDrivers;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\SystemLog;
use App\PaymentDrivers\Braintree\CreditCard;
@ -85,4 +86,26 @@ class BraintreePaymentDriver extends BaseDriver
{
return $this->payment_method->paymentResponse($request);
}
public function findOrCreateCustomer()
{
$existing = ClientGatewayToken::query()
->where('company_gateway_id', $this->company_gateway->id)
->where('client_id', $this->client->id)
->first();
if ($existing) {
return $this->gateway->customer()->find($existing->gateway_customer_reference);
}
$result = $this->gateway->customer()->create([
'firstName' => $this->client->present()->name,
'email' => $this->client->present()->email,
'phone' => $this->client->present()->phone,
]);
if ($result->success) {
return $result->customer;
}
}
}