| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sequra\PhpClient; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class BuilderAbstract |
| 7 |
*/ |
| 8 |
abstract class BuilderAbstract |
| 9 |
{ |
| 10 |
|
| 11 |
static $centsPerWhole = 100; |
| 12 |
protected $merchant_id; |
| 13 |
protected $_current_order = null; |
| 14 |
protected $_orders = array(); |
| 15 |
protected $_broken_orders = array(); |
| 16 |
protected $_delivery_report = null; |
| 17 |
protected $_building_report = false; |
| 18 |
|
| 19 |
public function __construct($merchant_id) |
| 20 |
{ |
| 21 |
$this->merchant_id = $merchant_id; |
| 22 |
} |
| 23 |
|
| 24 |
public static abstract function getPreviousOrders($customerID); |
| 25 |
|
| 26 |
public static function integerPrice($price) |
| 27 |
{ |
| 28 |
return is_numeric($price)? |
| 29 |
intval(round(self::$centsPerWhole * (float) $price)): |
| 30 |
0; |
| 31 |
} |
| 32 |
|
| 33 |
protected static function dateOrBlank($date) |
| 34 |
{ |
| 35 |
return $date ? date_format(date_create($date), 'Y-m-d') : ''; |
| 36 |
} |
| 37 |
|
| 38 |
public function build($state = null) |
| 39 |
{ |
| 40 |
$order = array( |
| 41 |
'merchant' => $this->merchant(), |
| 42 |
'cart' => $this->cartWithItems(), |
| 43 |
'delivery_address' => $this->deliveryAddress(), |
| 44 |
'invoice_address' => $this->invoiceAddress(), |
| 45 |
'customer' => $this->customer(), |
| 46 |
'gui' => $this->gui(), |
| 47 |
'platform' => $this->platform(), |
| 48 |
'state' => self::notNull($state) |
| 49 |
); |
| 50 |
$order = $this->fixTotals($order); |
| 51 |
$reference = $this->orderMerchantReference(); |
| 52 |
if ($state && count($reference) > 0) { |
| 53 |
$order['merchant_reference'] = $reference; |
| 54 |
} |
| 55 |
|
| 56 |
return Helper::removeHtmlEntities($order); |
| 57 |
} |
| 58 |
|
| 59 |
public function merchant() |
| 60 |
{ |
| 61 |
return array( |
| 62 |
'id' => $this->merchant_id, |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public abstract function cartWithItems(); |
| 67 |
|
| 68 |
public abstract function customer(); |
| 69 |
|
| 70 |
/* |
| 71 |
* Get order_ref_n to send to sequra |
| 72 |
*/ |
| 73 |
|
| 74 |
public function gui() |
| 75 |
{ |
| 76 |
$data = array( |
| 77 |
'layout' => $this->isMobile() ? 'mobile' : 'desktop', |
| 78 |
); |
| 79 |
|
| 80 |
return $data; |
| 81 |
} |
| 82 |
|
| 83 |
protected static function isMobile() |
| 84 |
{ |
| 85 |
$regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|" |
| 86 |
. "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|" |
| 87 |
. "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|" |
| 88 |
. "symbian|smartphone|mmp|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|" |
| 89 |
. "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220" |
| 90 |
. ")/i"; |
| 91 |
|
| 92 |
if (preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']))) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']), |
| 97 |
'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)); |
| 102 |
$mobile_agents = array( |
| 103 |
'w3c ', |
| 104 |
'acs-', |
| 105 |
'alav', |
| 106 |
'alca', |
| 107 |
'amoi', |
| 108 |
'audi', |
| 109 |
'avan', |
| 110 |
'benq', |
| 111 |
'bird', |
| 112 |
'blac', |
| 113 |
'blaz', |
| 114 |
'brew', |
| 115 |
'cell', |
| 116 |
'cldc', |
| 117 |
'cmd-', |
| 118 |
'dang', |
| 119 |
'doco', |
| 120 |
'eric', |
| 121 |
'hipt', |
| 122 |
'inno', |
| 123 |
'ipaq', |
| 124 |
'java', |
| 125 |
'jigs', |
| 126 |
'kddi', |
| 127 |
'keji', |
| 128 |
'leno', |
| 129 |
'lg-c', |
| 130 |
'lg-d', |
| 131 |
'lg-g', |
| 132 |
'lge-', |
| 133 |
'maui', |
| 134 |
'maxo', |
| 135 |
'midp', |
| 136 |
'mits', |
| 137 |
'mmef', |
| 138 |
'mobi', |
| 139 |
'mot-', |
| 140 |
'moto', |
| 141 |
'mwbp', |
| 142 |
'nec-', |
| 143 |
'newt', |
| 144 |
'noki', |
| 145 |
'oper', |
| 146 |
'palm', |
| 147 |
'pana', |
| 148 |
'pant', |
| 149 |
'phil', |
| 150 |
'play', |
| 151 |
'port', |
| 152 |
'prox', |
| 153 |
'qwap', |
| 154 |
'sage', |
| 155 |
'sams', |
| 156 |
'sany', |
| 157 |
'sch-', |
| 158 |
'sec-', |
| 159 |
'send', |
| 160 |
'seri', |
| 161 |
'sgh-', |
| 162 |
'shar', |
| 163 |
'sie-', |
| 164 |
'siem', |
| 165 |
'smal', |
| 166 |
'smar', |
| 167 |
'sony', |
| 168 |
'sph-', |
| 169 |
'symb', |
| 170 |
't-mo', |
| 171 |
'teli', |
| 172 |
'tim-', |
| 173 |
'tosh', |
| 174 |
'tsm-', |
| 175 |
'upg1', |
| 176 |
'upsi', |
| 177 |
'vk-v', |
| 178 |
'voda', |
| 179 |
'wap-', |
| 180 |
'wapa', |
| 181 |
'wapi', |
| 182 |
'wapp', |
| 183 |
'wapr', |
| 184 |
'webc', |
| 185 |
'winw', |
| 186 |
'winw', |
| 187 |
'xda ', |
| 188 |
'xda-' |
| 189 |
); |
| 190 |
|
| 191 |
if (in_array($mobile_ua, $mobile_agents)) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'OperaMini') > 0) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
public static abstract function platform(); |
| 203 |
|
| 204 |
protected static function notNull($value1) |
| 205 |
{ |
| 206 |
return is_null($value1) ? '' : $value1; |
| 207 |
} |
| 208 |
|
| 209 |
public function fixTotals($order) |
| 210 |
{ |
| 211 |
$totals = Helper::totals($order['cart']); |
| 212 |
$diff_with_tax = $order['cart']['order_total_with_tax'] - $totals['with_tax']; |
| 213 |
if (isset($order['cart']['order_total_without_tax'])) { |
| 214 |
$diff_without_tax = $order['cart']['order_total_without_tax'] - $totals['without_tax']; |
| 215 |
} else { |
| 216 |
$diff_without_tax = $diff_with_tax; |
| 217 |
} |
| 218 |
$diff_max = abs(max($diff_with_tax, $diff_without_tax)); |
| 219 |
/*Dont correct error bigger than Itm count cents*/ |
| 220 |
if ($diff_max == 0 || abs($diff_max) > count($order['cart']['items'])) { |
| 221 |
return $order; |
| 222 |
} |
| 223 |
$items = array(); |
| 224 |
$amended = false; |
| 225 |
foreach ($order['cart']['items'] as $item) { |
| 226 |
if (isset($item['type']) && 'discount' == $item['type']) { |
| 227 |
$item['total_without_tax'] += $diff_without_tax; |
| 228 |
$item['total_with_tax'] += $diff_with_tax; |
| 229 |
$item['name'] .= ('' == $item['name'] ? '' : ' + ') . 'ajuste por redondeos'; |
| 230 |
$amended = true; |
| 231 |
} |
| 232 |
$items[] = $item; |
| 233 |
} |
| 234 |
if ( ! $amended) { |
| 235 |
unset($item); |
| 236 |
$item['type'] = 'discount'; |
| 237 |
if ($diff_with_tax > 0) { |
| 238 |
$item['type'] = 'handling'; |
| 239 |
} |
| 240 |
$item['reference'] = ''; |
| 241 |
$item['name'] = 'ajuste por redondeos'; |
| 242 |
$item['total_without_tax'] = $diff_without_tax; |
| 243 |
$item['total_with_tax'] = $diff_with_tax; |
| 244 |
$items[] = $item; |
| 245 |
} |
| 246 |
|
| 247 |
$order['cart']['items'] = $items; |
| 248 |
|
| 249 |
return $order; |
| 250 |
} |
| 251 |
|
| 252 |
public function orderMerchantReference() |
| 253 |
{ |
| 254 |
$ret = array(); |
| 255 |
$ref = self::notNull($this->getOrderRef(1)); |
| 256 |
if ('' != $ref) { |
| 257 |
$ret['order_ref_1'] = $ref; |
| 258 |
} |
| 259 |
$ref = self::notNull($this->getOrderRef(2)); |
| 260 |
if ('' != $ref) { |
| 261 |
$ret['order_ref_2'] = $ref; |
| 262 |
} |
| 263 |
|
| 264 |
return $ret; |
| 265 |
} |
| 266 |
|
| 267 |
public abstract function getOrderRef($i); |
| 268 |
|
| 269 |
public function getDeliveryReport() |
| 270 |
{ |
| 271 |
if (is_null($this->_delivery_report)) { |
| 272 |
$this->buildDeliveryReport(); |
| 273 |
} |
| 274 |
|
| 275 |
return $this->_delivery_report; |
| 276 |
} |
| 277 |
|
| 278 |
public function buildDeliveryReport() |
| 279 |
{ |
| 280 |
$this->_building_report = true; |
| 281 |
$this->buildShippedOrders(); |
| 282 |
$this->buildBrokenOrders(); |
| 283 |
$this->_delivery_report = array( |
| 284 |
'merchant' => $this->merchant(), |
| 285 |
'orders' => $this->_orders, |
| 286 |
'broken_orders' => $this->_broken_orders, |
| 287 |
'statistics' => array('orders' => $this->getOrderStats()), |
| 288 |
'platform' => $this->platform() |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
public abstract function buildShippedOrders(); |
| 293 |
|
| 294 |
function buildBrokenOrders() |
| 295 |
{ |
| 296 |
foreach ($this->_orders as $key => $order) { |
| 297 |
if ( ! Helper::isConsistentCart($order['cart'])) { |
| 298 |
$this->_broken_orders[] = $order; |
| 299 |
unset($this->_orders[$key]); |
| 300 |
} |
| 301 |
} |
| 302 |
$this->_orders = array_values($this->_orders); |
| 303 |
} |
| 304 |
|
| 305 |
public abstract function getOrderStats(); |
| 306 |
|
| 307 |
public abstract function deliveryMethod(); |
| 308 |
|
| 309 |
public function items() |
| 310 |
{ |
| 311 |
return array_merge( |
| 312 |
$this->productItems(), |
| 313 |
$this->handlingItems(), |
| 314 |
$this->discountItems(), |
| 315 |
$this->extraItems() |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
public function productItems(){ |
| 320 |
return array(); |
| 321 |
} |
| 322 |
|
| 323 |
public function handlingItems(){ |
| 324 |
return array(); |
| 325 |
} |
| 326 |
|
| 327 |
public function discountItems(){ |
| 328 |
return array(); |
| 329 |
} |
| 330 |
|
| 331 |
public function extraItems(){ |
| 332 |
return array(); |
| 333 |
} |
| 334 |
} |
| 335 |
|