Skip to content

Commit e119438

Browse files
committed
MAGETWO-58676: [Github] #6296 PayPal Express payments fail as tries to remove stock twice
1 parent bc61cd6 commit e119438

File tree

2 files changed

+159
-70
lines changed

2 files changed

+159
-70
lines changed

app/code/Magento/Paypal/Model/Express/Checkout.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,9 @@ public function getCheckoutShortcutImageUrl()
387387
$pal = null;
388388
} elseif (!$pal) {
389389
$pal = null;
390-
$this->_getApi();
391390
try {
392-
$this->_api->callGetPalDetails();
393-
$pal = $this->_api->getPal();
391+
$this->_getApi()->callGetPalDetails();
392+
$pal = $this->_getApi()->getPal();
394393
$this->_configCacheType->save($pal, $cacheId);
395394
} catch (\Exception $e) {
396395
$this->_configCacheType->save(self::PAL_CACHE_ID, $cacheId);
@@ -503,11 +502,10 @@ public function start($returnUrl, $cancelUrl, $button = null)
503502
$this->_quote->reserveOrderId();
504503
$this->quoteRepository->save($this->_quote);
505504
// prepare API
506-
$this->_getApi();
507505
$solutionType = $this->_config->getMerchantCountry() == 'DE'
508506
? \Magento\Paypal\Model\Config::EC_SOLUTION_TYPE_MARK
509507
: $this->_config->getValue('solutionType');
510-
$this->_api->setAmount($this->_quote->getBaseGrandTotal())
508+
$this->_getApi()->setAmount($this->_quote->getBaseGrandTotal())
511509
->setCurrencyCode($this->_quote->getBaseCurrencyCode())
512510
->setInvNum($this->_quote->getReservedOrderId())
513511
->setReturnUrl($returnUrl)
@@ -516,7 +514,7 @@ public function start($returnUrl, $cancelUrl, $button = null)
516514
->setPaymentAction($this->_config->getValue('paymentAction'));
517515
if ($this->_giropayUrls) {
518516
list($successUrl, $cancelUrl, $pendingUrl) = $this->_giropayUrls;
519-
$this->_api->addData(
517+
$this->_getApi()->addData(
520518
[
521519
'giropay_cancel_url' => $cancelUrl,
522520
'giropay_success_url' => $successUrl,
@@ -526,13 +524,13 @@ public function start($returnUrl, $cancelUrl, $button = null)
526524
}
527525

528526
if ($this->_isBml) {
529-
$this->_api->setFundingSource('BML');
527+
$this->_getApi()->setFundingSource('BML');
530528
}
531529

532530
$this->_setBillingAgreementRequest();
533531

534532
if ($this->_config->getValue('requireBillingAddress') == PaypalConfig::REQUIRE_BILLING_ADDRESS_ALL) {
535-
$this->_api->setRequireBillingAddress(1);
533+
$this->_getApi()->setRequireBillingAddress(1);
536534
}
537535

538536
// suppress or export shipping address
@@ -541,21 +539,18 @@ public function start($returnUrl, $cancelUrl, $button = null)
541539
if ($this->_config->getValue('requireBillingAddress')
542540
== PaypalConfig::REQUIRE_BILLING_ADDRESS_VIRTUAL
543541
) {
544-
$this->_api->setRequireBillingAddress(1);
542+
$this->_getApi()->setRequireBillingAddress(1);
545543
}
546-
$this->_api->setSuppressShipping(true);
544+
$this->_getApi()->setSuppressShipping(true);
547545
} else {
548546

549-
$billingAddress = $this->_quote->getBillingAddress();
550-
if ($billingAddress) {
551-
$this->_api->setBillingAddress($billingAddress);
552-
}
547+
$this->_getApi()->setBillingAddress($this->_quote->getBillingAddress());
553548

554549
$address = $this->_quote->getShippingAddress();
555550
$isOverridden = 0;
556551
if (true === $address->validate()) {
557552
$isOverridden = 1;
558-
$this->_api->setAddress($address);
553+
$this->_getApi()->setAddress($address);
559554
}
560555
$this->_quote->getPayment()->setAdditionalInformation(
561556
self::PAYMENT_INFO_TRANSPORT_SHIPPING_OVERRIDDEN,
@@ -567,19 +562,19 @@ public function start($returnUrl, $cancelUrl, $button = null)
567562
/** @var $cart \Magento\Payment\Model\Cart */
568563
$cart = $this->_cartFactory->create(['salesModel' => $this->_quote]);
569564

570-
$this->_api->setPaypalCart($cart);
565+
$this->_getApi()->setPaypalCart($cart);
571566

572567
if (!$this->_taxData->getConfig()->priceIncludesTax()) {
573568
$this->setShippingOptions($cart, $address);
574569
}
575570

576-
$this->_config->exportExpressCheckoutStyleSettings($this->_api);
571+
$this->_config->exportExpressCheckoutStyleSettings($this->_getApi());
577572

578573
/* Temporary solution. @TODO: do not pass quote into Nvp model */
579-
$this->_api->setQuote($this->_quote);
580-
$this->_api->callSetExpressCheckout();
574+
$this->_getApi()->setQuote($this->_quote);
575+
$this->_getApi()->callSetExpressCheckout();
581576

582-
$token = $this->_api->getToken();
577+
$token = $this->_getApi()->getToken();
583578

584579
$this->_setRedirectUrl($button, $token);
585580

@@ -619,15 +614,15 @@ public function canSkipOrderReviewStep()
619614
*/
620615
public function returnFromPaypal($token)
621616
{
622-
$this->_getApi();
623-
$this->_api->setToken($token)
617+
$this->_getApi()
618+
->setToken($token)
624619
->callGetExpressCheckoutDetails();
625620
$quote = $this->_quote;
626621

627622
$this->ignoreAddressValidation();
628623

629624
// import shipping address
630-
$exportedShippingAddress = $this->_api->getExportedShippingAddress();
625+
$exportedShippingAddress = $this->_getApi()->getExportedShippingAddress();
631626
if (!$quote->getIsVirtual()) {
632627
$shippingAddress = $quote->getShippingAddress();
633628
if ($shippingAddress) {
@@ -646,8 +641,8 @@ public function returnFromPaypal($token)
646641

647642
// import shipping method
648643
$code = '';
649-
if ($this->_api->getShippingRateCode()) {
650-
$code = $this->_matchShippingMethodCode($shippingAddress, $this->_api->getShippingRateCode());
644+
if ($this->_getApi()->getShippingRateCode()) {
645+
$code = $this->_matchShippingMethodCode($shippingAddress, $this->_getApi()->getShippingRateCode());
651646
if ($code) {
652647
// possible bug of double collecting rates :-/
653648
$shippingAddress->setShippingMethod($code)->setCollectShippingRates(true);
@@ -676,7 +671,7 @@ public function returnFromPaypal($token)
676671
} else {
677672
$billingAddress = $quote->getBillingAddress();
678673
}
679-
$exportedBillingAddress = $this->_api->getExportedBillingAddress();
674+
$exportedBillingAddress = $this->_getApi()->getExportedBillingAddress();
680675

681676
$this->_setExportedAddressData($billingAddress, $exportedBillingAddress);
682677
$billingAddress->setCustomerNote($exportedBillingAddress->getData('note'));
@@ -686,8 +681,8 @@ public function returnFromPaypal($token)
686681
// import payment info
687682
$payment = $quote->getPayment();
688683
$payment->setMethod($this->_methodType);
689-
$this->_paypalInfo->importToPayment($this->_api, $payment);
690-
$payment->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_PAYER_ID, $this->_api->getPayerId())
684+
$this->_paypalInfo->importToPayment($this->_getApi(), $payment);
685+
$payment->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_PAYER_ID, $this->_getApi()->getPayerId())
691686
->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_TOKEN, $token);
692687
$quote->collectTotals();
693688
$this->quoteRepository->save($quote);
@@ -731,8 +726,7 @@ public function getShippingOptionsCallbackResponse(array $request)
731726

732727
try {
733728
// obtain addresses
734-
$this->_getApi();
735-
$address = $this->_api->prepareShippingOptionsCallbackAddress($request);
729+
$address = $this->_getApi()->prepareShippingOptionsCallbackAddress($request);
736730
$quoteAddress = $this->_quote->getShippingAddress();
737731

738732
// compare addresses, calculate shipping rates and prepare response
@@ -745,7 +739,7 @@ public function getShippingOptionsCallbackResponse(array $request)
745739
$this->totalsCollector->collectAddressTotals($this->_quote, $quoteAddress);
746740
$options = $this->_prepareShippingOptions($quoteAddress, false, true);
747741
}
748-
$response = $this->_api->setShippingOptions($options)->formatShippingOptionsCallback();
742+
$response = $this->_getApi()->setShippingOptions($options)->formatShippingOptionsCallback();
749743

750744
// log request and response
751745
$debugData['response'] = $response;
@@ -963,7 +957,7 @@ protected function _setBillingAgreementRequest()
963957
if (!$this->_agreementFactory->create()->needToCreateForCustomer($this->_customerId)) {
964958
return $this;
965959
}
966-
$this->_api->setBillingType($this->_api->getBillingAgreementType());
960+
$this->_getApi()->setBillingType($this->_getApi()->getBillingAgreementType());
967961
return $this;
968962
}
969963

@@ -1136,7 +1130,7 @@ public function getCustomerSession()
11361130
private function setShippingOptions(PaypalCart $cart, Address $address = null)
11371131
{
11381132
// for included tax always disable line items (related to paypal amount rounding problem)
1139-
$this->_api->setIsLineItemsEnabled($this->_config->getValue(PaypalConfig::TRANSFER_CART_LINE_ITEMS));
1133+
$this->_getApi()->setIsLineItemsEnabled($this->_config->getValue(PaypalConfig::TRANSFER_CART_LINE_ITEMS));
11401134

11411135
// add shipping options if needed and line items are available
11421136
$cartItems = $cart->getAllItems();
@@ -1147,7 +1141,7 @@ private function setShippingOptions(PaypalCart $cart, Address $address = null)
11471141
if (!$this->_quote->getIsVirtual()) {
11481142
$options = $this->_prepareShippingOptions($address, true);
11491143
if ($options) {
1150-
$this->_api->setShippingOptionsCallbackUrl(
1144+
$this->_getApi()->setShippingOptionsCallbackUrl(
11511145
$this->_coreUrl->getUrl(
11521146
'*/*/shippingOptionsCallback',
11531147
['quote_id' => $this->_quote->getId()]

0 commit comments

Comments
 (0)