Cmbc.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. Dever::apply('sdk/cmbc', 'pay');
  4. class Cmbc extends Core
  5. {
  6. public function __construct($config)
  7. {
  8. $project = Dever::project('pay');
  9. $this->config = new \Cmbc\Setting();
  10. # 通知接口
  11. $config['notify'] = $this->url($config['type'], $config['id']);
  12. # 证书
  13. $config['ssl'] = array
  14. (
  15. 'cert' => $config['file_cert'],
  16. 'key' => $config['file_key'],
  17. );
  18. $this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']);
  19. }
  20. /**
  21. * 通知
  22. */
  23. public function notify()
  24. {
  25. $input = file_get_contents("php://input");
  26. if ($input) {
  27. parse_str($input, $input);
  28. } else {
  29. $input = Dever::input();
  30. }
  31. $this->log('支付回调-初始化', $input);
  32. $tools = new \Cmbc\Handle();
  33. $callback = $tools->get('notify', $this->config);
  34. $result = $callback->request($input, $this);
  35. if ($result) {
  36. $this->log('支付回调-获取数据', $result);
  37. $this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
  38. echo 'success=Y';die;
  39. } else {
  40. echo 'success=N';die;
  41. }
  42. }
  43. /**
  44. * 获取统一下单的基本信息
  45. */
  46. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false, $other = false, $refer = false)
  47. {
  48. $trade_type = $this->getType($type);
  49. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  50. $tools = new \Cmbc\Handle();
  51. $order = $tools->get('order', $this->config);
  52. $request['mhtOrderNo'] = $order_id;
  53. $request['mhtOrderName'] = $name;
  54. $request['mhtOrderAmt'] = $cash * 100;
  55. $request['mhtOrderDetail'] = $name;
  56. $request['mhtOrderStartTime'] = date("YmdHis");
  57. $request['notifyUrl'] = $this->config->getNotifyUrl();
  58. $request['outputType'] = 1;
  59. if (!$openid) {
  60. $request['consumerId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  61. } else {
  62. $request['consumerId'] = $openid;
  63. }
  64. $result = $order->request($request);
  65. $this->updateOrderParam($order_id, $result);
  66. return $result;
  67. }
  68. /**
  69. * 获取二维码支付
  70. */
  71. public function qrcode($order, $refer)
  72. {
  73. $notify = new \NativePay();
  74. $result = $notify->GetPayUrl($order);
  75. $url = $result['code_url'];
  76. return array
  77. (
  78. 'url' => $url,
  79. 'type' => 'qrcode',
  80. );
  81. return $url;
  82. }
  83. /**
  84. * 获取小程序支付
  85. */
  86. public function applet($order)
  87. {
  88. $result = array();
  89. if (isset($order['prepay_id'])) {
  90. $result['time'] = $order['timeStamp'];
  91. $result['nonce_str'] = $order['nonceStr'];
  92. $result['prepay_id'] = $order['prepay_id'];
  93. $result['sign_type'] = $order['signType'];
  94. $result['sign'] = $order['paySign'];
  95. $result['type'] = 'applet';
  96. }
  97. return $result;
  98. }
  99. /**
  100. * 获取页面支付
  101. */
  102. public function page($order, $refer)
  103. {
  104. $refer = urldecode($refer);
  105. $tools = new \JsApiPay($this->config);
  106. $info = $tools->GetJsApiParameters($order);
  107. $html = '<script type="text/javascript">
  108. function jsApiCall()
  109. {
  110. WeixinJSBridge.invoke(
  111. "getBrandWCPayRequest",
  112. '.$info.',
  113. function(res){
  114. //WeixinJSBridge.log(res.err_msg);
  115. if(res.err_msg == "get_brand_wcpay_request:ok")
  116. {
  117. location.href = "'.$refer.'";
  118. } else {
  119. alert(res.err_code+res.err_desc+res.err_msg);
  120. }
  121. }
  122. );
  123. }
  124. function callpay()
  125. {
  126. if (typeof WeixinJSBridge == "undefined"){
  127. if( document.addEventListener ){
  128. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  129. }else if (document.attachEvent){
  130. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  131. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  132. }
  133. }else{
  134. jsApiCall();
  135. }
  136. }
  137. callpay();
  138. </script>';
  139. return $html;
  140. }
  141. private function getType($type)
  142. {
  143. switch ($type) {
  144. case 1:
  145. $type = 'JSAPI';
  146. break;
  147. case 2:
  148. $type = 'NATIVE';
  149. break;
  150. case 3:
  151. $type = 'APP';
  152. break;
  153. case 4:
  154. $type = 'MWEB';
  155. break;
  156. }
  157. return $type;
  158. }
  159. }