Cmbc.php 3.8 KB

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