VM暫存
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

2078 linhas
69KB

  1. <?php
  2. /*
  3. 電子發票SDK
  4. 版本:1.0.0
  5. @author Wesley
  6. */
  7. // 執行發票作業項目。
  8. abstract class InvoiceMethod
  9. {
  10. // 一般開立發票。
  11. const INVOICE = 'INVOICE';
  12. // 延遲或觸發開立發票。
  13. const INVOICE_DELAY = 'INVOICE_DELAY';
  14. // 開立折讓。
  15. const ALLOWANCE = 'ALLOWANCE';
  16. // 發票作廢。
  17. const INVOICE_VOID = 'INVOICE_VOID';
  18. // 折讓作廢。
  19. const ALLOWANCE_VOID = 'ALLOWANCE_VOID';
  20. // 查詢發票。
  21. const INVOICE_SEARCH = 'INVOICE_SEARCH';
  22. // 查詢作廢發票。
  23. const INVOICE_VOID_SEARCH = 'INVOICE_VOID_SEARCH';
  24. // 查詢折讓明細。
  25. const ALLOWANCE_SEARCH = 'ALLOWANCE_SEARCH';
  26. // 查詢折讓作廢明細。
  27. const ALLOWANCE_VOID_SEARCH = 'ALLOWANCE_VOID_SEARCH';
  28. // 發送通知。
  29. const INVOICE_NOTIFY = 'INVOICE_NOTIFY';
  30. // 付款完成觸發或延遲開立發票。
  31. const INVOICE_TRIGGER = 'INVOICE_TRIGGER';
  32. }
  33. // 電子發票開立註記。
  34. abstract class InvoiceState
  35. {
  36. // 需要開立電子發票。
  37. const Yes = 'Y';
  38. // 不需要開立電子發票。
  39. const No = '';
  40. }
  41. // 電子發票載具類別
  42. abstract class CarruerType
  43. {
  44. // 無載具
  45. const None = '';
  46. // 會員載具
  47. const Member = '1';
  48. // 買受人自然人憑證
  49. const Citizen = '2';
  50. // 買受人手機條碼
  51. const Cellphone = '3';
  52. }
  53. // 電子發票列印註記
  54. abstract class PrintMark
  55. {
  56. // 不列印
  57. const No = '0';
  58. // 列印
  59. const Yes = '1';
  60. }
  61. // 電子發票捐贈註記
  62. abstract class Donation
  63. {
  64. // 捐贈
  65. const Yes = '1';
  66. // 不捐贈
  67. const No = '2';
  68. }
  69. // 通關方式
  70. abstract class ClearanceMark
  71. {
  72. // 經海關出口
  73. const Yes = '1';
  74. // 非經海關出口
  75. const No = '2';
  76. }
  77. // 課稅類別
  78. abstract class TaxType
  79. {
  80. // 應稅
  81. const Dutiable = '1';
  82. // 零稅率
  83. const Zero = '2';
  84. // 免稅
  85. const Free = '3';
  86. // 應稅與免稅混合(限收銀機發票無法分辦時使用,且需通過申請核可)
  87. const Mix = '9';
  88. }
  89. // 字軌類別
  90. abstract class InvType
  91. {
  92. // 一般稅額
  93. const General = '07';
  94. // 特種稅額
  95. const Special = '08';
  96. }
  97. // 商品單價是否含稅
  98. abstract class VatType
  99. {
  100. // 商品單價含稅價
  101. const Yes = '1';
  102. // 商品單價未稅價
  103. const No = '0';
  104. }
  105. // 延遲註記
  106. abstract class DelayFlagType
  107. {
  108. // 延遲註記
  109. const Delay = '1';
  110. // 觸發註記
  111. const Trigger = '2';
  112. }
  113. // 交易類別
  114. abstract class PayTypeCategory
  115. {
  116. // ECBANK
  117. const Ecbank = '1';
  118. // ECPAY
  119. const Ecpay = '2';
  120. // ALLPAY
  121. const Allpay = '3';
  122. }
  123. // 通知類別
  124. abstract class AllowanceNotifyType
  125. {
  126. // 簡訊通知
  127. const Sms = 'S';
  128. // 電子郵件通知
  129. const Email = 'E';
  130. // 皆通知
  131. const All = 'A';
  132. // 皆不通知
  133. const None = 'N';
  134. }
  135. // 發送方式
  136. abstract class NotifyType
  137. {
  138. // 簡訊通知
  139. const Sms = 'S';
  140. // 電子郵件通知
  141. const Email = 'E';
  142. // 皆通知
  143. const All = 'A';
  144. }
  145. // 發送內容類型
  146. abstract class InvoiceTagType
  147. {
  148. // 發票開立
  149. const Invoice = 'I';
  150. // 發票作廢
  151. const Invoice_Void = 'II';
  152. // 折讓開立
  153. const Allowance = 'A';
  154. // 折讓作廢
  155. const Allowance_Void = 'AI';
  156. // 發票中獎
  157. const Invoice_Winning = 'AW';
  158. }
  159. // 發送對象
  160. abstract class NotifiedType
  161. {
  162. // 通知客戶
  163. const Customer = 'C';
  164. // 通知廠商
  165. const vendor = 'M';
  166. // 皆發送
  167. const All = 'A';
  168. }
  169. class AllInvoice
  170. {
  171. public $TimeStamp = 0;
  172. public $nInvCreateDate = 0;
  173. public $MerchantID = '';
  174. public $HashKey = '';
  175. public $HashIV = '';
  176. public $Send = 'Send';
  177. public $Invoice_Method = 'Invoice_Method'; // 電子發票執行項目
  178. public $Invoice_Url = 'Invoice_Url'; // 電子發票執行網址
  179. function __construct()
  180. {
  181. $this->AllInvoice();
  182. $this->TimeStamp = time();
  183. $this->Send = array(
  184. "RelateNumber" => '',
  185. "CustomerID" => '',
  186. "CustomerIdentifier" => '',
  187. "CustomerName" => '',
  188. "CustomerAddr" => '',
  189. "CustomerPhone" => '',
  190. "CustomerEmail" => '',
  191. "ClearanceMark" => '',
  192. "Print" => PrintMark::No,
  193. "Donation" => Donation::No,
  194. "LoveCode" => '',
  195. "CarruerType" => CarruerType::None,
  196. "CarruerNum" => '',
  197. "TaxType" => '',
  198. "SalesAmount" => '',
  199. "InvoiceRemark" => '',
  200. "Items" => array(),
  201. "InvType" => '',
  202. "InvCreateDate" => '',
  203. "vat" => VatType::Yes,
  204. "DelayFlag" => '',
  205. "DelayDay" => 0,
  206. "ECBankID" => '',
  207. "Tsr" => '',
  208. "PayType" => '',
  209. "PayAct" => '',
  210. "NotifyURL" => '',
  211. "InvoiceNo" => '',
  212. "AllowanceNotify" => '',
  213. "NotifyMail" => '',
  214. "NotifyPhone" => '',
  215. "AllowanceAmount" => '',
  216. "InvoiceNumber" => '',
  217. "Reason" => '',
  218. "AllowanceNo" => '',
  219. "Phone" => '',
  220. "Notify" => '',
  221. "InvoiceTag" => '',
  222. "Notified" => ''
  223. );
  224. }
  225. function AllInvoice()
  226. {
  227. }
  228. /**
  229. * 執行發票各項動作
  230. */
  231. function Check_Out()
  232. {
  233. // 變數宣告
  234. $arErrors = '' ;
  235. $aSend_Info = array() ; // 送出參數
  236. $aSend_CheckMac_Info = array() ; // 送出檢查碼用陣列
  237. $aReturn_Info = array() ; // 回傳參數
  238. $aReturn_CheckMacValue = array() ; // 回傳檢查碼用陣列
  239. $sReturn_CheckMacValue = '' ; // 回傳回來的CheckMacValue
  240. // A.一般開立發票、B.延遲或觸發開立發票、C.開立折讓使用
  241. $sItemName = '' ; // 商品名稱
  242. $sItemCount = '' ; // 商品數量
  243. $sItemWord = '' ; // 商品單位
  244. $sItemPrice = '' ; // 商品價格
  245. $sItemTaxType = '' ; // 商品課稅別
  246. $sItemAmount = '' ; // 商品合計
  247. $nItems_Foreach_Count = 1 ; // 商品計算
  248. // 參數檢查
  249. $arErrors = $this->Check_String() ;
  250. // 1.有錯誤變數
  251. if(sizeof($arErrors) > 0)
  252. {
  253. throw new Exception(join('<br>', $arErrors));
  254. }
  255. else
  256. {
  257. // 2.整理必要參數
  258. $aSend_Info = $this->Send ;
  259. $aSend_Info['TimeStamp'] = $this->TimeStamp ;
  260. $aSend_Info['MerchantID'] = $this->MerchantID ;
  261. // 3.判斷動作類型
  262. // A.一般開立發票
  263. if( $this->Invoice_Method == InvoiceMethod::INVOICE )
  264. {
  265. // 3-1過濾不需要的項目
  266. unset($aSend_Info['DelayFlag']) ;
  267. unset($aSend_Info['DelayDay']) ;
  268. unset($aSend_Info['ECBankID']) ;
  269. unset($aSend_Info['Tsr']) ;
  270. unset($aSend_Info['PayType']) ;
  271. unset($aSend_Info['PayAct']) ;
  272. unset($aSend_Info['NotifyURL']) ;
  273. unset($aSend_Info['InvoiceNo']) ;
  274. unset($aSend_Info['AllowanceNotify']) ;
  275. unset($aSend_Info['NotifyMail']) ;
  276. unset($aSend_Info['NotifyPhone']) ;
  277. unset($aSend_Info['AllowanceAmount']) ;
  278. unset($aSend_Info['InvoiceNumber']) ;
  279. unset($aSend_Info['Reason']) ;
  280. unset($aSend_Info['AllowanceNo']) ;
  281. unset($aSend_Info['Phone']) ;
  282. unset($aSend_Info['Notify']) ;
  283. unset($aSend_Info['InvoiceTag']) ;
  284. unset($aSend_Info['Notified']) ;
  285. // 3-2商品資訊組合
  286. $nItems_Count_Total = count($this->Send['Items']) ; // 商品總筆數
  287. foreach($this->Send['Items'] as $key => $value)
  288. {
  289. $sItemName .= $value['ItemName'] ;
  290. $sItemCount .= (int) $value['ItemCount'] ;
  291. $sItemWord .= $value['ItemWord'] ;
  292. $sItemPrice .= (int) $value['ItemPrice'] ;
  293. $sItemTaxType .= $value['ItemTaxType'] ;
  294. $sItemAmount .= (int) $value['ItemAmount'] ;
  295. if( $nItems_Foreach_Count < $nItems_Count_Total )
  296. {
  297. $sItemName .= '|' ;
  298. $sItemCount .= '|' ;
  299. $sItemWord .= '|' ;
  300. $sItemPrice .= '|' ;
  301. $sItemTaxType .= '|' ;
  302. $sItemAmount .= '|' ;
  303. }
  304. $nItems_Foreach_Count++ ;
  305. }
  306. $aSend_Info['ItemName'] = urlencode($sItemName); // 商品名稱
  307. $aSend_Info['ItemCount'] = $sItemCount ;
  308. $aSend_Info['ItemWord'] = urlencode($sItemWord); // 商品單位
  309. $aSend_Info['ItemPrice'] = $sItemPrice ;
  310. $aSend_Info['ItemTaxType'] = $sItemTaxType ;
  311. $aSend_Info['ItemAmount'] = $sItemAmount ;
  312. unset($aSend_Info['Items']) ;
  313. // 如果沒有填入資料,則清除該欄位,預設為當下時間
  314. if($aSend_Info['InvCreateDate'] == '')
  315. {
  316. unset($aSend_Info['InvCreateDate']) ;
  317. }
  318. // 3-3產生檢查碼
  319. $aSend_CheckMac_Info = $aSend_Info ;
  320. // 過濾不需要參數項目
  321. unset($aSend_CheckMac_Info['ItemName']) ;
  322. unset($aSend_CheckMac_Info['ItemWord']) ;
  323. unset($aSend_CheckMac_Info['InvoiceRemark']) ;
  324. // 載具編號內包含+號則改為空白
  325. $aSend_CheckMac_Info['CarruerNum'] = str_replace ('+',' ',$aSend_CheckMac_Info['CarruerNum']);
  326. // 產生檢查碼
  327. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  328. }
  329. // B.延遲或觸發開立發票
  330. if( $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  331. {
  332. // 3-1過濾不需要的項目
  333. unset($aSend_Info['InvCreateDate']) ;
  334. unset($aSend_Info['vat']) ;
  335. unset($aSend_Info['InvoiceNo']) ;
  336. unset($aSend_Info['AllowanceNotify']) ;
  337. unset($aSend_Info['NotifyMail']) ;
  338. unset($aSend_Info['NotifyPhone']) ;
  339. unset($aSend_Info['AllowanceAmount']) ;
  340. unset($aSend_Info['InvoiceNumber']) ;
  341. unset($aSend_Info['Reason']) ;
  342. unset($aSend_Info['AllowanceNo']) ;
  343. unset($aSend_Info['Phone']) ;
  344. unset($aSend_Info['Notify']) ;
  345. unset($aSend_Info['InvoiceTag']) ;
  346. unset($aSend_Info['Notified']) ;
  347. if(empty($aSend_Info['CustomerIdentifier']))
  348. {
  349. unset($aSend_Info['CustomerIdentifier']);
  350. }
  351. else
  352. {
  353. $aSend_Info['CustomerIdentifier'] ;
  354. }
  355. // 3-2商品資訊組合
  356. $nItems_Count_Total = count($this->Send['Items']) ; // 商品總筆數
  357. foreach($this->Send['Items'] as $key => $value)
  358. {
  359. $sItemName .= $value['ItemName'] ;
  360. $sItemCount .= (int) $value['ItemCount'] ;
  361. $sItemWord .= $value['ItemWord'] ;
  362. $sItemPrice .= (int) $value['ItemPrice'] ;
  363. $sItemTaxType .= $value['ItemTaxType'] ;
  364. $sItemAmount .= (int) $value['ItemAmount'] ;
  365. if( $nItems_Foreach_Count < $nItems_Count_Total )
  366. {
  367. $sItemName .= '|' ;
  368. $sItemCount .= '|' ;
  369. $sItemWord .= '|' ;
  370. $sItemPrice .= '|' ;
  371. $sItemTaxType .= '|' ;
  372. $sItemAmount .= '|' ;
  373. }
  374. $nItems_Foreach_Count++ ;
  375. }
  376. $aSend_Info['ItemName'] = urlencode($sItemName); // 商品名稱
  377. $aSend_Info['ItemCount'] = $sItemCount ;
  378. $aSend_Info['ItemWord'] = urlencode($sItemWord); // 商品單位
  379. $aSend_Info['ItemPrice'] = $sItemPrice ;
  380. $aSend_Info['ItemTaxType'] = $sItemTaxType ;
  381. $aSend_Info['ItemAmount'] = $sItemAmount ;
  382. unset($aSend_Info['Items']) ;
  383. // 3-3產生檢查碼
  384. $aSend_CheckMac_Info = $aSend_Info ;
  385. // 過濾不需要參數項目
  386. unset($aSend_CheckMac_Info['ItemName']) ;
  387. unset($aSend_CheckMac_Info['ItemWord']) ;
  388. unset($aSend_CheckMac_Info['InvoiceRemark']) ;
  389. // 載具編號內包含+號則改為空白
  390. $aSend_CheckMac_Info['CarruerNum'] = str_replace ('+',' ',$aSend_CheckMac_Info['CarruerNum']);
  391. // 產生檢查碼
  392. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  393. }
  394. // C.開立折讓
  395. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE )
  396. {
  397. // 3-1過濾不需要的項目
  398. unset($aSend_Info['RelateNumber']) ;
  399. unset($aSend_Info['CustomerID']) ;
  400. unset($aSend_Info['CustomerIdentifier']) ;
  401. unset($aSend_Info['CustomerAddr']) ;
  402. unset($aSend_Info['CustomerPhone']) ;
  403. unset($aSend_Info['CustomerEmail']) ;
  404. unset($aSend_Info['ClearanceMark']) ;
  405. unset($aSend_Info['Print']) ;
  406. unset($aSend_Info['Donation']) ;
  407. unset($aSend_Info['LoveCode']) ;
  408. unset($aSend_Info['CarruerType']) ;
  409. unset($aSend_Info['CarruerNum']) ;
  410. unset($aSend_Info['TaxType']) ;
  411. unset($aSend_Info['SalesAmount']) ;
  412. unset($aSend_Info['InvoiceRemark']) ;
  413. unset($aSend_Info['InvType']) ;
  414. unset($aSend_Info['InvCreateDate']) ;
  415. unset($aSend_Info['vat']) ;
  416. unset($aSend_Info['DelayFlag']) ;
  417. unset($aSend_Info['DelayDay']) ;
  418. unset($aSend_Info['ECBankID']) ;
  419. unset($aSend_Info['Tsr']) ;
  420. unset($aSend_Info['PayType']) ;
  421. unset($aSend_Info['PayAct']) ;
  422. unset($aSend_Info['NotifyURL']) ;
  423. unset($aSend_Info['InvoiceNumber']) ;
  424. unset($aSend_Info['Reason']) ;
  425. unset($aSend_Info['AllowanceNo']) ;
  426. unset($aSend_Info['Phone']) ;
  427. unset($aSend_Info['Notify']) ;
  428. unset($aSend_Info['InvoiceTag']) ;
  429. unset($aSend_Info['Notified']) ;
  430. // 3-2商品資訊組合
  431. $nItems_Count_Total = count($this->Send['Items']) ; // 商品總筆數
  432. foreach($this->Send['Items'] as $key => $value)
  433. {
  434. $sItemName .= $value['ItemName'] ;
  435. $sItemCount .= (int) $value['ItemCount'] ;
  436. $sItemWord .= $value['ItemWord'] ;
  437. $sItemPrice .= (int) $value['ItemPrice'] ;
  438. $sItemTaxType .= $value['ItemTaxType'] ;
  439. $sItemAmount .= (int) $value['ItemAmount'] ;
  440. if( $nItems_Foreach_Count < $nItems_Count_Total )
  441. {
  442. $sItemName .= '|' ;
  443. $sItemCount .= '|' ;
  444. $sItemWord .= '|' ;
  445. $sItemPrice .= '|' ;
  446. $sItemTaxType .= '|' ;
  447. $sItemAmount .= '|' ;
  448. }
  449. $nItems_Foreach_Count++ ;
  450. }
  451. $aSend_Info['ItemName'] = urlencode($sItemName); // 商品名稱
  452. $aSend_Info['ItemCount'] = $sItemCount ;
  453. $aSend_Info['ItemWord'] = urlencode($sItemWord); // 商品單位
  454. $aSend_Info['ItemPrice'] = $sItemPrice ;
  455. $aSend_Info['ItemTaxType'] = $sItemTaxType ;
  456. $aSend_Info['ItemAmount'] = $sItemAmount ;
  457. unset($aSend_Info['Items']) ;
  458. // 3-3產生檢查碼
  459. $aSend_CheckMac_Info = $aSend_Info ;
  460. // 過濾不需要參數項目
  461. unset($aSend_CheckMac_Info['ItemName']) ;
  462. unset($aSend_CheckMac_Info['ItemWord']) ;
  463. // 產生檢查碼
  464. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  465. }
  466. // D.發票作廢
  467. if( $this->Invoice_Method == InvoiceMethod::INVOICE_VOID )
  468. {
  469. // 3-1過濾不需要的項目
  470. unset($aSend_Info['RelateNumber']) ;
  471. unset($aSend_Info['CustomerID']) ;
  472. unset($aSend_Info['CustomerIdentifier']) ;
  473. unset($aSend_Info['CustomerName']) ;
  474. unset($aSend_Info['CustomerAddr']) ;
  475. unset($aSend_Info['CustomerPhone']) ;
  476. unset($aSend_Info['CustomerEmail']) ;
  477. unset($aSend_Info['ClearanceMark']) ;
  478. unset($aSend_Info['Print']) ;
  479. unset($aSend_Info['Donation']) ;
  480. unset($aSend_Info['LoveCode']) ;
  481. unset($aSend_Info['CarruerType']) ;
  482. unset($aSend_Info['CarruerNum']) ;
  483. unset($aSend_Info['TaxType']) ;
  484. unset($aSend_Info['SalesAmount']) ;
  485. unset($aSend_Info['InvoiceRemark']) ;
  486. unset($aSend_Info['ItemName']) ;
  487. unset($aSend_Info['ItemCount']) ;
  488. unset($aSend_Info['ItemWord']) ;
  489. unset($aSend_Info['ItemPrice']) ;
  490. unset($aSend_Info['ItemTaxType']) ;
  491. unset($aSend_Info['ItemAmount']) ;
  492. unset($aSend_Info['InvType']) ;
  493. unset($aSend_Info['InvCreateDate']) ;
  494. unset($aSend_Info['vat']) ;
  495. unset($aSend_Info['DelayFlag']) ;
  496. unset($aSend_Info['DelayDay']) ;
  497. unset($aSend_Info['ECBankID']) ;
  498. unset($aSend_Info['Tsr']) ;
  499. unset($aSend_Info['PayType']) ;
  500. unset($aSend_Info['PayAct']) ;
  501. unset($aSend_Info['NotifyURL']) ;
  502. unset($aSend_Info['InvoiceNo']) ;
  503. unset($aSend_Info['AllowanceNotify']) ;
  504. unset($aSend_Info['NotifyMail']) ;
  505. unset($aSend_Info['NotifyPhone']) ;
  506. unset($aSend_Info['AllowanceAmount']) ;
  507. unset($aSend_Info['AllowanceNo']) ;
  508. unset($aSend_Info['Phone']) ;
  509. unset($aSend_Info['Notify']) ;
  510. unset($aSend_Info['InvoiceTag']) ;
  511. unset($aSend_Info['Notified']) ;
  512. // 3-2商品資訊組合
  513. unset($aSend_Info['Items']) ;
  514. // 3-3產生檢查碼
  515. $aSend_CheckMac_Info = $aSend_Info ;
  516. // 過濾不需要參數項目
  517. unset($aSend_CheckMac_Info['Reason']) ;
  518. // 產生檢查碼
  519. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  520. }
  521. // E.折讓作廢
  522. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID )
  523. {
  524. // 3-1過濾不需要的項目
  525. unset($aSend_Info['RelateNumber']) ;
  526. unset($aSend_Info['CustomerID']) ;
  527. unset($aSend_Info['CustomerIdentifier']) ;
  528. unset($aSend_Info['CustomerName']) ;
  529. unset($aSend_Info['CustomerAddr']) ;
  530. unset($aSend_Info['CustomerPhone']) ;
  531. unset($aSend_Info['CustomerEmail']) ;
  532. unset($aSend_Info['ClearanceMark']) ;
  533. unset($aSend_Info['Print']) ;
  534. unset($aSend_Info['Donation']) ;
  535. unset($aSend_Info['LoveCode']) ;
  536. unset($aSend_Info['CarruerType']) ;
  537. unset($aSend_Info['CarruerNum']) ;
  538. unset($aSend_Info['TaxType']) ;
  539. unset($aSend_Info['SalesAmount']) ;
  540. unset($aSend_Info['InvoiceRemark']) ;
  541. unset($aSend_Info['ItemName']) ;
  542. unset($aSend_Info['ItemCount']) ;
  543. unset($aSend_Info['ItemWord']) ;
  544. unset($aSend_Info['ItemPrice']) ;
  545. unset($aSend_Info['ItemTaxType']) ;
  546. unset($aSend_Info['ItemAmount']) ;
  547. unset($aSend_Info['InvType']) ;
  548. unset($aSend_Info['InvCreateDate']) ;
  549. unset($aSend_Info['vat']) ;
  550. unset($aSend_Info['DelayFlag']) ;
  551. unset($aSend_Info['DelayDay']) ;
  552. unset($aSend_Info['ECBankID']) ;
  553. unset($aSend_Info['Tsr']) ;
  554. unset($aSend_Info['PayType']) ;
  555. unset($aSend_Info['PayAct']) ;
  556. unset($aSend_Info['NotifyURL']) ;
  557. unset($aSend_Info['AllowanceNotify']) ;
  558. unset($aSend_Info['NotifyMail']) ;
  559. unset($aSend_Info['NotifyPhone']) ;
  560. unset($aSend_Info['AllowanceAmount']) ;
  561. unset($aSend_Info['InvoiceNumber']) ;
  562. unset($aSend_Info['Phone']) ;
  563. unset($aSend_Info['Notify']) ;
  564. unset($aSend_Info['InvoiceTag']) ;
  565. unset($aSend_Info['Notified']) ;
  566. // 3-2商品資訊組合
  567. unset($aSend_Info['Items']) ;
  568. // 3-3產生檢查碼
  569. $aSend_CheckMac_Info = $aSend_Info ;
  570. // 過濾不需要參數項目
  571. unset($aSend_CheckMac_Info['Reason']) ;
  572. // 產生檢查碼
  573. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  574. }
  575. // F.查詢發票 G.查詢作廢發票
  576. if( $this->Invoice_Method == InvoiceMethod::INVOICE_SEARCH || $this->Invoice_Method == InvoiceMethod::INVOICE_VOID_SEARCH )
  577. {
  578. // 3-1過濾不需要的項目
  579. unset($aSend_Info['CustomerID']) ;
  580. unset($aSend_Info['CustomerIdentifier']) ;
  581. unset($aSend_Info['CustomerName']) ;
  582. unset($aSend_Info['CustomerAddr']) ;
  583. unset($aSend_Info['CustomerPhone']) ;
  584. unset($aSend_Info['CustomerEmail']) ;
  585. unset($aSend_Info['ClearanceMark']) ;
  586. unset($aSend_Info['Print']) ;
  587. unset($aSend_Info['Donation']) ;
  588. unset($aSend_Info['LoveCode']) ;
  589. unset($aSend_Info['CarruerType']) ;
  590. unset($aSend_Info['CarruerNum']) ;
  591. unset($aSend_Info['TaxType']) ;
  592. unset($aSend_Info['SalesAmount']) ;
  593. unset($aSend_Info['InvoiceRemark']) ;
  594. unset($aSend_Info['ItemName']) ;
  595. unset($aSend_Info['ItemCount']) ;
  596. unset($aSend_Info['ItemWord']) ;
  597. unset($aSend_Info['ItemPrice']) ;
  598. unset($aSend_Info['ItemTaxType']) ;
  599. unset($aSend_Info['ItemAmount']) ;
  600. unset($aSend_Info['InvType']) ;
  601. unset($aSend_Info['InvCreateDate']) ;
  602. unset($aSend_Info['vat']) ;
  603. unset($aSend_Info['DelayFlag']) ;
  604. unset($aSend_Info['DelayDay']) ;
  605. unset($aSend_Info['ECBankID']) ;
  606. unset($aSend_Info['Tsr']) ;
  607. unset($aSend_Info['PayType']) ;
  608. unset($aSend_Info['PayAct']) ;
  609. unset($aSend_Info['NotifyURL']) ;
  610. unset($aSend_Info['InvoiceNo']) ;
  611. unset($aSend_Info['AllowanceNotify']) ;
  612. unset($aSend_Info['NotifyMail']) ;
  613. unset($aSend_Info['NotifyPhone']) ;
  614. unset($aSend_Info['AllowanceAmount']) ;
  615. unset($aSend_Info['InvoiceNumber']) ;
  616. unset($aSend_Info['Reason']) ;
  617. unset($aSend_Info['AllowanceNo']) ;
  618. unset($aSend_Info['Phone']) ;
  619. unset($aSend_Info['Notify']) ;
  620. unset($aSend_Info['InvoiceTag']) ;
  621. unset($aSend_Info['Notified']) ;
  622. // 3-2商品資訊組合
  623. unset($aSend_Info['Items']) ;
  624. // 3-3產生檢查碼
  625. $aSend_CheckMac_Info = $aSend_Info ;
  626. // 過濾不需要參數項目
  627. unset($aSend_CheckMac_Info['Reason']) ;
  628. // 產生檢查碼
  629. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  630. }
  631. // H.查詢折讓明細 I.查詢折讓作廢明細
  632. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE_SEARCH || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID_SEARCH )
  633. {
  634. // 3-1過濾不需要的項目
  635. unset($aSend_Info['RelateNumber']) ;
  636. unset($aSend_Info['CustomerID']) ;
  637. unset($aSend_Info['CustomerIdentifier']) ;
  638. unset($aSend_Info['CustomerName']) ;
  639. unset($aSend_Info['CustomerAddr']) ;
  640. unset($aSend_Info['CustomerPhone']) ;
  641. unset($aSend_Info['CustomerEmail']) ;
  642. unset($aSend_Info['ClearanceMark']) ;
  643. unset($aSend_Info['Print']) ;
  644. unset($aSend_Info['Donation']) ;
  645. unset($aSend_Info['LoveCode']) ;
  646. unset($aSend_Info['CarruerType']) ;
  647. unset($aSend_Info['CarruerNum']) ;
  648. unset($aSend_Info['TaxType']) ;
  649. unset($aSend_Info['SalesAmount']) ;
  650. unset($aSend_Info['InvoiceRemark']) ;
  651. unset($aSend_Info['ItemName']) ;
  652. unset($aSend_Info['ItemCount']) ;
  653. unset($aSend_Info['ItemWord']) ;
  654. unset($aSend_Info['ItemPrice']) ;
  655. unset($aSend_Info['ItemTaxType']) ;
  656. unset($aSend_Info['ItemAmount']) ;
  657. unset($aSend_Info['InvType']) ;
  658. unset($aSend_Info['InvCreateDate']) ;
  659. unset($aSend_Info['vat']) ;
  660. unset($aSend_Info['DelayFlag']) ;
  661. unset($aSend_Info['DelayDay']) ;
  662. unset($aSend_Info['ECBankID']) ;
  663. unset($aSend_Info['Tsr']) ;
  664. unset($aSend_Info['PayType']) ;
  665. unset($aSend_Info['PayAct']) ;
  666. unset($aSend_Info['NotifyURL']) ;
  667. unset($aSend_Info['AllowanceNotify']) ;
  668. unset($aSend_Info['NotifyMail']) ;
  669. unset($aSend_Info['NotifyPhone']) ;
  670. unset($aSend_Info['AllowanceAmount']) ;
  671. unset($aSend_Info['InvoiceNumber']) ;
  672. unset($aSend_Info['Reason']) ;
  673. unset($aSend_Info['Phone']) ;
  674. unset($aSend_Info['Notify']) ;
  675. unset($aSend_Info['InvoiceTag']) ;
  676. unset($aSend_Info['Notified']) ;
  677. // 3-2商品資訊組合
  678. unset($aSend_Info['Items']) ;
  679. // 3-3產生檢查碼
  680. $aSend_CheckMac_Info = $aSend_Info ;
  681. // 產生檢查碼
  682. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  683. }
  684. // J.發送通知
  685. if( $this->Invoice_Method == InvoiceMethod::INVOICE_NOTIFY )
  686. {
  687. // 3-1過濾不需要的項目
  688. unset($aSend_Info['RelateNumber']) ;
  689. unset($aSend_Info['CustomerID']) ;
  690. unset($aSend_Info['CustomerIdentifier']) ;
  691. unset($aSend_Info['CustomerName']) ;
  692. unset($aSend_Info['CustomerAddr']) ;
  693. unset($aSend_Info['CustomerPhone']) ;
  694. unset($aSend_Info['CustomerEmail']) ;
  695. unset($aSend_Info['ClearanceMark']) ;
  696. unset($aSend_Info['Print']) ;
  697. unset($aSend_Info['Donation']) ;
  698. unset($aSend_Info['LoveCode']) ;
  699. unset($aSend_Info['CarruerType']) ;
  700. unset($aSend_Info['CarruerNum']) ;
  701. unset($aSend_Info['TaxType']) ;
  702. unset($aSend_Info['SalesAmount']) ;
  703. unset($aSend_Info['InvoiceRemark']) ;
  704. unset($aSend_Info['ItemName']) ;
  705. unset($aSend_Info['ItemCount']) ;
  706. unset($aSend_Info['ItemWord']) ;
  707. unset($aSend_Info['ItemPrice']) ;
  708. unset($aSend_Info['ItemTaxType']) ;
  709. unset($aSend_Info['ItemAmount']) ;
  710. unset($aSend_Info['InvType']) ;
  711. unset($aSend_Info['InvCreateDate']) ;
  712. unset($aSend_Info['vat']) ;
  713. unset($aSend_Info['DelayFlag']) ;
  714. unset($aSend_Info['DelayDay']) ;
  715. unset($aSend_Info['ECBankID']) ;
  716. unset($aSend_Info['Tsr']) ;
  717. unset($aSend_Info['PayType']) ;
  718. unset($aSend_Info['PayAct']) ;
  719. unset($aSend_Info['NotifyURL']) ;
  720. unset($aSend_Info['AllowanceNotify']) ;
  721. unset($aSend_Info['NotifyPhone']) ;
  722. unset($aSend_Info['AllowanceAmount']) ;
  723. unset($aSend_Info['InvoiceNumber']) ;
  724. unset($aSend_Info['Reason']) ;
  725. // 3-2商品資訊組合
  726. unset($aSend_Info['Items']) ;
  727. // 3-3產生檢查碼
  728. $aSend_CheckMac_Info = $aSend_Info ;
  729. // 產生檢查碼
  730. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  731. }
  732. // K.查詢折讓明細
  733. if( $this->Invoice_Method == InvoiceMethod::INVOICE_TRIGGER )
  734. {
  735. // 3-1過濾不需要的項目
  736. unset($aSend_Info['RelateNumber']) ;
  737. unset($aSend_Info['CustomerID']) ;
  738. unset($aSend_Info['CustomerIdentifier']) ;
  739. unset($aSend_Info['CustomerName']) ;
  740. unset($aSend_Info['CustomerAddr']) ;
  741. unset($aSend_Info['CustomerPhone']) ;
  742. unset($aSend_Info['CustomerEmail']) ;
  743. unset($aSend_Info['ClearanceMark']) ;
  744. unset($aSend_Info['Print']) ;
  745. unset($aSend_Info['Donation']) ;
  746. unset($aSend_Info['LoveCode']) ;
  747. unset($aSend_Info['CarruerType']) ;
  748. unset($aSend_Info['CarruerNum']) ;
  749. unset($aSend_Info['TaxType']) ;
  750. unset($aSend_Info['SalesAmount']) ;
  751. unset($aSend_Info['InvoiceRemark']) ;
  752. unset($aSend_Info['ItemName']) ;
  753. unset($aSend_Info['ItemCount']) ;
  754. unset($aSend_Info['ItemWord']) ;
  755. unset($aSend_Info['ItemPrice']) ;
  756. unset($aSend_Info['ItemTaxType']) ;
  757. unset($aSend_Info['ItemAmount']) ;
  758. unset($aSend_Info['InvType']) ;
  759. unset($aSend_Info['InvCreateDate']) ;
  760. unset($aSend_Info['vat']) ;
  761. unset($aSend_Info['DelayFlag']) ;
  762. unset($aSend_Info['DelayDay']) ;
  763. unset($aSend_Info['ECBankID']) ;
  764. unset($aSend_Info['PayAct']) ;
  765. unset($aSend_Info['NotifyURL']) ;
  766. unset($aSend_Info['InvoiceNo']) ;
  767. unset($aSend_Info['AllowanceNotify']) ;
  768. unset($aSend_Info['NotifyMail']) ;
  769. unset($aSend_Info['NotifyPhone']) ;
  770. unset($aSend_Info['AllowanceAmount']) ;
  771. unset($aSend_Info['InvoiceNumber']) ;
  772. unset($aSend_Info['Reason']) ;
  773. unset($aSend_Info['AllowanceNo']) ;
  774. unset($aSend_Info['Phone']) ;
  775. unset($aSend_Info['Notify']) ;
  776. unset($aSend_Info['InvoiceTag']) ;
  777. unset($aSend_Info['Notified']) ;
  778. // 3-2商品資訊組合
  779. unset($aSend_Info['Items']) ;
  780. // 3-3產生檢查碼
  781. $aSend_CheckMac_Info = $aSend_Info ;
  782. // 產生檢查碼
  783. $aSend_Info['CheckMacValue'] = $this->GenerateCheckMacValue($aSend_CheckMac_Info) ;
  784. }
  785. // 4.送出資訊
  786. if(true)
  787. {
  788. $aReturn_Info = $this->ServerPost($aSend_Info);
  789. }
  790. // 5.狀態回傳驗證
  791. if(true)
  792. {
  793. // A. B. C. D. E.
  794. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY || $this->Invoice_Method == InvoiceMethod::ALLOWANCE || $this->Invoice_Method == InvoiceMethod::INVOICE_VOID || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID)
  795. {
  796. // 5-1 CheckMac檢查
  797. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  798. {
  799. $aReturn_CheckMacValue = $aReturn_Info ;
  800. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  801. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  802. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  803. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  804. {
  805. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  806. }
  807. }
  808. else
  809. {
  810. // 傳出參數錯誤,查無資料
  811. array_push($arErrors, 'Error:10100050');
  812. }
  813. }
  814. // F.查詢發票
  815. if( $this->Invoice_Method == InvoiceMethod::INVOICE_SEARCH )
  816. {
  817. // 5-1 CheckMac檢查
  818. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  819. {
  820. $aReturn_CheckMacValue = $aReturn_Info ;
  821. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  822. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  823. unset($aReturn_CheckMacValue['ItemName']) ;
  824. unset($aReturn_CheckMacValue['ItemWord']) ;
  825. unset($aReturn_CheckMacValue['InvoiceRemark']) ;
  826. if($aReturn_Info['RtnCode'] == 1)
  827. {
  828. $aReturn_CheckMacValue['IIS_Customer_Name'] = $this->Replace_Symbol(urlencode($aReturn_CheckMacValue['IIS_Customer_Name']));
  829. $aReturn_CheckMacValue['IIS_Customer_Addr'] = $this->Replace_Symbol(urlencode($aReturn_CheckMacValue['IIS_Customer_Addr']));
  830. $aReturn_CheckMacValue['IIS_Customer_Email'] = $this->Replace_Symbol(urlencode($aReturn_CheckMacValue['IIS_Customer_Email']));
  831. // EMIAL項目的@不做urlencode還原回來
  832. $aReturn_CheckMacValue['IIS_Customer_Email'] = str_replace('%40', '@', $aReturn_CheckMacValue['IIS_Customer_Email']);
  833. }
  834. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  835. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  836. {
  837. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  838. }
  839. }
  840. else
  841. {
  842. // 傳出參數錯誤,查無資料
  843. array_push($arErrors, 'Error:10100050');
  844. }
  845. }
  846. // G.查詢作廢發票
  847. if( $this->Invoice_Method == InvoiceMethod::INVOICE_VOID_SEARCH )
  848. {
  849. // 5-1 CheckMac檢查
  850. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  851. {
  852. $aReturn_CheckMacValue = $aReturn_Info ;
  853. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  854. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  855. unset($aReturn_CheckMacValue['Reason']) ;
  856. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  857. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  858. {
  859. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  860. }
  861. }
  862. else
  863. {
  864. // 傳出參數錯誤,查無資料
  865. array_push($arErrors, 'Error:10100050');
  866. }
  867. }
  868. // H.查詢折讓明細
  869. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE_SEARCH )
  870. {
  871. // 5-1 CheckMac檢查
  872. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  873. {
  874. $aReturn_CheckMacValue = $aReturn_Info ;
  875. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  876. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  877. unset($aReturn_CheckMacValue['ItemName']) ;
  878. unset($aReturn_CheckMacValue['ItemWord']) ;
  879. $aReturn_CheckMacValue['IIS_Customer_Name'] = $this->Replace_Symbol(urlencode($aReturn_CheckMacValue['IIS_Customer_Name'])) ;
  880. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  881. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  882. {
  883. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  884. }
  885. }
  886. else
  887. {
  888. // 傳出參數錯誤,查無資料
  889. array_push($arErrors, 'Error:10100050');
  890. }
  891. }
  892. // I.查詢折讓作廢明細
  893. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID_SEARCH )
  894. {
  895. // 5-1 CheckMac檢查
  896. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  897. {
  898. $aReturn_CheckMacValue = $aReturn_Info ;
  899. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  900. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  901. unset($aReturn_CheckMacValue['Reason']) ;
  902. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  903. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  904. {
  905. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  906. }
  907. }
  908. else
  909. {
  910. // 傳出參數錯誤,查無資料
  911. array_push($arErrors, 'Error:10100050');
  912. }
  913. }
  914. // J.發送通知
  915. if( $this->Invoice_Method == InvoiceMethod::INVOICE_NOTIFY )
  916. {
  917. // 5-1 CheckMac檢查
  918. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  919. {
  920. $aReturn_CheckMacValue = $aReturn_Info ;
  921. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  922. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  923. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  924. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  925. {
  926. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  927. }
  928. }
  929. else
  930. {
  931. // 傳出參數錯誤,查無資料
  932. array_push($arErrors, 'Error:10100050');
  933. }
  934. }
  935. // K.付款完成觸發或延遲開立發票
  936. if( $this->Invoice_Method == InvoiceMethod::INVOICE_TRIGGER )
  937. {
  938. // 5-1 CheckMac檢查
  939. if(count($aReturn_Info) > 0 && isset($aReturn_Info['CheckMacValue']))
  940. {
  941. $aReturn_CheckMacValue = $aReturn_Info ;
  942. $sReturn_CheckMacValue = $aReturn_Info['CheckMacValue'] ;
  943. unset($aReturn_CheckMacValue['CheckMacValue']) ;
  944. $sCheckMacValueGen = $this->GenerateCheckMacValue($aReturn_CheckMacValue) ;
  945. if($sCheckMacValueGen != $sReturn_CheckMacValue )
  946. {
  947. array_push($arErrors, '1000001 CheckMacValue verify fail.');
  948. }
  949. }
  950. else
  951. {
  952. // 傳出參數錯誤,查無資料
  953. array_push($arErrors, 'Error:10100050');
  954. }
  955. }
  956. return (count($arErrors) > 0) ? $arErrors : $aReturn_Info ;
  957. }
  958. }
  959. exit;
  960. }
  961. /**
  962. * 檢查各個參數是否符合規格
  963. */
  964. function Check_String()
  965. {
  966. $arErrors = array();
  967. // 檢查是否傳入動作方式
  968. if($this->Invoice_Method == '' || $this->Invoice_Method == 'Invoice_Method')
  969. {
  970. array_push($arErrors, 'Invoice_Method is required.');
  971. }
  972. // 檢查是否有傳入MerchantID
  973. if(strlen($this->MerchantID) == 0)
  974. {
  975. array_push($arErrors, 'MerchantID is required.');
  976. }
  977. if(strlen($this->MerchantID) > 10)
  978. {
  979. array_push($arErrors, 'MerchantID max langth as 10.');
  980. }
  981. // 檢查是否有傳入HashKey
  982. if(strlen($this->HashKey) == 0)
  983. {
  984. array_push($arErrors, 'HashKey is required.');
  985. }
  986. // 檢查是否有傳入HashIV
  987. if(strlen($this->HashIV) == 0)
  988. {
  989. array_push($arErrors, 'HashIV is required.');
  990. }
  991. // 檢查是否有傳送網址
  992. if(strlen($this->Invoice_Url) == 0)
  993. {
  994. array_push($arErrors, 'Invoice_Url is required.');
  995. }
  996. // 檢查 A.一般開立發票、B.延遲觸發開立發票、F.查詢發票、G.查詢作廢發票
  997. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY || $this->Invoice_Method == InvoiceMethod::INVOICE_SEARCH || $this->Invoice_Method == InvoiceMethod::INVOICE_VOID_SEARCH)
  998. {
  999. // 4.廠商自訂編號
  1000. // *預設不可為空值
  1001. if(strlen($this->Send['RelateNumber']) == 0)
  1002. {
  1003. array_push($arErrors, '4:RelateNumber is required.');
  1004. }
  1005. // *預設最大長度為30碼
  1006. if(strlen($this->Send['RelateNumber']) > 30)
  1007. {
  1008. array_push($arErrors, '4:RelateNumber max langth as 30.');
  1009. }
  1010. }
  1011. // 檢查 A.一般開立發票、B.延遲觸發開立發票
  1012. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  1013. {
  1014. // 5.客戶代號 CustomerID
  1015. // *載具類別為1 則客戶代號需有值
  1016. if($this->Send['CarruerType'] == 1)
  1017. {
  1018. if(strlen($this->Send['CustomerID']) == 0 )
  1019. {
  1020. array_push($arErrors, '5:CustomerID is required.');
  1021. }
  1022. }
  1023. // *預設最大長度為20碼
  1024. if(strlen($this->Send['CustomerID']) > 20 )
  1025. {
  1026. array_push($arErrors, '5:CustomerID max langth as 20.');
  1027. }
  1028. // *比對客戶代號 只接受英、數字與下底線格式
  1029. if(strlen($this->Send['CustomerID']) > 0)
  1030. {
  1031. if( !preg_match('/^[a-zA-Z0-9_]+$/', $this->Send['CustomerID']) )
  1032. {
  1033. arRay_push($arErrors, '5:Invalid CustomerID.');
  1034. }
  1035. }
  1036. // 6.統一編號判斷 CustomerIdentifier
  1037. // *若統一編號有值時,則固定長度為數字8碼
  1038. if( strlen( $this->Send['CustomerIdentifier'] ) > 0 )
  1039. {
  1040. if( !preg_match('/^[0-9]{8}$/', $this->Send['CustomerIdentifier']) )
  1041. {
  1042. array_push($arErrors, '6:CustomerIdentifier length should be 8.');
  1043. }
  1044. }
  1045. }
  1046. // 檢查 C.開立折讓
  1047. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE )
  1048. {
  1049. // 7.客戶名稱 CustomerName
  1050. // x僅能為中英數字格式
  1051. // *預設最大長度為30碼
  1052. if( mb_strlen($this->Send['CustomerName'], 'UTF-8') > 30)
  1053. {
  1054. array_push($arErrors, '7:CustomerName max length as 30.');
  1055. }
  1056. // *UrlEncode
  1057. $this->Send['CustomerName'] = urlencode($this->Send['CustomerName']);
  1058. $this->Send['CustomerName'] = $this->Replace_Symbol($this->Send['CustomerName']) ;
  1059. }
  1060. // 檢查 A.一般開立發票、B.延遲觸發開立發票
  1061. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  1062. {
  1063. // 7.客戶名稱 CustomerName
  1064. // x僅能為中英數字格式
  1065. // *若列印註記 = '1' (列印)時,則客戶名稱須有值
  1066. if ($this->Send['Print'] == PrintMark::Yes)
  1067. {
  1068. if (mb_strlen($this->Send['CustomerName'], 'UTF-8') == 0)
  1069. {
  1070. array_push($arErrors, "7:CustomerName is required.");
  1071. }
  1072. }
  1073. // *預設最大長度為30碼
  1074. if( mb_strlen($this->Send['CustomerName'], 'UTF-8') > 30)
  1075. {
  1076. array_push($arErrors, '7:CustomerName max length as 30.');
  1077. }
  1078. // *UrlEncode
  1079. $this->Send['CustomerName'] = urlencode($this->Send['CustomerName']);
  1080. $this->Send['CustomerName'] = $this->Replace_Symbol($this->Send['CustomerName']) ;
  1081. // 8.客戶地址 CustomerAddr(UrlEncode, 預設為空字串)
  1082. // *若列印註記 = '1' (列印)時,則客戶地址須有值
  1083. if ($this->Send['Print'] == PrintMark::Yes)
  1084. {
  1085. if (mb_strlen($this->Send['CustomerAddr'], 'UTF-8') == 0)
  1086. {
  1087. array_push($arErrors, "8:CustomerAddr is required.");
  1088. }
  1089. }
  1090. // *預設最大長度為100碼
  1091. if (mb_strlen($this->Send['CustomerAddr'], 'UTF-8') > 100)
  1092. {
  1093. array_push($arErrors, "8:CustomerAddr max length as 100.");
  1094. }
  1095. // *UrlEncode
  1096. $this->Send['CustomerAddr'] = urlencode($this->Send['CustomerAddr']);
  1097. $this->Send['CustomerAddr'] = $this->Replace_Symbol($this->Send['CustomerAddr']) ;
  1098. // 9.客戶手機號碼 CustomerPhone
  1099. // *預設最小長度為1碼,最大長度為20碼
  1100. if (strlen($this->Send['CustomerPhone']) > 20)
  1101. {
  1102. array_push($arErrors, "9:CustomerPhone max length as 20.");
  1103. }
  1104. // *預設格式為數字組成
  1105. if (strlen($this->Send['CustomerPhone']) > 0)
  1106. {
  1107. if( !preg_match('/^[0-9]*$/', $this->Send['CustomerPhone']) )
  1108. {
  1109. array_push($arErrors, '9:Invalid CustomerPhone.');
  1110. }
  1111. }
  1112. // 10.客戶電子信箱 CustomerEmail(UrlEncode, 預設為空字串, 與CustomerPhone擇一不可為空)
  1113. // *預設最大長度為80碼
  1114. if (strlen($this->Send['CustomerEmail']) > 80)
  1115. {
  1116. array_push($arErrors, "10:CustomerEmail max length as 80.");
  1117. }
  1118. // *若客戶電子信箱有值時,則格式僅能為Email的標準格式
  1119. if(strlen($this->Send['CustomerEmail']) > 0 )
  1120. {
  1121. if ( !preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is', $this->Send['CustomerEmail']) )
  1122. {
  1123. array_push($arErrors, '10:Invalid CustomerEmail Format.');
  1124. }
  1125. }
  1126. // *UrlEncode
  1127. $this->Send['CustomerEmail'] = urlencode($this->Send['CustomerEmail']);
  1128. $this->Send['CustomerEmail'] = $this->Replace_Symbol($this->Send['CustomerEmail']) ;
  1129. // 9. 10.
  1130. // *若客戶手機號碼為空值時,則客戶電子信箱不可為空值
  1131. if (strlen($this->Send['CustomerPhone']) == 0 && strlen($this->Send['CustomerEmail']) == 0)
  1132. {
  1133. array_push($arErrors, "9-10:CustomerPhone or CustomerEmail is required.");
  1134. }
  1135. // 11.通關方式 ClearanceMark(預設為空字串)
  1136. // *最多1字元
  1137. if (strlen($this->Send['ClearanceMark']) > 1)
  1138. {
  1139. array_push($arErrors, "11:ClearanceMark max length as 1.");
  1140. }
  1141. // *請設定空字串,僅課稅類別為零稅率(Zero)時,此參數不可為空字串
  1142. if ($this->Send['TaxType'] == TaxType::Zero)
  1143. {
  1144. if ( ( $this->Send['ClearanceMark'] != ClearanceMark::Yes ) && ( $this->Send['ClearanceMark'] != ClearanceMark::No ) )
  1145. {
  1146. array_push($arErrors, "11:ClearanceMark is required.");
  1147. }
  1148. }
  1149. else
  1150. {
  1151. if (strlen($this->Send['ClearanceMark']) > 0)
  1152. {
  1153. array_push($arErrors, "11:Please remove ClearanceMark.");
  1154. }
  1155. }
  1156. // 12.列印註記 Print(預設為No)
  1157. // *列印註記僅能為 0 或 1
  1158. if ( ( $this->Send['Print'] != PrintMark::Yes ) && ( $this->Send['Print'] != PrintMark::No ) )
  1159. {
  1160. array_push($arErrors, "12:Invalid Print.");
  1161. }
  1162. // *若捐贈註記 = '1' (捐贈)時,則VAL = '0' (不列印)
  1163. if ($this->Send['Donation'] == Donation::Yes)
  1164. {
  1165. if ($this->Send['Print'] != PrintMark::No)
  1166. {
  1167. array_push($arErrors, "12:Donation Print should be No.");
  1168. }
  1169. }
  1170. // *若統一編號有值時,則VAL = '1' (列印)
  1171. if (strlen($this->Send['CustomerIdentifier']) > 0)
  1172. {
  1173. if ($this->Send['Print'] != PrintMark::Yes)
  1174. {
  1175. array_push($arErrors, "12:CustomerIdentifier Print should be Yes.");
  1176. }
  1177. }
  1178. // 13.捐贈註記 Donation
  1179. // *固定給定下述預設值若為捐贈時,則VAL = '1',若為不捐贈時,則VAL = '2'
  1180. if ( ($this->Send['Donation'] != Donation::Yes ) && ( $this->Send['Donation'] != Donation::No ) )
  1181. {
  1182. array_push($arErrors, "13:Invalid Donation.");
  1183. }
  1184. // *若統一編號有值時,則VAL = '2' (不捐贈)
  1185. if (strlen($this->Send['CustomerIdentifier']) > 0 && $this->Send['Donation'] == Donation::Yes )
  1186. {
  1187. array_push($arErrors, "13:CustomerIdentifier Donation should be No.");
  1188. }
  1189. // 14.愛心碼 LoveCode(預設為空字串)
  1190. // *若捐贈註記 = '1' (捐贈)時,則須有值
  1191. if ($this->Send['Donation'] == Donation::Yes)
  1192. {
  1193. if ( !preg_match('/^([xX]{1}[0-9]{2,6}|[0-9]{3,7})$/', $this->Send['LoveCode']) )
  1194. {
  1195. array_push($arErrors, "14:Invalid LoveCode.");
  1196. }
  1197. }
  1198. else
  1199. {
  1200. if (strlen($this->Send['LoveCode']) > 0)
  1201. {
  1202. array_push($arErrors, "14:Please remove LoveCode.");
  1203. }
  1204. }
  1205. // 15.載具類別 CarruerType(預設為None)
  1206. // *固定給定下述預設值None、Member、Cellphone
  1207. if ( ( $this->Send['CarruerType'] != CarruerType::None ) && ( $this->Send['CarruerType'] != CarruerType::Member ) && ( $this->Send['CarruerType'] != CarruerType::Citizen ) && ( $this->Send['CarruerType'] != CarruerType::Cellphone ) )
  1208. {
  1209. array_push($arErrors, "15:Invalid CarruerType.");
  1210. }
  1211. else
  1212. {
  1213. // *統一編號不為空字串時,則載具類別不可為會載具或自然人憑證載具
  1214. if (strlen($this->Send['CustomerIdentifier']) > 0)
  1215. {
  1216. if ($this->Send['CarruerType'] == CarruerType::Member || $this->Send['CarruerType'] == CarruerType::Citizen )
  1217. {
  1218. array_push($arErrors, "15:Invalid CarruerType.");
  1219. }
  1220. }
  1221. }
  1222. // 16.載具編號 CarruerNum(預設為空字串)
  1223. switch ($this->Send['CarruerType'])
  1224. {
  1225. // *載具類別為無載具(None)或會員載具(Member)時,請設定空字串
  1226. case CarruerType::None:
  1227. case CarruerType::Member:
  1228. if (strlen($this->Send['CarruerNum']) > 0)
  1229. {
  1230. array_push($arErrors, "16:Please remove CarruerNum.");
  1231. }
  1232. break;
  1233. // *載具類別為買受人自然人憑證(Citizen)時,請設定自然人憑證號碼,前2碼為大小寫英文,後14碼為數字
  1234. case CarruerType::Citizen:
  1235. if ( !preg_match('/^[a-zA-Z]{2}\d{14}$/', $this->Send['CarruerNum']) )
  1236. {
  1237. array_push($arErrors, "16:Invalid CarruerNum.");
  1238. }
  1239. break;
  1240. // *載具類別為買受人手機條碼(Cellphone)時,請設定手機條碼,第1碼為「/」,後7碼為大小寫英文、數字、「+」、「-」或「.」
  1241. case CarruerType::Cellphone:
  1242. if ( !preg_match('/^\/{1}[0-9a-zA-Z+-.]{7}$/', $this->Send['CarruerNum']) )
  1243. {
  1244. array_push($arErrors, "16:Invalid CarruerNum.");
  1245. }
  1246. break;
  1247. default:
  1248. array_push($arErrors, "16:Please remove CarruerNum.");
  1249. }
  1250. // 17.課稅類別 TaxType(不可為空)
  1251. // *不可為空
  1252. if (strlen($this->Send['TaxType']) == 0)
  1253. {
  1254. array_push($arErrors, "17:TaxType is required.");
  1255. }
  1256. // *僅能為 1應稅 2零稅率 3免稅 9.應稅與免稅混合
  1257. if ( ( $this->Send['TaxType'] != TaxType::Dutiable ) && ( $this->Send['TaxType'] != TaxType::Zero ) && ( $this->Send['TaxType'] != TaxType::Free ) && ( $this->Send['TaxType'] != TaxType::Mix ) )
  1258. {
  1259. array_push($arErrors, "17:Invalid TaxType.");
  1260. }
  1261. // 18.發票金額 SalesAmount
  1262. // *不可為空
  1263. if (strlen($this->Send['SalesAmount']) == 0)
  1264. {
  1265. array_push($arErrors, "18:SalesAmount is required.");
  1266. }
  1267. // 19.備註 InvoiceRemark
  1268. // *請將參數值做UrlEncode
  1269. if (strlen($this->Send['InvoiceRemark']) != 0)
  1270. {
  1271. // *UrlEncode
  1272. $this->Send['InvoiceRemark'] = urlencode($this->Send['InvoiceRemark']);
  1273. $this->Send['InvoiceRemark'] = $this->Replace_Symbol($this->Send['InvoiceRemark']) ;
  1274. }
  1275. }
  1276. // 檢查 A.一般開立發票、B.延遲觸發開立發票、C.開立折讓
  1277. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY || $this->Invoice_Method == InvoiceMethod::ALLOWANCE )
  1278. {
  1279. // 20.21.22.23.24.25. 商品資訊
  1280. // *不可為空
  1281. if (sizeof($this->Send['Items']) == 0)
  1282. {
  1283. array_push($arErrors, '20-25:Items is required.');
  1284. }
  1285. else
  1286. {
  1287. // 檢查是否存在保留字元 '|'
  1288. $bFind_Tag = true;
  1289. $bError_Tag = false;
  1290. foreach($this->Send['Items'] as $key => $value)
  1291. {
  1292. $bFind_Tag = strpos($value['ItemName'], '|') ;
  1293. if($bFind_Tag != false || empty($value['ItemName']))
  1294. {
  1295. $bError_Tag = true ;
  1296. array_push($arErrors, '20-25:Invalid ItemName.');
  1297. break;
  1298. }
  1299. $bFind_Tag = strpos($value['ItemCount'], '|') ;
  1300. if($bFind_Tag != false || empty($value['ItemCount']))
  1301. {
  1302. $bError_Tag = true ;
  1303. array_push($arErrors, '20-25:Invalid ItemCount.');
  1304. break;
  1305. }
  1306. $bFind_Tag = strpos($value['ItemWord'], '|') ;
  1307. if($bFind_Tag != false || empty($value['ItemWord']))
  1308. {
  1309. $bError_Tag = true ;
  1310. array_push($arErrors, '20-25:Invalid ItemWord.');
  1311. break;
  1312. }
  1313. $bFind_Tag = strpos($value['ItemPrice'], '|') ;
  1314. if($bFind_Tag != false || empty($value['ItemPrice']))
  1315. {
  1316. $bError_Tag = true ;
  1317. array_push($arErrors, '20-25:Invalid ItemPrice.');
  1318. break;
  1319. }
  1320. $bFind_Tag = strpos($value['ItemTaxType'], '|') ;
  1321. if($bFind_Tag != false || empty($value['ItemTaxType']))
  1322. {
  1323. $bError_Tag = true ;
  1324. array_push($arErrors, '20-25:Invalid ItemTaxType.');
  1325. break;
  1326. }
  1327. $bFind_Tag = strpos($value['ItemAmount'], '|') ;
  1328. if($bFind_Tag != false || empty($value['ItemAmount']))
  1329. {
  1330. $bError_Tag = true ;
  1331. array_push($arErrors, '20-25:Invalid ItemAmount.');
  1332. break;
  1333. }
  1334. }
  1335. // 檢查商品格式
  1336. if(!$bError_Tag)
  1337. {
  1338. foreach($this->Send['Items'] as $key => $value)
  1339. {
  1340. // *ItemCount數字判斷
  1341. if ( !preg_match('/^[0-9]*$/', $value['ItemCount']) )
  1342. {
  1343. array_push($arErrors, '20-25:Invalid ItemCount.');
  1344. }
  1345. // *ItemWord 預設最大長度為6碼
  1346. if (strlen($value['ItemWord']) > 6 )
  1347. {
  1348. array_push($arErrors, '20-25:ItemWord max length as 6.');
  1349. }
  1350. // *ItemPrice數字判斷
  1351. if ( !preg_match('/^[0-9]*$/', $value['ItemPrice']) )
  1352. {
  1353. array_push($arErrors, '20-25:Invalid ItemPrice.');
  1354. }
  1355. // *ItemAmount數字判斷
  1356. if ( !preg_match('/^[0-9]*$/', $value['ItemAmount']) )
  1357. {
  1358. array_push($arErrors, '20-25:Invalid ItemAmount.');
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. // 檢查 A.一般開立發票、B.延遲觸發開立發票
  1365. if( $this->Invoice_Method == InvoiceMethod::INVOICE || $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  1366. {
  1367. // 27.字軌類別
  1368. // *InvType(不可為空) 僅能為 07 或 08 狀態
  1369. if( ( $this->Send['InvType'] != InvType::General ) && ( $this->Send['InvType'] != InvType::Special ) )
  1370. {
  1371. array_push($arErrors, "27:Invalid InvType.");
  1372. }
  1373. }
  1374. // 檢查 A.一般開立發票
  1375. if( $this->Invoice_Method == InvoiceMethod::INVOICE )
  1376. {
  1377. // 28.發票開立時間
  1378. // *UrlEncode
  1379. $this->Send['InvCreateDate'] = urlencode($this->Send['InvCreateDate']);
  1380. $this->Send['InvCreateDate'] = $this->Replace_Symbol($this->Send['InvCreateDate']) ;
  1381. // 29.商品單價是否含稅(預設為含稅價)
  1382. // *固定給定下述預設值 若為含稅價,則VAL = '1'
  1383. if(!empty($this->Send['vat']))
  1384. {
  1385. if( ( $this->Send['vat'] != VatType::Yes ) && ( $this->Send['vat'] != VatType::No ) )
  1386. {
  1387. array_push($arErrors, "29:Invalid VatType.");
  1388. }
  1389. }
  1390. }
  1391. // 檢查 B.延遲觸發開立發票
  1392. if( $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  1393. {
  1394. // 30.延遲註記 DelayFlag
  1395. if( ( $this->Send['DelayFlag'] != DelayFlagType::Delay ) && ( $this->Send['DelayFlag'] != DelayFlagType::Trigger ) )
  1396. {
  1397. array_push($arErrors, "30:Invalid DelayFlagType.");
  1398. }
  1399. // 31.延遲天數 DelayDay
  1400. // 延遲天數,範圍0~15,設定為0時,付款完成後立即開立發票
  1401. // *DelayDay(不可為空, 預設為0)
  1402. $this->Send['DelayDay'] = (int)$this->Send['DelayDay'];
  1403. // *若為延遲開立時,延遲天數須介於1至15天內
  1404. if ( $this->Send['DelayFlag'] == DelayFlagType::Delay )
  1405. {
  1406. if ($this->Send['DelayDay'] < 1 || $this->Send['DelayDay'] > 15)
  1407. {
  1408. array_push($arErrors, "31:DelayDay should be 1 ~ 15.");
  1409. }
  1410. }
  1411. // *若為觸發開立時,延遲天數須介於0至15天內
  1412. if ($this->Send['DelayFlag'] == DelayFlagType::Trigger)
  1413. {
  1414. if ($this->Send['DelayDay'] < 0 || $this->Send['DelayDay'] > 15)
  1415. {
  1416. array_push($arErrors, "31:DelayDay should be 0 ~ 15.");
  1417. }
  1418. }
  1419. }
  1420. // 檢查 B.延遲觸發開立發票、K.付款完成觸發或延遲開立發票
  1421. if( $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY || $this->Invoice_Method == InvoiceMethod::INVOICE_TRIGGER)
  1422. {
  1423. // 33.交易單號 Tsr
  1424. // *必填項目
  1425. if(strlen($this->Send['Tsr']) == 0 )
  1426. {
  1427. array_push($arErrors, '33:Tsr is required.');
  1428. }
  1429. // *判斷最大字元是否超過30字
  1430. if (strlen($this->Send['Tsr']) > 30)
  1431. {
  1432. array_push($arErrors, '33:Tsr max length as 30.');
  1433. }
  1434. // 34.交易類別 PayType
  1435. // *僅允許 1.ECBANK 2.ECPAY 3.ALLPAY
  1436. if( ( $this->Send['PayType'] != PayTypeCategory::Ecbank ) && ( $this->Send['PayType'] != PayTypeCategory::Ecpay ) && ( $this->Send['PayType'] != PayTypeCategory::Allpay ) )
  1437. {
  1438. array_push($arErrors, "34:Invalid PayType.");
  1439. }
  1440. }
  1441. // 檢查 B.延遲觸發開立發票
  1442. if( $this->Invoice_Method == InvoiceMethod::INVOICE_DELAY )
  1443. {
  1444. // 35.交易類別名稱 PayAct
  1445. // *固定給定下述預設值
  1446. switch ($this->Send['PayType'])
  1447. {
  1448. // *若交易類別='1'時,則VAL = 'ECBANK'
  1449. case PayTypeCategory::Ecbank:
  1450. $this->Send['PayAct'] = 'ECBANK' ;
  1451. break;
  1452. // *若交易類別='2'時,則VAL = 'ECPAY'
  1453. case PayTypeCategory::Ecpay:
  1454. $this->Send['PayAct'] = 'ECPAY' ;
  1455. break;
  1456. // *若交易類別='3'時,則VAL = 'ALLPAY'
  1457. case PayTypeCategory::Allpay:
  1458. $this->Send['PayAct'] = 'ALLPAY' ;
  1459. break;
  1460. default:
  1461. $this->Send['PayAct'] = '' ;
  1462. }
  1463. // *必填項目 交易類別名稱預設不能為空值
  1464. if(strlen($this->Send['PayAct']) == 0 )
  1465. {
  1466. array_push($arErrors, '35:PayAct is required.');
  1467. }
  1468. }
  1469. // 檢查 C.開立折讓、E.折讓作廢、H.查詢折讓明細、I.查詢折讓作廢明細、J.發送通知
  1470. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_SEARCH || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID_SEARCH || $this->Invoice_Method == InvoiceMethod::INVOICE_NOTIFY )
  1471. {
  1472. // 37.發票號碼 InvoiceNo
  1473. // *必填項目
  1474. if(strlen($this->Send['InvoiceNo']) == 0 )
  1475. {
  1476. array_push($arErrors, '37:InvoiceNo is required.');
  1477. }
  1478. // *預設長度固定10碼
  1479. if (strlen($this->Send['InvoiceNo']) != 10)
  1480. {
  1481. array_push($arErrors, '37:InvoiceNo length as 10.');
  1482. }
  1483. }
  1484. // 檢查 C.開立折讓
  1485. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE )
  1486. {
  1487. // 38.通知類別 AllowanceNotify
  1488. // *固定給定下述預設值
  1489. if( ( $this->Send['AllowanceNotify'] != AllowanceNotifyType::Sms ) && ( $this->Send['AllowanceNotify'] != AllowanceNotifyType::Email ) && ( $this->Send['AllowanceNotify'] != AllowanceNotifyType::All ) && ( $this->Send['AllowanceNotify'] != AllowanceNotifyType::None ) )
  1490. {
  1491. array_push($arErrors, "38:Invalid AllowanceNotifyType.");
  1492. }
  1493. // 39.通知電子信箱 NotifyMail
  1494. // *若客戶電子信箱有值時,則格式僅能為Email的標準格式
  1495. if(strlen($this->Send['NotifyMail']) > 0 )
  1496. {
  1497. if ( !preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is', $this->Send['NotifyMail'] ) )
  1498. {
  1499. array_push($arErrors, '39:Invalid Email Format.');
  1500. }
  1501. }
  1502. // *下述情況通知電子信箱不可為空值(通知類別為E-電子郵件)
  1503. if($this->Send['AllowanceNotify'] == AllowanceNotifyType::Email && strlen($this->Send['NotifyMail']) == 0 )
  1504. {
  1505. array_push($arErrors, "39:NotifyMail is required.");
  1506. }
  1507. // *UrlEncode
  1508. $this->Send['NotifyMail'] = urlencode($this->Send['NotifyMail']);
  1509. $this->Send['NotifyMail'] = $this->Replace_Symbol($this->Send['NotifyMail']) ;
  1510. // 40.通知手機號碼 NotifyPhone
  1511. // *若客戶手機號碼有值時,則預設格式為數字組成
  1512. if(strlen($this->Send['NotifyPhone']) > 0 )
  1513. {
  1514. if ( !preg_match('/^[0-9]*$/', $this->Send['NotifyPhone']) )
  1515. {
  1516. array_push($arErrors, '40:Invalid NotifyPhone.');
  1517. }
  1518. }
  1519. // * 最大20字元
  1520. if (strlen($this->Send['NotifyPhone']) > 20)
  1521. {
  1522. array_push($arErrors, '40:NotifyPhone max length as 20.');
  1523. }
  1524. // *下述情況通知手機號碼不可為空值(通知類別為S-簡訊)
  1525. if( $this->Send['AllowanceNotify'] == AllowanceNotifyType::Sms && strlen($this->Send['NotifyPhone']) == 0 )
  1526. {
  1527. array_push($arErrors, "40:NotifyPhone is required.");
  1528. }
  1529. // 39-40 通知電子信箱、通知手機號碼不能全為空值 (如果狀態為SMS 或 EMAIL)
  1530. if(strlen($this->Send['NotifyPhone']) == 0 && strlen($this->Send['NotifyMail']) == 0 && ( $this->Send['AllowanceNotify'] == AllowanceNotifyType::Sms || $this->Send['AllowanceNotify'] == AllowanceNotifyType::Email ) )
  1531. {
  1532. array_push($arErrors, "39-40:NotifyMail or NotifyPhone is required.");
  1533. }
  1534. else
  1535. {
  1536. // *下述情況通知手機號碼與電子信箱不可為空值(通知類別為A-皆通知)
  1537. if( $this->Send['AllowanceNotify'] == AllowanceNotifyType::All && ( strlen($this->Send['NotifyMail']) == 0 || strlen($this->Send['NotifyPhone']) == 0 ) )
  1538. {
  1539. array_push($arErrors, "39-40:NotifyMail And NotifyPhone is required.");
  1540. }
  1541. // *下述情況通知手機號碼與電子信箱為空值(通知類別為N-皆不通知)
  1542. if($this->Send['AllowanceNotify'] == AllowanceNotifyType::None && ( strlen($this->Send['NotifyMail']) > 0 || strlen($this->Send['NotifyPhone']) > 0 ))
  1543. {
  1544. array_push($arErrors, "39-40:Please remove NotifyMail And NotifyPhone.");
  1545. }
  1546. }
  1547. // 41.折讓單總金額 AllowanceAmount
  1548. // *必填項目
  1549. if(strlen($this->Send['AllowanceAmount']) == 0)
  1550. {
  1551. array_push($arErrors, "41:AllowanceAmount is required.");
  1552. }
  1553. else
  1554. {
  1555. // *含稅總金額
  1556. $this->Send['AllowanceAmount'] = (int) $this->Send['AllowanceAmount'] ;
  1557. }
  1558. }
  1559. // 檢查 D.發票作廢
  1560. if( $this->Invoice_Method == InvoiceMethod::INVOICE_VOID )
  1561. {
  1562. // 42.發票號碼 InvoiceNumber
  1563. // *必填項目
  1564. if(strlen($this->Send['InvoiceNumber']) == 0)
  1565. {
  1566. array_push($arErrors, "42:InvoiceNumber is required.");
  1567. }
  1568. // *預設長度固定10碼
  1569. if(strlen($this->Send['InvoiceNumber']) != 10)
  1570. {
  1571. array_push($arErrors, '42:InvoiceNumber length as 10.');
  1572. }
  1573. }
  1574. // 檢查 D.發票作廢、折讓作廢
  1575. if( $this->Invoice_Method == InvoiceMethod::INVOICE_VOID || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID )
  1576. {
  1577. // 43.作廢原因 Reason
  1578. // *必填欄位
  1579. if(strlen($this->Send['Reason']) == 0)
  1580. {
  1581. array_push($arErrors, "43:Reason is required.");
  1582. }
  1583. // *字數限制在20(含)個字以內
  1584. if(strlen($this->Send['Reason']) > 20)
  1585. {
  1586. array_push($arErrors, "43:Reason max length as 20.");
  1587. }
  1588. // *urlencode
  1589. $this->Send['Reason'] = urlencode($this->Send['Reason']);
  1590. $this->Send['Reason'] = $this->Replace_Symbol($this->Send['Reason']) ;
  1591. }
  1592. // 檢查 E.折讓作廢、H.查詢折讓明細 I.查詢折讓作廢明細、J.發送通知
  1593. if( $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_SEARCH || $this->Invoice_Method == InvoiceMethod::ALLOWANCE_VOID_SEARCH || $this->Invoice_Method == InvoiceMethod::INVOICE_NOTIFY )
  1594. {
  1595. // 44.折讓編號 AllowanceNo
  1596. // *除了J.發送通知非必填不須判斷其他項目都要
  1597. if( $this->Invoice_Method != InvoiceMethod::INVOICE_NOTIFY )
  1598. {
  1599. if(strlen($this->Send['AllowanceNo']) == 0)
  1600. {
  1601. array_push($arErrors, "44:AllowanceNo is required.");
  1602. }
  1603. }
  1604. // *若有值長度固定16字元
  1605. if(strlen($this->Send['AllowanceNo']) != 0 && strlen($this->Send['AllowanceNo']) != 16 )
  1606. {
  1607. array_push($arErrors, '44:AllowanceNo length as 16.');
  1608. }
  1609. }
  1610. // 檢查J.發送通知
  1611. if( $this->Invoice_Method == InvoiceMethod::INVOICE_NOTIFY )
  1612. {
  1613. // 45.NotifyMail 發送電子信箱
  1614. // *若客戶電子信箱有值時,則格式僅能為Email的標準格式
  1615. if(strlen($this->Send['NotifyMail']) > 0 )
  1616. {
  1617. if ( !preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is', $this->Send['NotifyMail']) )
  1618. {
  1619. array_push($arErrors, '45:Invalid Email Format.');
  1620. }
  1621. }
  1622. // *下述情況通知電子信箱不可為空值(發送方式為E-電子郵件)
  1623. if( $this->Send['Notify'] == NotifyType::Email && strlen($this->Send['NotifyMail']) == 0 )
  1624. {
  1625. array_push($arErrors, "39:NotifyMail is required.");
  1626. }
  1627. // *UrlEncode
  1628. $this->Send['NotifyMail'] = urlencode($this->Send['NotifyMail']);
  1629. $this->Send['NotifyMail'] = $this->Replace_Symbol($this->Send['NotifyMail']) ;
  1630. // 46.通知手機號碼 NotifyPhone
  1631. // *若客戶手機號碼有值時,則預設格式為數字組成
  1632. if(strlen($this->Send['Phone']) > 0 )
  1633. {
  1634. if ( !preg_match('/^[0-9]*$/', $this->Send['Phone']) )
  1635. {
  1636. array_push($arErrors, '46:Invalid Phone.');
  1637. }
  1638. }
  1639. // *最大長度為20碼
  1640. if(strlen($this->Send['Phone']) > 20 )
  1641. {
  1642. array_push($arErrors, "46:Phone max length as 20.");
  1643. }
  1644. // *下述情況通知手機號碼不可為空值(發送方式為S-簡訊)
  1645. if( $this->Send['Notify'] == NotifyType::Sms && strlen($this->Send['Phone']) == 0 )
  1646. {
  1647. array_push($arErrors, "46:Phone is required.");
  1648. }
  1649. // 45-46 發送簡訊號碼、發送電子信箱不能全為空值
  1650. if(strlen($this->Send['Phone']) == 0 && strlen($this->Send['NotifyMail']) == 0)
  1651. {
  1652. array_push($arErrors, "45-46:NotifyMail or Phone is required.");
  1653. }
  1654. else
  1655. {
  1656. if( $this->Send['Notify'] == NotifyType::All && ( strlen($this->Send['NotifyMail']) == 0 || strlen($this->Send['Phone']) == 0 ) )
  1657. {
  1658. array_push($arErrors, "45-46:NotifyMail and Phone is required.");
  1659. }
  1660. }
  1661. // 47. 發送方式 Notify
  1662. // *固定給定下述預設值
  1663. if( ($this->Send['Notify'] != NotifyType::Sms ) && ( $this->Send['Notify'] != NotifyType::Email ) && ( $this->Send['Notify'] != NotifyType::All ) )
  1664. {
  1665. array_push($arErrors, "47:Notify is required.");
  1666. }
  1667. // 48.發送內容類型 InvoiceTag
  1668. // *固定給定下述預設值
  1669. if( ( $this->Send['InvoiceTag'] != InvoiceTagType::Invoice ) && ( $this->Send['InvoiceTag'] != InvoiceTagType::Invoice_Void ) && ( $this->Send['InvoiceTag'] != InvoiceTagType::Allowance ) && ( $this->Send['InvoiceTag'] != InvoiceTagType::Allowance_Void ) && ( $this->Send['InvoiceTag'] != InvoiceTagType::Invoice_Winning ) )
  1670. {
  1671. array_push($arErrors, "48:InvoiceTag is required.");
  1672. }
  1673. // 49.發送對象 Notified
  1674. // *固定給定下述預設值
  1675. if( ( $this->Send['Notified'] != NotifiedType::Customer ) && ( $this->Send['Notified'] != NotifiedType::vendor ) && ( $this->Send['Notified'] != NotifiedType::All ) )
  1676. {
  1677. array_push($arErrors, "49:Notified is required.");
  1678. }
  1679. }
  1680. return $arErrors ;
  1681. }
  1682. /**
  1683. * 產生檢查碼
  1684. * 傳入 $arParameters 各參數
  1685. * 傳出 $sMacValue 檢查碼
  1686. */
  1687. function GenerateCheckMacValue($arParameters)
  1688. {
  1689. $sMacValue = '' ;
  1690. if(isset($arParameters))
  1691. {
  1692. // 資料排序
  1693. // php 5.3以下不支援
  1694. // ksort($arParameters, SORT_NATURAL | SORT_FLAG_CASE);
  1695. uksort($arParameters, array('AllInvoice','merchantSort'));
  1696. // 開始組合字串
  1697. $sMacValue = 'HashKey=' . $this->HashKey ;
  1698. foreach($arParameters as $key => $value)
  1699. {
  1700. $sMacValue .= '&' . $key . '=' . $value ;
  1701. }
  1702. $sMacValue .= '&HashIV=' . $this->HashIV ;
  1703. // URL Encode編碼
  1704. $sMacValue = urlencode($sMacValue);
  1705. // 轉成小寫
  1706. $sMacValue = strtolower($sMacValue);
  1707. // 取代為與 dotNet 相符的字元
  1708. $sMacValue = str_replace('%2d', '-', $sMacValue);
  1709. $sMacValue = str_replace('%5f', '_', $sMacValue);
  1710. $sMacValue = str_replace('%2e', '.', $sMacValue);
  1711. $sMacValue = str_replace('%21', '!', $sMacValue);
  1712. $sMacValue = str_replace('%2a', '*', $sMacValue);
  1713. $sMacValue = str_replace('%28', '(', $sMacValue);
  1714. $sMacValue = str_replace('%29', ')', $sMacValue);
  1715. // MD5編碼
  1716. $sMacValue = md5($sMacValue);
  1717. $sMacValue = strtoupper($sMacValue);
  1718. }
  1719. return $sMacValue ;
  1720. }
  1721. /**
  1722. * 參數內特殊字元取代
  1723. * 傳入 $sParameters 參數
  1724. * 傳出 $sReturn_Info 回傳取代後變數
  1725. */
  1726. function Replace_Symbol($sParameters)
  1727. {
  1728. if(!empty($sParameters))
  1729. {
  1730. $sParameters = str_replace('%2D', '-', $sParameters);
  1731. $sParameters = str_replace('%2d', '-', $sParameters);
  1732. $sParameters = str_replace('%5F', '_', $sParameters);
  1733. $sParameters = str_replace('%5f', '_', $sParameters);
  1734. $sParameters = str_replace('%2E', '.', $sParameters);
  1735. $sParameters = str_replace('%2e', '.', $sParameters);
  1736. $sParameters = str_replace('%21', '!', $sParameters);
  1737. $sParameters = str_replace('%2A', '*', $sParameters);
  1738. $sParameters = str_replace('%2a', '*', $sParameters);
  1739. $sParameters = str_replace('%28', '(', $sParameters);
  1740. $sParameters = str_replace('%29', ')', $sParameters);
  1741. }
  1742. return $sParameters ;
  1743. }
  1744. /**
  1745. * 自訂排序使用
  1746. */
  1747. private static function merchantSort($a,$b)
  1748. {
  1749. return strcasecmp($a, $b);
  1750. }
  1751. /**
  1752. * 幕後送出參數
  1753. * 傳入 $aSend_Info 送出參數
  1754. * 傳出 $aReturn_Info 回傳參數
  1755. */
  1756. private function ServerPost($aSend_Info)
  1757. {
  1758. // 變數宣告
  1759. $sSend_Info = '' ;
  1760. $sReturn_Info = '' ;
  1761. $aReturn_Info = array() ;
  1762. // 組合字串
  1763. foreach($aSend_Info as $key => $value)
  1764. {
  1765. if( $sSend_Info == '')
  1766. {
  1767. $sSend_Info .= $key . '=' . $value ;
  1768. }
  1769. else
  1770. {
  1771. $sSend_Info .= '&' . $key . '=' . $value ;
  1772. }
  1773. }
  1774. // 送出參數
  1775. $ch = curl_init();
  1776. curl_setopt($ch, CURLOPT_URL, $this->Invoice_Url);
  1777. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  1778. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  1779. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1780. curl_setopt($ch, CURLOPT_POST, TRUE);
  1781. curl_setopt($ch, CURLOPT_POSTFIELDS, $sSend_Info);
  1782. // 回傳參數
  1783. $sReturn_Info = curl_exec($ch);
  1784. curl_close($ch);
  1785. // 轉結果為陣列。
  1786. parse_str($sReturn_Info, $aReturn_Info);
  1787. return $aReturn_Info;
  1788. }
  1789. }
  1790. ?>