From ebedbf8809a2a8f5d85c94ada2bf0d3f005fbb19 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 20 Nov 2017 16:32:17 +0200 Subject: [PATCH] Update shipping address from PayPal --- .../PaymentDrivers/BasePaymentDriver.php | 17 ++++++++++--- .../PayPalExpressPaymentDriver.php | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 71c704b0bcca..ae1eecf5820c 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -871,23 +871,32 @@ class BasePaymentDriver return $payment; } + protected function updateClientFromOffsite($transRef, $paymentRef) + { + // do nothing + } + public function completeOffsitePurchase($input) { $this->input = $input; - $ref = array_get($this->input, 'token') ?: $this->invitation->transaction_reference; + $transRef = array_get($this->input, 'token') ?: $this->invitation->transaction_reference; if (method_exists($this->gateway(), 'completePurchase')) { $details = $this->paymentDetails(); $response = $this->gateway()->completePurchase($details)->send(); - $ref = $response->getTransactionReference() ?: $ref; + $paymentRef = $response->getTransactionReference() ?: $transRef; if ($response->isCancelled()) { return false; } elseif (! $response->isSuccessful()) { throw new Exception($response->getMessage()); } + } else { + $paymentRef = $transRef; } + $this->updateClientFromOffsite($transRef, $paymentRef); + // check invoice still has balance if (! floatval($this->invoice()->balance)) { throw new Exception(trans('texts.payment_error_code', ['code' => 'NB'])); @@ -895,12 +904,12 @@ class BasePaymentDriver // check this isn't a duplicate transaction reference if (Payment::whereAccountId($this->invitation->account_id) - ->whereTransactionReference($ref) + ->whereTransactionReference($paymentRef) ->first()) { throw new Exception(trans('texts.payment_error_code', ['code' => 'DT'])); } - return $this->createPayment($ref); + return $this->createPayment($paymentRef); } public function tokenLinks() diff --git a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php index ffec414c59b7..008d0cf971be 100644 --- a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -41,4 +41,29 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver } } + protected function updateClientFromOffsite($transRef, $paymentRef) + { + $response = $this->gateway()->fetchCheckout([ + 'token' => $transRef + ])->send(); + + $data = $response->getData(); + $client = $this->client(); + + $client->shipping_address1 = trim($data['SHIPTOSTREET']); + $client->shipping_address2 = ''; + $client->shipping_city = trim($data['SHIPTOCITY']); + $client->shipping_state = trim($data['SHIPTOSTATE']); + $client->shipping_postal_code = trim($data['SHIPTOZIP']); + + if ($country = cache('countries')->filter(function ($item) use ($data) { + return strtolower($item->iso_3166_2) == strtolower(trim($data['SHIPTOCOUNTRYCODE'])); + })->first()) { + $client->shipping_country_id = $country->id; + } else { + $client->shipping_country_id = null; + } + + $client->save(); + } }