| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- /**
- * This file is part of the Klarna KP module
- *
- * (c) Klarna Bank AB (publ)
- *
- * For the full copyright and license information, please view the NOTICE
- * and LICENSE files that were distributed with this source code.
- */
- namespace Klarna\Kp\Model\Api;
- use Klarna\Kp\Api\Data\AddressInterface;
- use Klarna\Kp\Api\Data\AttachmentInterface;
- use Klarna\Kp\Api\Data\CustomerInterface;
- use Klarna\Kp\Api\Data\OptionsInterface;
- use Klarna\Kp\Api\Data\OrderlineInterface;
- use Klarna\Kp\Api\Data\RequestInterface;
- use Klarna\Kp\Api\Data\UrlsInterface;
- /**
- * Class Request
- *
- * @package Klarna\Kp\Model\Api
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class Request implements RequestInterface
- {
- /**
- * @var string
- */
- private $merchant_reference1;
- /**
- * @var string
- */
- private $merchant_reference2;
- /**
- * @var OptionsInterface
- */
- private $options;
- /**
- * @var CustomerInterface
- */
- private $customer;
- /**
- * @var UrlsInterface
- */
- private $urls;
- /**
- * @var string
- */
- private $purchase_country;
- /**
- * @var string
- */
- private $purchase_currency;
- /**
- * @var string
- */
- private $locale;
- /**
- * @var AddressInterface
- */
- private $billing_address;
- /**
- * @var AddressInterface
- */
- private $shipping_address;
- /**
- * @var int
- */
- private $order_tax_amount;
- /**
- * @var OrderlineInterface[]
- */
- private $order_lines;
- /**
- * @var AttachmentInterface
- */
- private $attachment;
- /**
- * @var int
- */
- private $order_amount;
- /**
- * @var string
- */
- private $design;
- /**
- * @var array
- */
- private $custom_payment_methods;
- /**
- * Request constructor.
- *
- * @param array $data
- */
- public function __construct($data = [])
- {
- foreach ($data as $key => $value) {
- if (property_exists($this, $key)) {
- $this->$key = $value;
- }
- }
- }
- /**
- * Used for storing merchant's internal order number or other reference (max 255 characters).
- *
- * @param string $reference
- */
- public function setMerchantReference1($reference)
- {
- $this->merchant_reference1 = $reference;
- }
- /**
- * Used for storing merchant's internal order number or other reference (max 255 characters).
- *
- * @param string $reference
- */
- public function setMerchantReference2($reference)
- {
- $this->merchant_reference2 = $reference;
- }
- /**
- * @param OptionsInterface $options
- */
- public function setOptions(OptionsInterface $options)
- {
- $this->options = $options;
- }
- /**
- * Information about the liable customer of the order.
- *
- * @param CustomerInterface $customer
- */
- public function setCustomer(CustomerInterface $customer)
- {
- $this->customer = $customer;
- }
- /**
- * @param UrlsInterface $urls
- */
- public function setMerchantUrls(UrlsInterface $urls)
- {
- $this->urls = $urls;
- }
- /**
- * ISO 3166 alpha-2 => Purchase country
- *
- * @param string $purchaseCountry
- */
- public function setPurchaseCountry($purchaseCountry)
- {
- $this->purchase_country = $purchaseCountry;
- }
- /**
- * ISO 4217 => Purchase currency.
- *
- * @param string $purchaseCurrency
- */
- public function setPurchaseCurrency($purchaseCurrency)
- {
- $this->purchase_currency = $purchaseCurrency;
- }
- /**
- * RFC 1766 => Customer's locale.
- *
- * @param string $locale
- */
- public function setLocale($locale)
- {
- $this->locale = $locale;
- }
- /**
- * The billing address.
- *
- * @param AddressInterface $billingAddress
- */
- public function setBillingAddress(AddressInterface $billingAddress)
- {
- $this->billing_address = $billingAddress;
- }
- /**
- * The shipping address.
- *
- * @param AddressInterface $shippingAddress
- */
- public function setShippingAddress(AddressInterface $shippingAddress)
- {
- $this->shipping_address = $shippingAddress;
- }
- /**
- * The total tax amount of the order. Implicit decimal (eg 1000 instead of 10.00)
- *
- * @param int $orderTaxAmount
- */
- public function setOrderTaxAmount($orderTaxAmount)
- {
- $this->order_tax_amount = $orderTaxAmount;
- }
- /**
- * The applicable order lines.
- *
- * @param array|OrderlineInterface[] $orderlines
- */
- public function setOrderLines(array $orderlines)
- {
- $this->order_lines = $orderlines;
- }
- /**
- * Add an orderline to the request.
- *
- * @param OrderlineInterface $orderline
- */
- public function addOrderLine(OrderlineInterface $orderline)
- {
- $this->order_lines[] = $orderline;
- }
- /**
- * Container for optional merchant specific data.
- *
- * @param AttachmentInterface $attachment
- */
- public function setAttachment(AttachmentInterface $attachment)
- {
- $this->attachment = $attachment;
- }
- /**
- * Total amount of the order, including tax and any discounts. Implicit decimal (eg 1000 instead of 10.00)
- *
- * @param int $orderAmount
- */
- public function setOrderAmount($orderAmount)
- {
- $this->order_amount = $orderAmount;
- }
- /**
- * Used to load a specific design for the credit form
- *
- * @param string $design
- */
- public function setDesign($design)
- {
- $this->design = $design;
- }
- /**
- * An optional list of merchant triggered payment methods
- *
- * @param array $paymentMethods
- */
- public function setCustomPaymentMethods(array $paymentMethods)
- {
- $this->custom_payment_methods = $paymentMethods;
- }
- /**
- * Add a merchant triggered payment method to request
- *
- * @param string $paymentMethod
- */
- public function addCustomPaymentMethods($paymentMethod)
- {
- $this->custom_payment_methods[] = $paymentMethod;
- }
- /**
- * Generate array object needed for API call
- *
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function toArray()
- {
- $request = [
- 'purchase_country' => $this->purchase_country,
- 'purchase_currency' => $this->purchase_currency,
- 'locale' => $this->locale,
- 'order_amount' => $this->order_amount,
- 'order_tax_amount' => $this->order_tax_amount,
- 'order_lines' => null,
- 'billing_address' => null,
- 'shipping_address' => null,
- 'customer' => null,
- 'merchant_urls' => null,
- 'merchant_reference1' => $this->merchant_reference1,
- 'merchant_reference2' => $this->merchant_reference2,
- 'design' => $this->design,
- 'options' => null,
- 'custom_payment_methods' => $this->custom_payment_methods,
- 'attachment' => null
- ];
- if (null !== $this->billing_address) {
- $request['billing_address'] = $this->billing_address->toArray();
- }
- if (null !== $this->shipping_address) {
- $request['shipping_address'] = $this->shipping_address->toArray();
- }
- if (null !== $this->customer) {
- $request['customer'] = $this->customer->toArray();
- }
- if (null !== $this->urls) {
- $request['merchant_urls'] = $this->urls->toArray();
- }
- if (null !== $this->attachment) {
- $request['attachment'] = $this->attachment->toArray();
- }
- if (null !== $this->order_lines) {
- $request['order_lines'] = [];
- foreach ($this->order_lines as $line) {
- $request['order_lines'][] = $line->toArray();
- }
- }
- if (null !== $this->options) {
- $request['options'] = $this->options->toArray();
- }
- return array_filter($request, function ($value) {
- if ($value === null) {
- return false;
- }
- if (is_array($value) && count($value) === 0) {
- return false;
- }
- return true;
- });
- }
- }
|