| 1 |
<?php |
| 2 |
|
| 3 |
namespace Imoje\Payment; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class CartData |
| 7 |
* |
| 8 |
* @package Imoje\Payment |
| 9 |
*/ |
| 10 |
class Invoice |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* @const string |
| 15 |
*/ |
| 16 |
const TAX_23 = 'TAX_23'; |
| 17 |
|
| 18 |
/** |
| 19 |
* @const string |
| 20 |
*/ |
| 21 |
const TAX_22 = 'TAX_22'; |
| 22 |
|
| 23 |
/** |
| 24 |
* @const string |
| 25 |
*/ |
| 26 |
const TAX_8 = 'TAX_8'; |
| 27 |
|
| 28 |
/** |
| 29 |
* @const string |
| 30 |
*/ |
| 31 |
const TAX_7 = 'TAX_7'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @const string |
| 35 |
*/ |
| 36 |
const TAX_5 = 'TAX_5'; |
| 37 |
|
| 38 |
/** |
| 39 |
* @const string |
| 40 |
*/ |
| 41 |
const TAX_3 = 'TAX_3'; |
| 42 |
|
| 43 |
/** |
| 44 |
* @const string |
| 45 |
*/ |
| 46 |
const TAX_0 = 'TAX_0'; |
| 47 |
|
| 48 |
/** |
| 49 |
* @const string |
| 50 |
*/ |
| 51 |
const TAX_EXEMPT = 'TAX_EXEMPT'; |
| 52 |
|
| 53 |
/** |
| 54 |
* @const string |
| 55 |
*/ |
| 56 |
const TAX_NOT_LIABLE = 'TAX_NOT_LIABLE'; |
| 57 |
|
| 58 |
/** |
| 59 |
* @const string |
| 60 |
*/ |
| 61 |
const TAX_REVERSE_CHARGE = 'TAX_REVERSE_CHARGE'; |
| 62 |
|
| 63 |
/** |
| 64 |
* @const string |
| 65 |
*/ |
| 66 |
const TAX_NOT_EXLUCDING = 'TAX_NOT_EXCLUDING'; |
| 67 |
|
| 68 |
/** |
| 69 |
* @const string |
| 70 |
*/ |
| 71 |
const BUYER_PERSON = 'PERSON'; |
| 72 |
/** |
| 73 |
* @const string |
| 74 |
*/ |
| 75 |
const BUYER_COMPANY = 'COMPANY'; |
| 76 |
|
| 77 |
/** |
| 78 |
* @const string |
| 79 |
*/ |
| 80 |
const ID_TYPE_VAT = 'VAT_ID'; |
| 81 |
|
| 82 |
/** |
| 83 |
* @const string |
| 84 |
*/ |
| 85 |
const VAT_COUNTRY_ALPHA2 = 'PL'; |
| 86 |
|
| 87 |
/** |
| 88 |
* @var array |
| 89 |
*/ |
| 90 |
protected $buyer; |
| 91 |
|
| 92 |
/** |
| 93 |
* @var array |
| 94 |
*/ |
| 95 |
protected $positions; |
| 96 |
|
| 97 |
/** |
| 98 |
* @param string $name |
| 99 |
* @param int $code |
| 100 |
* @param int $quantity |
| 101 |
* @param string $taxStake |
| 102 |
* @param string $grossAmount |
| 103 |
* @param int $discountAmount |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function addItem($name, $code, $quantity, $taxStake, $grossAmount, $discountAmount = 0) |
| 108 |
{ |
| 109 |
|
| 110 |
$position = [ |
| 111 |
'name' => $name, |
| 112 |
'code' => (string) $code, |
| 113 |
'quantity' => (float) $quantity, |
| 114 |
'unit' => 'Sztuki', |
| 115 |
'taxStake' => $taxStake, |
| 116 |
'grossAmount' => (int) $grossAmount, |
| 117 |
]; |
| 118 |
|
| 119 |
if($discountAmount) { |
| 120 |
$position['discountAmount'] = (int) $discountAmount; |
| 121 |
} |
| 122 |
|
| 123 |
$this->positions[] = $position; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string $type |
| 128 |
* @param string $email |
| 129 |
* @param string $fullName |
| 130 |
* @param string $street |
| 131 |
* @param string $city |
| 132 |
* @param string $postalCode |
| 133 |
* @param string $countryCodeAlpha2 |
| 134 |
* @param string $idCountryCodeAlpha2 |
| 135 |
* @param string $idType |
| 136 |
* @param string $idNumber |
| 137 |
* |
| 138 |
* @return Invoice |
| 139 |
*/ |
| 140 |
public function setBuyer($type, $email, $fullName, $street, $city, $postalCode, $countryCodeAlpha2, $idCountryCodeAlpha2 = '', $idType = '', $idNumber = '') |
| 141 |
{ |
| 142 |
|
| 143 |
$array = [ |
| 144 |
'type' => $type, |
| 145 |
'email' => $email, |
| 146 |
'fullName' => $fullName, |
| 147 |
'street' => $street, |
| 148 |
'city' => $city, |
| 149 |
'postalCode' => $postalCode, |
| 150 |
'countryCodeAlpha2' => $countryCodeAlpha2, |
| 151 |
]; |
| 152 |
|
| 153 |
if($idCountryCodeAlpha2) { |
| 154 |
$array['idCountryCodeAlpha2'] = $idCountryCodeAlpha2; |
| 155 |
} |
| 156 |
|
| 157 |
if($idType) { |
| 158 |
$array['idType'] = $idType; |
| 159 |
} |
| 160 |
|
| 161 |
if($idNumber) { |
| 162 |
$array['idNumber'] = $idNumber; |
| 163 |
} |
| 164 |
|
| 165 |
$this->buyer = $array; |
| 166 |
|
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param string $idCountryCodeAlpha2 |
| 172 |
* @param string $idNumber |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public function setCompanyBuyer($idCountryCodeAlpha2, $idNumber, $fullName = '') |
| 177 |
{ |
| 178 |
$this->buyer['type'] = self::BUYER_COMPANY; |
| 179 |
$this->buyer['idCountryCodeAlpha2'] = $idCountryCodeAlpha2; |
| 180 |
$this->buyer['idType'] = self::ID_TYPE_VAT; |
| 181 |
$this->buyer['idNumber'] = $idNumber; |
| 182 |
if($fullName) { |
| 183 |
$this->buyer['fullName'] = $fullName; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @return array|string |
| 189 |
*/ |
| 190 |
public function prepare($isApi) |
| 191 |
{ |
| 192 |
|
| 193 |
$array = [ |
| 194 |
'buyer' => $this->buyer, |
| 195 |
'positions' => $this->positions, |
| 196 |
]; |
| 197 |
|
| 198 |
return $isApi |
| 199 |
? $array |
| 200 |
: base64_encode(gzencode(json_encode($array), 5)); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @param CartData $cart |
| 205 |
* @param string $email |
| 206 |
* @param bool $isApi |
| 207 |
* |
| 208 |
* @return array|string |
| 209 |
*/ |
| 210 |
public static function get($cart, $email, $isApi) |
| 211 |
{ |
| 212 |
|
| 213 |
if(!($cart instanceof CartData)) { |
| 214 |
return []; |
| 215 |
} |
| 216 |
|
| 217 |
$cart = $cart->prepareCartDataArray(); |
| 218 |
|
| 219 |
$invoice = new Invoice(); |
| 220 |
|
| 221 |
if(isset($cart['items']) && $cart['items']) { |
| 222 |
|
| 223 |
foreach($cart['items'] as $item) { |
| 224 |
|
| 225 |
$tax = null; |
| 226 |
try { |
| 227 |
$tax = constant('self::TAX_' . $item['vat']); |
| 228 |
} catch(\Error $e) { |
| 229 |
} |
| 230 |
|
| 231 |
if(!$tax) { |
| 232 |
return []; |
| 233 |
} |
| 234 |
|
| 235 |
$invoice->addItem($item['name'], |
| 236 |
$item['id'], |
| 237 |
$item['quantity'], |
| 238 |
constant('\Imoje\Payment\Invoice::TAX_' . $item['vat']), |
| 239 |
$item['amount'] |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
$invoice->setBuyer(Invoice::BUYER_PERSON, |
| 244 |
$email, |
| 245 |
$cart['address']['billing']['name'], |
| 246 |
$cart['address']['billing']['street'], |
| 247 |
$cart['address']['billing']['city'], |
| 248 |
$cart['address']['billing']['postalCode'], |
| 249 |
$cart['address']['billing']['country']); |
| 250 |
|
| 251 |
return $invoice->prepare($isApi); |
| 252 |
} |
| 253 |
|
| 254 |
return []; |
| 255 |
} |
| 256 |
} |
| 257 |
|