/
GuestPaymentInformationManagement.php
117 lines (103 loc) · 4.02 KB
/
GuestPaymentInformationManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Model;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPaymentInformationManagementInterface
{
/**
* @var \Magento\Quote\Api\GuestBillingAddressManagementInterface
*/
protected $billingAddressManagement;
/**
* @var \Magento\Quote\Api\GuestPaymentMethodManagementInterface
*/
protected $paymentMethodManagement;
/**
* @var \Magento\Quote\Api\GuestCartManagementInterface
*/
protected $cartManagement;
/**
* @var \Magento\Checkout\Api\PaymentInformationManagementInterface
*/
protected $paymentInformationManagement;
/**
* @var \Magento\Quote\Model\QuoteIdMaskFactory
*/
protected $quoteIdMaskFactory;
/**
* @var CartRepositoryInterface
*/
protected $cartRepository;
/**
* @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
* @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
* @param \Magento\Quote\Api\GuestCartManagementInterface $cartManagement
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
* @param CartRepositoryInterface $cartRepository
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement,
\Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement,
\Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
\Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
CartRepositoryInterface $cartRepository
) {
$this->billingAddressManagement = $billingAddressManagement;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->cartManagement = $cartManagement;
$this->paymentInformationManagement = $paymentInformationManagement;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->cartRepository = $cartRepository;
}
/**
* {@inheritDoc}
*/
public function savePaymentInformationAndPlaceOrder(
$cartId,
$email,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
try {
$orderId = $this->cartManagement->placeOrder($cartId);
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Cannot place order'), $e);
}
return $orderId;
}
/**
* {@inheritDoc}
*/
public function savePaymentInformation(
$cartId,
$email,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
if ($billingAddress) {
$billingAddress->setEmail($email);
$this->billingAddressManagement->assign($cartId, $billingAddress);
} else {
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
$this->cartRepository->getActive($quoteIdMask->getQuoteId())->getBillingAddress()->setEmail($email);
}
$this->paymentMethodManagement->set($cartId, $paymentMethod);
return true;
}
/**
* {@inheritDoc}
*/
public function getPaymentInformation($cartId)
{
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
return $this->paymentInformationManagement->getPaymentInformation($quoteIdMask->getQuoteId());
}
}