Yspay.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. class Yspay extends Core
  4. {
  5. public function __construct($config)
  6. {
  7. $project = Dever::project('pay');
  8. # 通知接口
  9. $config['notify'] = $this->url($config['type'], $config['id']);
  10. $this->config = $config;
  11. }
  12. /**
  13. * 通知
  14. */
  15. public function notify()
  16. {
  17. $input = file_get_contents("php://input");
  18. if ($input) {
  19. parse_str($input, $input);
  20. } else {
  21. $input = Dever::input();
  22. }
  23. $this->log('支付回调-初始化', $input);
  24. $tools = new \Cmbc\Handle();
  25. $callback = $tools->get('notify', $this->config);
  26. $result = $callback->request($input, $this);
  27. if ($result) {
  28. $this->log('支付回调-获取数据', $result);
  29. $this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
  30. echo 'success=Y';die;
  31. } else {
  32. echo 'success=N';die;
  33. }
  34. }
  35. /**
  36. * 获取统一下单的基本信息
  37. */
  38. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  39. {
  40. $trade_type = $this->getType($type);
  41. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  42. $request['merOrderId'] = $order_id;
  43. $request['mid'] = $this->config['mchid'];
  44. $request['tid'] = $this->config['mchid'];
  45. $request['instMid'] = 'MINIDEFAULT';
  46. $request['totalAmount'] = $cash * 100;
  47. $request['subAppId'] = $this->config['appid'];
  48. $request['requestTimestamp'] = date("Y-m-d H:i:s");
  49. $request['expireTime'] = $this->config['timeout'];
  50. $request['notifyUrl'] = $this->config['notify'];
  51. $request['tradeType'] = 'MINI';
  52. /*
  53. # 是否启用分账
  54. $request['divisionFlag'] = true;
  55. # 平台分账金额
  56. $request['platformAmount'] = $request['totalAmount'] * 0.1;
  57. if ($request['divisionFlag']) {
  58. $request['subOrders'] = array();
  59. $request['subOrders']['mid'] = 1;
  60. $request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
  61. }*/
  62. if (!$openid) {
  63. # 测试的openid
  64. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  65. } else {
  66. $request['subOpenId'] = $openid;
  67. }
  68. $result = $this->request('unified-order', $request);
  69. $this->updateOrderParam($order_id, $result);
  70. return $result;
  71. }
  72. # 退款
  73. public function refundByOrder($order_id)
  74. {
  75. $info = Dever::db('pay/order')->one(array('order_id' => $order_id));
  76. if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 5)) {
  77. $this->refund($info['order_id'], $info['cash']);
  78. Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => 5));
  79. return $result;
  80. }
  81. return false;
  82. }
  83. # 退款
  84. public function refund($order_id, $cash)
  85. {
  86. $out_trade_no = $order_id;
  87. $cash = $cash * 100;
  88. $total_fee = $cash;
  89. $refund_fee = $cash;
  90. $input = new \WxPayRefund();
  91. $input->SetOut_trade_no($out_trade_no);
  92. $input->SetTotal_fee($total_fee);
  93. $input->SetRefund_fee($refund_fee);
  94. $input->SetOut_refund_no($out_trade_no . '_' . time());
  95. $input->SetOp_user_id($this->config->GetMerchantId());
  96. $result = \WxPayApi::refund($this->config, $input);
  97. return $result;
  98. }
  99. /**
  100. * 获取二维码支付
  101. */
  102. public function qrcode($order, $refer)
  103. {
  104. $notify = new \NativePay();
  105. $result = $notify->GetPayUrl($order);
  106. $url = $result['code_url'];
  107. return $url;
  108. }
  109. /**
  110. * 获取小程序支付
  111. */
  112. public function applet($order)
  113. {
  114. $result = array();
  115. if (isset($order['prepay_id'])) {
  116. $result['time'] = $order['timeStamp'];
  117. $result['nonce_str'] = $order['nonceStr'];
  118. $result['prepay_id'] = $order['prepay_id'];
  119. $result['sign_type'] = $order['signType'];
  120. $result['sign'] = $order['paySign'];
  121. }
  122. return $result;
  123. }
  124. /**
  125. * 获取页面支付
  126. */
  127. public function page($order, $refer)
  128. {
  129. $refer = urldecode($refer);
  130. $tools = new \JsApiPay($this->config);
  131. $info = $tools->GetJsApiParameters($order);
  132. $html = '<script type="text/javascript">
  133. function jsApiCall()
  134. {
  135. WeixinJSBridge.invoke(
  136. "getBrandWCPayRequest",
  137. '.$info.',
  138. function(res){
  139. //WeixinJSBridge.log(res.err_msg);
  140. if(res.err_msg == "get_brand_wcpay_request:ok")
  141. {
  142. location.href = "'.$refer.'";
  143. } else {
  144. alert(res.err_code+res.err_desc+res.err_msg);
  145. }
  146. }
  147. );
  148. }
  149. function callpay()
  150. {
  151. if (typeof WeixinJSBridge == "undefined"){
  152. if( document.addEventListener ){
  153. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  154. }else if (document.attachEvent){
  155. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  156. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  157. }
  158. }else{
  159. jsApiCall();
  160. }
  161. }
  162. callpay();
  163. </script>';
  164. return $html;
  165. }
  166. private function getType($type)
  167. {
  168. switch ($type) {
  169. case 1:
  170. $type = 'JSAPI';
  171. break;
  172. case 2:
  173. $type = 'NATIVE';
  174. break;
  175. case 3:
  176. $type = 'APP';
  177. break;
  178. case 4:
  179. $type = 'MWEB';
  180. break;
  181. }
  182. return $type;
  183. }
  184. }