216 lines
4.2 KiB
PHP
216 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Imoje\Payment;
|
|
|
|
/**
|
|
* Class CartData
|
|
*
|
|
* @package Imoje\Payment
|
|
*/
|
|
class CartData
|
|
{
|
|
|
|
const TYPE_SCHEMA_ITEMS = 'cartDataItems';
|
|
const TYPE_SCHEMA_ADDRESS = 'cartDataAddress';
|
|
const TYPE_SCHEMA_DISCOUNT = 'cartDataDiscount';
|
|
const TYPE_SCHEMA_SHIPPING = 'cartDataShipping';
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $items;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $addressBilling;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $createdAt;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $amount;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $addressDelivery;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $shipping = [];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $discount = [];
|
|
|
|
/**
|
|
* @param $id
|
|
* @param $vat
|
|
* @param $name
|
|
* @param $amount
|
|
* @param $quantity
|
|
*
|
|
* @return void
|
|
*/
|
|
public function addItem($id, $vat, $name, $amount, $quantity)
|
|
{
|
|
|
|
$this->items[] = [
|
|
'id' => $id,
|
|
'vat' => $vat,
|
|
'name' => $name,
|
|
'amount' => $amount,
|
|
'quantity' => $quantity,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param int $vat
|
|
* @param string $name
|
|
* @param int $amount
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setDiscount($vat, $name, $amount)
|
|
{
|
|
|
|
$this->discount = [
|
|
'vat' => $vat,
|
|
'name' => $name,
|
|
'amount' => $amount,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param int $vat
|
|
* @param string $name
|
|
* @param int $amount
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setShipping($vat, $name, $amount)
|
|
{
|
|
|
|
$this->shipping = [
|
|
'vat' => $vat,
|
|
'name' => $name,
|
|
'amount' => $amount,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param int $amount
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setAmount($amount)
|
|
{
|
|
|
|
$this->amount = $amount;
|
|
}
|
|
|
|
/**
|
|
* @param int $createdAt
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setCreatedAt($createdAt)
|
|
{
|
|
|
|
$this->createdAt = $createdAt;
|
|
}
|
|
|
|
/**
|
|
* @param string $city
|
|
* @param string $name
|
|
* @param string $phone
|
|
* @param string $street
|
|
* @param string $country
|
|
* @param string $postalCode
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setAddressBilling($city, $name, $phone, $street, $country, $postalCode)
|
|
{
|
|
$this->addressBilling = [
|
|
'city' => $city,
|
|
'name' => $name,
|
|
'phone' => (string) $phone,
|
|
'street' => $street,
|
|
'country' => $country,
|
|
'postalCode' => (string) $postalCode,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param string $city
|
|
* @param string $name
|
|
* @param string $phone
|
|
* @param string $street
|
|
* @param string $country
|
|
* @param string $postalCode
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setAddressDelivery($city, $name, $phone, $street, $country, $postalCode)
|
|
{
|
|
$this->addressDelivery = [
|
|
'city' => $city,
|
|
'name' => $name,
|
|
'phone' => (string) $phone,
|
|
'street' => $street,
|
|
'country' => $country,
|
|
'postalCode' => (string) $postalCode,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function prepareCartData()
|
|
{
|
|
|
|
return base64_encode(gzencode(json_encode($this->prepareCartDataArray()), 5));
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function prepareCartDataArray()
|
|
{
|
|
|
|
$data = [
|
|
'address' => [
|
|
'billing' => $this->addressBilling,
|
|
'delivery' => $this->addressDelivery,
|
|
],
|
|
'items' => $this->items,
|
|
];
|
|
|
|
if (!empty($this->discount)) {
|
|
$data['discount'] = $this->discount;
|
|
}
|
|
|
|
if (!empty($this->shipping)) {
|
|
$data['shipping'] = $this->shipping;
|
|
}
|
|
|
|
if (!empty($this->createdAt)) {
|
|
$data['createdAt'] = $this->createdAt;
|
|
}
|
|
|
|
if (!empty($this->amount)) {
|
|
$data['amount'] = $this->amount;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|