cmbpay.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. define('CMPPAY_ONLINE',true);
  10. use Log;
  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. // if(CMPPAY_ONLINE == true) {
  61. // $pub_key = 'MIGJAoGBALKsktbh7j9O9pM0p7qnxxImgODqxjpiT7Xl2bvZCywJtwsNI6CchqAagOYGJjG0NZsnjFunTw5YM9TD5KxsUOILAL6IaNMH/fWREhVjkUDJ4CYtLWlKozElvXRp1iZxf66yHHhN4t7TE5S9NWpEBSn37TEfFLU99Go1WReI1XN1AgMBAAE=';
  62. // } else {
  63. //
  64. // }
  65. $pk = chunk_split($pub_key, 64, "\n");
  66. $pk = "-----BEGIN PUBLIC KEY-----\n{$pk}-----END PUBLIC KEY-----\n";
  67. return $pk;
  68. }
  69. public function check()
  70. {
  71. if($this->success() == false) {
  72. return false;
  73. }
  74. $sign = $this->sign();
  75. $pk = $this->public_key();
  76. $pkid = openssl_pkey_get_public($pk);
  77. $content = $this->filter_param($this->mParam);
  78. $ok = openssl_verify($content, $sign, $pkid, OPENSSL_ALGO_SHA1);
  79. openssl_free_key($pkid);
  80. return $ok;
  81. }
  82. private function substr($arr,$src)
  83. {
  84. $len = strlen($src);
  85. $ar_len = 0;
  86. foreach ($arr as $key => $value) {
  87. $ar_len += intval($value);
  88. }
  89. if($ar_len > $len) return false;
  90. $result = [];
  91. $pos = 0;
  92. foreach ($arr as $key => $len)
  93. {
  94. $val = substr($src,$pos,$len);
  95. $pos += $len;
  96. $result[$key] = $val;
  97. }
  98. return $result;
  99. }
  100. public function format()
  101. {
  102. $body = $this->mParam['MerchantPara'];
  103. $params = preg_split('/\||:/', $body);
  104. $result = [];
  105. for ($i = 0; $i < count($params); ++$i) {
  106. $key = $params[$i];
  107. $val = $params[++$i];
  108. $result[$key] = $val;
  109. }
  110. $ar_key = array("BranchID" => 4,"CoNo" => 6, "Date" => 8,"trade_no" => 20);
  111. $msg = $this->substr($ar_key,$this->mParam['Msg']);
  112. $result['trade_no'] = $msg['trade_no'];
  113. return $result;
  114. }
  115. }
  116. class cmbpay implements IPay
  117. {
  118. const notify_signurl = BASE_SITE_URL . '/mobile/cmbpay_sign.php';
  119. const notify_payurl = BASE_SITE_URL . '/mobile/cmbpay_notify.php';
  120. const app_returl = BASE_SITE_URL . '/mobile/index.php';
  121. const debug_notify_signurl = 'http://121.43.114.153/mobile/cmbpay_sign.php';
  122. const debug_notify_payurl = 'http://121.43.114.153/mobile/cmbpay_notify.php';
  123. const debug_app_returl = 'http://121.43.114.153/mobile/index.php';
  124. static public function config()
  125. {
  126. if(CMPPAY_ONLINE == true)
  127. {
  128. return array(
  129. 'PAY_URL' => 'https://netpay.cmbchina.com/netpayment/BaseHttp.dll?PrePayEUserP',
  130. 'CMCX_URL' => 'http://58.61.30.110/CmbBank_B2B/UI/DIDI/DoBusiness.ashx', //签约协议查询接口
  131. 'MchNo' => 'N6255331',//协议商户企业编号 //请填写自己的商户企业编号
  132. 'BranchID' => '0021',//开户分行号 4位 请自己配置
  133. 'CoNo' => '006438', //商户号 6位 请自己配置
  134. 'singKey' => 'StanleyLRLZ12345',//签名密钥 查询单笔订单需要
  135. );
  136. }
  137. else
  138. {
  139. return array(
  140. 'PAY_URL' => 'http://61.144.248.29:801/netpayment/BaseHttp.dll?PrePayEUserP',
  141. 'CMCX_URL' => 'http://58.61.30.110/CmbBank_B2B/UI/DIDI/DoBusiness.ashx', //签约协议查询接口
  142. 'MchNo' => 'P0026365',//协议商户企业编号 //请填写自己的商户企业编号
  143. 'BranchID' => '0021',//开户分行号 4位 请自己配置
  144. 'CoNo' => '000140', //商户号 6位 请自己配置
  145. 'singKey' => '',//签名密钥 查询单笔订单需要 测试环境为空
  146. );
  147. }
  148. }
  149. static private function pay_notify_url()
  150. {
  151. if(is_debug()) {
  152. return self::debug_notify_payurl;
  153. }
  154. else {
  155. return self::notify_payurl;
  156. }
  157. }
  158. static private function app_returl()
  159. {
  160. if(is_debug()) {
  161. return self::debug_app_returl;
  162. }
  163. else {
  164. return self::app_returl;
  165. }
  166. }
  167. static private function sign_notify_url()
  168. {
  169. if(is_debug()) {
  170. return self::debug_notify_signurl;
  171. }
  172. else {
  173. return self::notify_signurl;
  174. }
  175. }
  176. public function on_notify($param)
  177. {
  178. $nofity = new NotifyData($param);
  179. if($nofity->check()) {
  180. return $nofity->format();
  181. } else {
  182. Log::record('招行支付回调错误',Log::ERR);
  183. return false;
  184. }
  185. }
  186. public function gen_pay($pay_sn, $cents,$order_sn,$subject)
  187. {
  188. $config = self::config();
  189. $bill_no = mt_rand(1000000000, 9999999999);
  190. $payprice = $cents / 100;
  191. $time = time();
  192. $date = date('Ymd', $time);
  193. $user_id = $_SESSION['member_id'];
  194. $attach = "pay_sn:{$pay_sn}|order_sn:{$order_sn}|member_id:{$user_id}";
  195. $param = $this->gen_param($time,$payprice,$attach,$bill_no);
  196. $code = $this->genMerchantCode($param);
  197. $ret_pam = "act=pay_return&op=cmbpay&pay_sn={$pay_sn}&order_sn={$order_sn}&member_id={$user_id}";
  198. $data = [
  199. 'BranchID' => $config['BranchID'],
  200. 'CoNo' => $config['CoNo'],
  201. 'BillNo' => $bill_no,
  202. 'Amount' => $payprice,
  203. 'Date' => $date,
  204. 'ExpireTimeSpan' => '30',
  205. 'MerchantUrl' => self::pay_notify_url(),
  206. 'MerchantPara' => $attach,
  207. 'MerchantCode' => $code,
  208. 'MerchantRetUrl' => self::app_returl(),
  209. 'MerchantRetPara' => $ret_pam
  210. ];
  211. $count = count($data);
  212. $strParam = '';
  213. $index = 0;
  214. foreach ($data as $key => $value) {
  215. $strParam .= "{$key}=" . urlencode($value);
  216. if($index != $count - 1) {
  217. $strParam .= "&";
  218. }
  219. ++$index;
  220. }
  221. return array("req_url" => $config['PAY_URL'],"data" => $strParam);
  222. }
  223. private function gen_param($time,$payprice,$attach,$bill_no)
  224. {
  225. $config = self::config();
  226. $date = date('Ymd', $time);
  227. $seq = $date . $bill_no;
  228. $user_id = $_SESSION['member_id'];
  229. $param = [
  230. 'strKey' => $config['singKey'],
  231. 'strDate' => $date,
  232. 'strBranchID' => $config['BranchID'],
  233. 'strCono' => $config['CoNo'],
  234. 'strBillNo' => $bill_no,
  235. 'strAmount' => "{$payprice}",
  236. 'strMerchantPara' => $attach,
  237. 'strMerchantUrl' => self::pay_notify_url(),
  238. 'strPayerID' => "{$user_id}",
  239. 'strPayeeID' => '',
  240. 'strClientIP' => $_SERVER['REMOTE_ADDR'],
  241. 'strGoodsType' => '54011600',
  242. 'strReserved' => join('', [
  243. '<Protocol>',
  244. '<PNo>' . $user_id . '</PNo>',
  245. '<TS>' . date('YmdHis', $time) . '</TS>',
  246. '<MchNo>' . $config['MchNo'] . '</MchNo>',
  247. '<Seq>' . $seq . '</Seq>',
  248. '<URL>' . self::sign_notify_url() . '</URL>',
  249. '</Protocol>',
  250. ])
  251. ];
  252. return $param;
  253. }
  254. private function genMerchantCode($param)
  255. {
  256. $str = '';
  257. if (!empty($param['strClientIP'])) {
  258. $str .= '<$ClientIP$>' . $param['strClientIP'] . '</$ClientIP$>';
  259. }
  260. if (!empty($param['strGoodsType'])) {
  261. $str .= '<$GoodsType$>' . $param['strGoodsType'] . '</$GoodsType$>';
  262. }
  263. if (!empty($param['strReserved'])) {
  264. $str .= '<$Reserved$>' . $param['strReserved'] . '</$Reserved$>';
  265. }
  266. $rnd = mt_rand();
  267. $str = $rnd . '|' . $param['strPayerID'] . '<$CmbSplitter$>' . $param['strPayeeID'] . $str;
  268. $rc4Key = md5($param['strKey']);
  269. $rc4Key = pack('H*', $rc4Key);
  270. $rc4Val = openssl_encrypt($str, "RC4", $rc4Key, true);
  271. $rc4Val = base64_encode($rc4Val);
  272. $rc4Val = str_replace('+', '*', $rc4Val);
  273. $sha1Val = $param['strKey'] . $rc4Val . $param['strDate']
  274. . $param['strBranchID'] . $param['strCono'] . $param['strBillNo']
  275. . $param['strAmount'] . $param['strMerchantPara'] . $param['strMerchantUrl'];
  276. $sha1Val = sha1($sha1Val);
  277. return "|{$rc4Val}|{$sha1Val}";
  278. }
  279. }