Cmbc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $this->log('支付回调-初始化', $input);
  27. parse_str($input, $input);
  28. $tools = new \Cmbc\Handle();
  29. $callback = $tools->get('notify', $this->config);
  30. $result = $callback->request($input, $this);
  31. if ($result) {
  32. $this->log('支付回调-获取数据', $result);
  33. $this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt'], '支付成功');
  34. echo 'success=Y';die;
  35. } else {
  36. echo 'success=N';die;
  37. }
  38. }
  39. /**
  40. * 获取统一下单的基本信息
  41. */
  42. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  43. {
  44. $trade_type = $this->getType($type);
  45. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  46. $tools = new \Cmbc\Handle();
  47. $order = $tools->get('order', $this->config);
  48. $request['mhtOrderNo'] = $order_id;
  49. $request['mhtOrderName'] = $name;
  50. $request['mhtOrderAmt'] = $cash * 100;
  51. $request['mhtOrderDetail'] = $name;
  52. $request['mhtOrderStartTime'] = date("YmdHis");
  53. $request['notifyUrl'] = $this->config->getNotifyUrl();
  54. $request['outputType'] = 1;
  55. if (!$openid) {
  56. $request['consumerId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  57. } else {
  58. $request['consumerId'] = $openid;
  59. }
  60. $result = $order->request($request);
  61. $this->updateOrderParam($order_id, $result);
  62. return $result;
  63. }
  64. /**
  65. * 获取二维码支付
  66. */
  67. public function qrcode($order, $refer)
  68. {
  69. $notify = new \NativePay();
  70. $result = $notify->GetPayUrl($order);
  71. $url = $result['code_url'];
  72. return $url;
  73. }
  74. /**
  75. * 获取小程序支付
  76. */
  77. public function applet($order)
  78. {
  79. $result = array();
  80. if (isset($order['prepay_id'])) {
  81. $result['time'] = $order['timeStamp'];
  82. $result['nonce_str'] = $order['nonceStr'];
  83. $result['prepay_id'] = $order['prepay_id'];
  84. $result['sign_type'] = $order['signType'];
  85. $result['sign'] = $order['paySign'];
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 获取页面支付
  91. */
  92. public function page($order, $refer)
  93. {
  94. $refer = urldecode($refer);
  95. $tools = new \JsApiPay($this->config);
  96. $info = $tools->GetJsApiParameters($order);
  97. $html = '<script type="text/javascript">
  98. function jsApiCall()
  99. {
  100. WeixinJSBridge.invoke(
  101. "getBrandWCPayRequest",
  102. '.$info.',
  103. function(res){
  104. //WeixinJSBridge.log(res.err_msg);
  105. if(res.err_msg == "get_brand_wcpay_request:ok")
  106. {
  107. location.href = "'.$refer.'";
  108. } else {
  109. alert(res.err_code+res.err_desc+res.err_msg);
  110. }
  111. }
  112. );
  113. }
  114. function callpay()
  115. {
  116. if (typeof WeixinJSBridge == "undefined"){
  117. if( document.addEventListener ){
  118. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  119. }else if (document.attachEvent){
  120. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  121. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  122. }
  123. }else{
  124. jsApiCall();
  125. }
  126. }
  127. callpay();
  128. </script>';
  129. return $html;
  130. }
  131. private function getType($type)
  132. {
  133. switch ($type) {
  134. case 1:
  135. $type = 'JSAPI';
  136. break;
  137. case 2:
  138. $type = 'NATIVE';
  139. break;
  140. }
  141. return $type;
  142. }
  143. }