Yspay.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $request['divisionFlag'] = true;
  54. # 平台分账金额
  55. $request['platformAmount'] = $request['totalAmount'] * 0.1;
  56. if ($request['divisionFlag']) {
  57. $request['subOrders'] = array();
  58. $request['subOrders']['mid'] = 1;
  59. $request['subOrders']['totalAmount'] = $request['totalAmount'] - $request['platformAmount'];
  60. }
  61. if (!$openid) {
  62. # 测试的openid
  63. $request['subOpenId'] = 'ofBUV0RUoy_8C4VctZjrSDGzhUfY';
  64. } else {
  65. $request['subOpenId'] = $openid;
  66. }
  67. $result = $this->request('unified-order', $request);
  68. $this->updateOrderParam($order_id, $result);
  69. return $result;
  70. }
  71. # 退款
  72. public function refundByOrder($order_id)
  73. {
  74. $info = Dever::db('pay/order')->one(array('order_id' => $order_id));
  75. if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 5)) {
  76. $this->refund($info['order_id'], $info['cash']);
  77. Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => 5));
  78. return $result;
  79. }
  80. return false;
  81. }
  82. # 退款
  83. public function refund($order_id, $cash)
  84. {
  85. $out_trade_no = $order_id;
  86. $cash = $cash * 100;
  87. $total_fee = $cash;
  88. $refund_fee = $cash;
  89. $input = new \WxPayRefund();
  90. $input->SetOut_trade_no($out_trade_no);
  91. $input->SetTotal_fee($total_fee);
  92. $input->SetRefund_fee($refund_fee);
  93. $input->SetOut_refund_no($out_trade_no . '_' . time());
  94. $input->SetOp_user_id($this->config->GetMerchantId());
  95. $result = \WxPayApi::refund($this->config, $input);
  96. return $result;
  97. }
  98. /**
  99. * 获取二维码支付
  100. */
  101. public function qrcode($order, $refer)
  102. {
  103. $notify = new \NativePay();
  104. $result = $notify->GetPayUrl($order);
  105. $url = $result['code_url'];
  106. return $url;
  107. }
  108. /**
  109. * 获取小程序支付
  110. */
  111. public function applet($order)
  112. {
  113. $result = array();
  114. if (isset($order['prepay_id'])) {
  115. $result['time'] = $order['timeStamp'];
  116. $result['nonce_str'] = $order['nonceStr'];
  117. $result['prepay_id'] = $order['prepay_id'];
  118. $result['sign_type'] = $order['signType'];
  119. $result['sign'] = $order['paySign'];
  120. }
  121. return $result;
  122. }
  123. /**
  124. * 获取页面支付
  125. */
  126. public function page($order, $refer)
  127. {
  128. $refer = urldecode($refer);
  129. $tools = new \JsApiPay($this->config);
  130. $info = $tools->GetJsApiParameters($order);
  131. $html = '<script type="text/javascript">
  132. function jsApiCall()
  133. {
  134. WeixinJSBridge.invoke(
  135. "getBrandWCPayRequest",
  136. '.$info.',
  137. function(res){
  138. //WeixinJSBridge.log(res.err_msg);
  139. if(res.err_msg == "get_brand_wcpay_request:ok")
  140. {
  141. location.href = "'.$refer.'";
  142. } else {
  143. alert(res.err_code+res.err_desc+res.err_msg);
  144. }
  145. }
  146. );
  147. }
  148. function callpay()
  149. {
  150. if (typeof WeixinJSBridge == "undefined"){
  151. if( document.addEventListener ){
  152. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  153. }else if (document.attachEvent){
  154. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  155. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  156. }
  157. }else{
  158. jsApiCall();
  159. }
  160. }
  161. callpay();
  162. </script>';
  163. return $html;
  164. }
  165. private function getType($type)
  166. {
  167. switch ($type) {
  168. case 1:
  169. $type = 'JSAPI';
  170. break;
  171. case 2:
  172. $type = 'NATIVE';
  173. break;
  174. case 3:
  175. $type = 'APP';
  176. break;
  177. case 4:
  178. $type = 'MWEB';
  179. break;
  180. }
  181. return $type;
  182. }
  183. }