cmbpay.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/9/21
  6. * Time: 下午7:54
  7. */
  8. namespace Pay;
  9. require_once (BASE_DATA_PATH . "/api/cmbpay/Cmb.class.php");
  10. use \Cmb;
  11. class NotifyData
  12. {
  13. private $mParam;
  14. public function __construct($param)
  15. {
  16. $this->mParam = $param;
  17. }
  18. private function success()
  19. {
  20. return ($this->mParam['Succeed'] =='Y');
  21. }
  22. private function filter_param($param)
  23. {
  24. $filter = [];
  25. foreach ($param as $key => $value)
  26. {
  27. if($key != 'Signature') {
  28. $filter[$key] = ($value);//urlencode
  29. }
  30. }
  31. $pos = 0;
  32. $count = count($filter);
  33. $str = '';
  34. foreach ($filter as $key => $value)
  35. {
  36. $str .= "{$key}=" . "{$value}";
  37. if($pos != $count -1) {
  38. $str .= "&";
  39. }
  40. $pos++;
  41. }
  42. return $str;
  43. }
  44. private function sign()
  45. {
  46. $strsign = $this->mParam['Signature'];
  47. $signAsc = explode("|", $strsign);
  48. $sign = '';
  49. foreach ($signAsc as $v) {
  50. if ($v ==='') {
  51. continue;
  52. }
  53. $sign .= chr($v);
  54. }
  55. return $sign;
  56. }
  57. private function public_key()
  58. {
  59. $pub_key = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALuUIwMGhvbpCwYzKCMzvSMQiLAAj5y74cN09N1TXVONPWhvLWkuzEPSd1ogPJLWiVyEG7gEIBT3zTlCV+NMou0CAwEAAQ==';
  60. $pk = chunk_split($pub_key, 64, "\n");
  61. $pk = "-----BEGIN PUBLIC KEY-----\n{$pk}-----END PUBLIC KEY-----\n";
  62. return $pk;
  63. }
  64. public function check()
  65. {
  66. if($this->success() == false) {
  67. return false;
  68. }
  69. $sign = $this->sign();
  70. $pk = $this->public_key();
  71. $pkid = openssl_pkey_get_public($pk);
  72. $content = $this->filter_param($this->mParam);
  73. $ok = openssl_verify($content, $sign, $pkid, OPENSSL_ALGO_SHA1);
  74. openssl_free_key($pkid);
  75. return $ok;
  76. }
  77. private function substr($arr,$src)
  78. {
  79. $len = strlen($src);
  80. $ar_len = 0;
  81. foreach ($arr as $key => $value) {
  82. $ar_len += intval($value);
  83. }
  84. if($ar_len > $len) return false;
  85. $result = [];
  86. $pos = 0;
  87. foreach ($arr as $key => $len)
  88. {
  89. $val = substr($src,$pos,$len);
  90. $pos += $len;
  91. $result[$key] = $val;
  92. }
  93. return $result;
  94. }
  95. public function format()
  96. {
  97. $body = $this->mParam['MerchantPara'];
  98. $params = preg_split('/\||:/', $body);
  99. $result = [];
  100. for ($i = 0; $i < count($params); ++$i) {
  101. $key = $params[$i];
  102. $val = $params[++$i];
  103. $result[$key] = $val;
  104. }
  105. $ar_key = array("BranchID" => 4,"CoNo" => 6, "Date" => 8,"trade_no" => 20);
  106. $msg = $this->substr($ar_key,$this->mParam['Msg']);
  107. $result['trade_no'] = $msg['trade_no'];
  108. return $result;
  109. }
  110. }
  111. class cmbpay implements IPay
  112. {
  113. const pay_url = 'http://61.144.248.29:801/netpayment/BaseHttp.dll?PrePayEUserP';
  114. const notify_signurl = BASE_SITE_URL . '/mobile/cmbpay_sign.php';
  115. const notify_payurl = BASE_SITE_URL . '/mobile/cmbpay_notify.php';
  116. const app_returl = BASE_SITE_URL . '/mobile/index.php?act=pay_return&op=cmbpay';
  117. const debug_notify_signurl = 'http://121.43.114.153/mobile/cmbpay_sign.php';
  118. const debug_notify_payurl = 'http://121.43.114.153/mobile/cmbpay_notify.php';
  119. const debug_app_returl = 'http://121.43.114.153/mobile/index.php';
  120. static private function pay_notify_url()
  121. {
  122. if(is_debug()) {
  123. return self::debug_notify_payurl;
  124. }
  125. else {
  126. return self::notify_payurl;
  127. }
  128. }
  129. static private function app_returl()
  130. {
  131. if(is_debug()) {
  132. return self::debug_app_returl;
  133. }
  134. else {
  135. return self::app_returl;
  136. }
  137. }
  138. static private function sign_notify_url()
  139. {
  140. if(is_debug()) {
  141. return self::debug_notify_signurl;
  142. }
  143. else {
  144. return self::notify_signurl;
  145. }
  146. }
  147. public function on_notify($param)
  148. {
  149. $nofity = new NotifyData($param);
  150. if($nofity->check()) {
  151. return $nofity->format();
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function gen_pay($pay_sn, $cents,$order_sn,$subject)
  157. {
  158. $config = Cmb::config();
  159. $bill_no = mt_rand(1000000000, 9999999999);
  160. $payprice = $cents / 100;
  161. $time = time();
  162. $date = date('Ymd', $time);
  163. $user_id = $_SESSION['member_id'];
  164. $attach = "pay_sn:{$pay_sn}|order_sn:{$order_sn}|member_id:{$user_id}";
  165. $param = $this->gen_param($time,$payprice,$attach,$bill_no);
  166. $code = $this->genMerchantCode($param);
  167. $data = [
  168. 'BranchID' => $config['BranchID'],
  169. 'CoNo' => $config['CoNo'],
  170. 'BillNo' => $bill_no,
  171. 'Amount' => $payprice,
  172. 'Date' => $date,
  173. 'ExpireTimeSpan' => '30',
  174. 'MerchantUrl' => self::pay_notify_url(),
  175. 'MerchantPara' => $attach,
  176. 'MerchantCode' => $code,
  177. 'MerchantRetUrl' => self::app_returl(),
  178. 'MerchantRetPara' => $attach
  179. ];
  180. $count = count($data);
  181. $strParam = '';
  182. $index = 0;
  183. foreach ($data as $key => $value) {
  184. $strParam .= "{$key}=" . urlencode($value);
  185. if($index != $count - 1) {
  186. $strParam .= "&";
  187. }
  188. ++$index;
  189. }
  190. return array("req_url" => self::pay_url,"data" => $strParam);
  191. }
  192. private function gen_param($time,$payprice,$attach,$bill_no)
  193. {
  194. $date = date('Ymd', $time);
  195. $seq = $date . $bill_no;
  196. $user_id = $_SESSION['member_id'];
  197. $config = Cmb::config();
  198. $param = [
  199. 'strKey' => $config['singKey'],
  200. 'strDate' => $date,
  201. 'strBranchID' => $config['BranchID'],
  202. 'strCono' => $config['CoNo'],
  203. 'strBillNo' => $bill_no,
  204. 'strAmount' => "{$payprice}",
  205. 'strMerchantUrl' => self::pay_notify_url(),
  206. 'strMerchantPara' => $attach,
  207. 'strPayerID' => "{$user_id}",
  208. 'strPayeeID' => '',
  209. 'strClientIP' => $_SERVER['REMOTE_ADDR'],
  210. 'strGoodsType' => '54011600',
  211. 'strReserved' => join('', [
  212. '<Protocol>',
  213. '<PNo>' . $user_id . '</PNo>',
  214. '<TS>' . date('YmdHis', $time) . '</TS>',
  215. '<MchNo>' . $config['MchNo'] . '</MchNo>',
  216. '<Seq>' . $seq . '</Seq>',
  217. '<URL>' . self::sign_notify_url() . '</URL>',
  218. '</Protocol>',
  219. ])
  220. ];
  221. return $param;
  222. }
  223. private function genMerchantCode($param)
  224. {
  225. $str = '';
  226. if (!empty($param['strClientIP'])) {
  227. $str .= '<$ClientIP$>' . $param['strClientIP'] . '</$ClientIP$>';
  228. }
  229. if (!empty($param['strGoodsType'])) {
  230. $str .= '<$GoodsType$>' . $param['strGoodsType'] . '</$GoodsType$>';
  231. }
  232. if (!empty($param['strReserved'])) {
  233. $str .= '<$Reserved$>' . $param['strReserved'] . '</$Reserved$>';
  234. }
  235. $rnd = mt_rand();
  236. $str = $rnd . '|' . $param['strPayerID'] . '<$CmbSplitter$>' . $param['strPayeeID'] . $str;
  237. $rc4Key = md5($param['strKey']);
  238. $rc4Key = pack('H*', $rc4Key);
  239. $rc4Val = openssl_encrypt($str, "RC4", $rc4Key, true);
  240. $rc4Val = base64_encode($rc4Val);
  241. $rc4Val = str_replace('+', '*', $rc4Val);
  242. $sha1Val = $param['strKey'] . $rc4Val . $param['strDate']
  243. . $param['strBranchID'] . $param['strCono'] . $param['strBillNo']
  244. . $param['strAmount'] . $param['strMerchantPara'] . $param['strMerchantUrl'];
  245. $sha1Val = sha1($sha1Val);
  246. return "|{$rc4Val}|{$sha1Val}";
  247. }
  248. }