Cmbc.php 3.8 KB

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