Update shipping address from PayPal

This commit is contained in:
Hillel Coren 2017-11-20 16:32:17 +02:00
parent 7fbe213146
commit ebedbf8809
2 changed files with 38 additions and 4 deletions

View File

@ -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()

View File

@ -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();
}
}