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