VM暫存
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

252 satır
8.2KB

  1. <?php
  2. require_once 'auth_mpi_mac.php'; // 24 位元
  3. //require_once 'auth_mpi_mac8.php'; // 8 位元
  4. /**
  5. * 中國信託金流介接
  6. */
  7. class CTBCAgent {
  8. public $SSLAuthUI = "SSLAuthUI"; // URL授權介面
  9. public $MerchantID = "MerchantID"; // 銀行所授與的特店代號,純數字,固定 13 碼。
  10. public $TerminalID = "TerminalID"; // 銀行所授與的終端機代號,純數字,固定 8 碼。
  11. public $Option = "Option"; // 純數字欄位,依交易方式不同填入不同的資料,說明如下:
  12. /*
  13.  一般交易請填「1」。
  14.  分期交易請填一到兩碼的分期期數。
  15.  紅利交易請填固定兩碼的產品代碼。
  16.  紅利分期交易請填第一至二碼固定為產品代碼,第三碼或三至四碼為分期期數。
  17. */
  18. public $Key = "Key"; // 此為貴特店在URL 帳務管理後台登錄的壓碼字串。
  19. public $MerchantName = "MerchantName"; // 特店所要顯示的商店名稱,中文請填BIG5 碼。
  20. public $OrderDetail = "OrderDetail"; // 訂單描述,中文請填BIG5 碼。
  21. public $AutoCap = "AutoCap"; // 是否自動請款 (0–不自動請款, 1–自動請款)
  22. public $AuthResURL = "AuthResURL"; // 從收單行端取得授權碼後,要導回的網址,請勿填入特殊字元@、#、%、?、&等。
  23. public $Customize = "Customize"; // 設定刷卡頁顯示特定語系或客制化頁面。(1–繁體中文, 2–簡體中文, 3–英文, 5–客制化頁面)
  24. function __construct() {
  25. $this->CTBCAgent();
  26. }
  27. function CTBCAgent() {
  28. }
  29. /**
  30. * 1. CTBC 結帳
  31. *
  32. * [data]
  33. * *merID : 特店網站專用代號
  34. * *URLEnc: 交易訊息的密文(URLEnc)
  35. */
  36. public function CheckOut($data) {
  37. extract($data);
  38. // 取得url enc
  39. $urlEnc = $this->URLEnc($data);
  40. // 轉址
  41. $szHtml = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  42. $szHtml .= '<div style="text-align:center;" ><form id="__ctbcForm" method="post" target="' . $target . '" action="' . $this->SSLAuthUI . '">';
  43. $szHtml .="<input type='hidden' name='URLEnc' value='$urlEnc' />";
  44. $szHtml .="<input type='hidden' name='merID' value='$merID' />";
  45. $szHtml .= '<script type="text/javascript">document.getElementById("__ctbcForm").submit();</script>';
  46. $szHtml .= '</form></div>';
  47. echo $szHtml;
  48. }
  49. /**
  50. * 2. 解密URL 交易訊息的密文(URLDec)
  51. *
  52. * [data]
  53. * *encRes= : 密文
  54. * debug: 預設(進行交易時)請填0,偵錯時請填1。
  55. */
  56. public function Decrypt($data) {
  57. extract($data);
  58. if (empty($debug)) { $debug = "0"; }
  59. return gendecrypt(
  60. $encRes,
  61. $this->Key,
  62. $debug);
  63. }
  64. /**
  65. * 3. 取得 checkmac
  66. */
  67. public function CheckMac($data) {
  68. extract($data);
  69. if (empty($debug)) { $debug = "0"; }
  70. if (!empty($prodcode)) {
  71. $option = $prodcode; // option: 紅利交易
  72. }else{
  73. $option = $numberofpay; // option: 一般交易, 分期交易
  74. }
  75. return auth_out_mac(
  76. $status, $errcode, $authcode, $authamt, $lidm, $offsetamt, $originalamt, $utilizedpoint,
  77. $option,
  78. $last4digitpan,
  79. $this->Key,
  80. $debug);
  81. }
  82. /**
  83. * 解密URL 交易訊息的密文(URLDec)
  84. *
  85. * [data]
  86. * *encRes= : 密文
  87. * debug: 預設(進行交易時)請填0,偵錯時請填1。
  88. */
  89. function URLDec($data) {
  90. extract($data);
  91. if (empty($debug)) { $debug = "0"; }
  92. $EncArray = gendecrypt(
  93. $encRes,
  94. $this->Key,
  95. $debug);
  96. $MACString = '';
  97. $URLEnc = '';
  98. //echo "<BR>\n";
  99. foreach($EncArray AS $name => $val){
  100. echo $name ."=>". urlencode(trim($val,"\x00..\x08")) ."\n";
  101. }
  102. if(isset($EncArray['status'])){
  103. $status = isset($EncArray['status']) ? $EncArray['status'] : "";
  104. $errCode = isset($EncArray['errcode']) ? $EncArray['errcode'] : "";
  105. $authCode = isset($EncArray['authcode']) ? $EncArray['authcode'] : "";
  106. $authAmt = isset($EncArray['authamt']) ? $EncArray['authamt'] : "";
  107. $lidm = isset($EncArray['lidm']) ? $EncArray['lidm'] : "";
  108. $OffsetAmt = isset($EncArray['offsetamt']) ? $EncArray['offsetamt'] : "";
  109. $OriginalAmt = isset($EncArray['originalamt']) ? $EncArray['originalamt'] : "";
  110. $UtilizedPoint = isset($EncArray['utilizedpoint']) ? $EncArray['utilizedpoint'] : "";
  111. $Option = isset($EncArray['numberofpay']) ? $EncArray[' numberofpay'] : "";
  112. //紅利交易時請帶入prodcode
  113. //$Option = isset($EncArray['prodcode']) ? $EncArray['prodcode'] : "";
  114. $Last4digitPAN = isset($EncArray['last4digitpan']) ? $EncArray['last4digitpan'] : "";
  115. $pidResult= isset($EncArray['pidResult']) ? $EncArray['pidResult'] : "";
  116. $CardNumber = isset($EncArray['CardNumber']) ? $EncArray['CardNumber'] : "";
  117. $MACString = auth_out_mac(
  118. $status, $errCode, $authCode, $authAmt, $lidm, $OffsetAmt, $OriginalAmt, $UtilizedPoint,
  119. $Option,
  120. //$this->Option,
  121. $Last4digitPAN,
  122. $this->Key,
  123. $debug);
  124. echo "MACString=$MACString\n";
  125. $outmac = isset($EncArray['outmac']) ? $EncArray['outmac'] : "";
  126. echo "outmac=$outmac\n";
  127. }else{
  128. // do nothing
  129. foreach ($EncArray as $key => $value){
  130. switch ($key){
  131. /* 支付後的回傳的基本參數 */
  132. case "status": $status = $value; break;
  133. case "errcode": $errcode = $value; break;
  134. case "authcode": $authcode = $value; break;
  135. case "authamt": $authamt = $value; break;
  136. case "lidm": $lidm = $value; break;
  137. case "offsetamt": $offsetamt = $value; break;
  138. case "originalamt": $originalamt = $value; break;
  139. case "utilizedpoint": $utilizedpoint = $value; break;
  140. case "numberofpay": $numberofpay = $value; break;
  141. case "last4digitpan": $last4digitpan = $value; break;
  142. case "pidResult": $pid_result = $value; break;
  143. case "CardNumber": $card_number = $value; break;
  144. default: break;
  145. }
  146. }
  147. // 一律記錄log。
  148. $data = array(
  149. 'status' => $status,
  150. 'errcode' => $errcode,
  151. 'authcode' => $authcode,
  152. 'authamt' => $authamt,
  153. 'lidm' => $lidm,
  154. 'offsetamt' => $offsetamt,
  155. 'originalamt' => $originalamt,
  156. 'utilizedpoint' => $utilizedpoint,
  157. 'numberofpay' => $numberofpay,
  158. 'last4digitpan' => $last4digitpan,
  159. 'pid_result' => $pid_result,
  160. 'card_number' => $card_number
  161. );
  162. echo json_encode($data);
  163. }
  164. }
  165. /**
  166. * 產生URL 交易訊息的密文(URLEnc)
  167. *
  168. * [data]
  169. * *lidm : 訂單編號
  170. * *purchAmt: 總金額
  171. * txType: 交易方式,長度為一碼數字。(一般交易:0, 分期交易:1, 紅利折抵一般交易:2, 紅利折抵分期交易:4)
  172. * debug: 預設(進行交易時)請填0,偵錯時請填1。
  173. */
  174. function URLEnc($data) {
  175. extract($data);
  176. if (empty($txType)) { $txType = "0"; } // 預設 0
  177. if (empty($debug)) { $debug = "0"; } // 預設 0
  178. $MACString = auth_in_mac(
  179. $this->MerchantID,
  180. $this->TerminalID,
  181. $lidm,
  182. /* 為電子商場的應用程式所給予此筆交易的訂單編號,
  183. 資料型態為最長19 個字元的文字串。
  184. 訂單編號字串之字元僅接受一般英文字母、數字及底線’_’的組合,
  185. 不可出現其餘符號字元
  186. */
  187. $purchAmt,// 為消費者此筆交易所購買商品欲授權總金額,正整數格式的字串。
  188. $txType, // 交易方式
  189. $this->Option,
  190. $this->Key,
  191. $this->MerchantName,
  192. $this->AuthResURL,
  193. $this->OrderDetail,
  194. $this->AutoCap,
  195. $this->Customize,
  196. $debug);
  197. //echo "InMac=$MACString\n";
  198. $URLEnc = get_auth_urlenc(
  199. $this->MerchantID,
  200. $this->TerminalID,
  201. $lidm,
  202. $purchAmt,
  203. $txType,
  204. $this->Option,
  205. $this->Key,
  206. $this->MerchantName,
  207. $this->AuthResURL,
  208. $this->OrderDetail,
  209. $this->AutoCap,
  210. $this->Customize,
  211. $MACString,
  212. $debug);
  213. //echo "UrlEnc=$URLEnc\n";
  214. return $URLEnc;
  215. }
  216. }