Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstantPurchaseOption.php
Go to the documentation of this file.
1 <?php
8 
13 use InvalidArgumentException;
14 
22 {
26  private $paymentToken;
27 
31  private $shippingAddress;
32 
36  private $billingAddress;
37 
41  private $shippingMethod;
42 
56  public function __construct(
57  PaymentTokenInterface $paymentToken = null,
58  Address $shippingAddress = null,
59  Address $billingAddress = null,
60  ShippingMethodInterface $shippingMethod = null
61  ) {
62  $customers = [];
63  if ($paymentToken) {
64  $customers[] = $paymentToken->getCustomerId();
65  }
66  if ($shippingAddress) {
67  $customers[] = $shippingAddress->getCustomerId();
68  }
69  if ($billingAddress) {
70  $customers[] = $billingAddress->getCustomerId();
71  }
72  if (count(array_unique($customers)) > 1) {
73  throw new InvalidArgumentException('Provided data does not belong to same customer.');
74  }
75 
76  $this->paymentToken = $paymentToken;
77  $this->shippingAddress = $shippingAddress;
78  $this->billingAddress = $billingAddress;
79  $this->shippingMethod = $shippingMethod;
80  }
81 
88  public function isAvailable(): bool
89  {
90  return isset(
91  $this->paymentToken,
92  $this->shippingAddress,
93  $this->billingAddress,
94  $this->shippingMethod
95  ) && $this->shippingMethod->getAvailable();
96  }
97 
106  {
107  if (!isset($this->paymentToken)) {
108  throw new LocalizedException(
109  __("A payment method isn't defined for instance purchase. Verify and try again.")
110  );
111  }
112  return $this->paymentToken;
113  }
114 
122  public function getShippingAddress(): Address
123  {
124  if (!isset($this->shippingAddress)) {
125  throw new LocalizedException(__('Shipping address is not defined for instance purchase.'));
126  }
127  return $this->shippingAddress;
128  }
129 
137  public function getBillingAddress(): Address
138  {
139  if (!isset($this->billingAddress)) {
140  throw new LocalizedException(__('Billing address is not defined for instance purchase.'));
141  }
142  return $this->billingAddress;
143  }
144 
153  {
154  if (!isset($this->shippingMethod)) {
155  throw new LocalizedException(__('Shipping method is not defined for instance purchase.'));
156  }
157  return $this->shippingMethod;
158  }
159 }
__()
Definition: __.php:13
__construct(PaymentTokenInterface $paymentToken=null, Address $shippingAddress=null, Address $billingAddress=null, ShippingMethodInterface $shippingMethod=null)