Charge.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-02-17
  10. // +—————————————————————————————————————————————————————————————————————
  11. /**
  12. * 充值
  13. */
  14. class Api_Charge extends PhalApi_Api {
  15. public function getRules() {
  16. return array(
  17. 'getAliOrder' => array(
  18. 'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
  19. 'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
  20. 'changeid' => array('name' => 'changeid', 'type' => 'int', 'require' => true, 'desc' => '充值规则ID'),
  21. 'coin' => array('name' => 'coin', 'type' => 'string', 'require' => true, 'desc' => '钻石'),
  22. 'money' => array('name' => 'money', 'type' => 'string', 'require' => true, 'desc' => '充值金额'),
  23. ),
  24. 'getWxOrder' => array(
  25. 'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
  26. 'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
  27. 'changeid' => array('name' => 'changeid', 'type' => 'string', 'require' => true, 'desc' => '充值规则ID'),
  28. 'coin' => array('name' => 'coin', 'type' => 'string', 'require' => true, 'desc' => '钻石'),
  29. 'money' => array('name' => 'money', 'type' => 'string', 'require' => true, 'desc' => '充值金额'),
  30. ),
  31. );
  32. }
  33. /* 获取订单号 */
  34. protected function getOrderid($uid){
  35. $orderid=$uid.'_'.date('YmdHis').rand(100,999);
  36. return $orderid;
  37. }
  38. /**
  39. * 微信支付获取订单号
  40. * @desc 用于 微信支付获取订单号
  41. * @return int code 操作码,0表示成功
  42. * @return array info
  43. * @return string info[0] 支付信息
  44. * @return string msg 提示信息
  45. */
  46. public function getWxOrder() {
  47. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  48. $uid=checkNull($this->uid);
  49. $token=checkNull($this->token);
  50. $changeid=checkNull($this->changeid);
  51. $coin=checkNull($this->coin);
  52. $money=checkNull($this->money);
  53. $checkToken=checkToken($uid,$token);
  54. if($checkToken==700){
  55. $rs['code'] = $checkToken;
  56. $rs['msg'] = '您的登陆状态失效,请重新登陆!';
  57. return $rs;
  58. }
  59. $orderid=$this->getOrderid($uid);
  60. $type=2;
  61. if($coin==0){
  62. $rs['code']=1002;
  63. $rs['msg']='信息错误';
  64. return $rs;
  65. }
  66. $configpri = getConfigPri();
  67. $configpub = getConfigPub();
  68. //配置参数检测
  69. if($configpri['wx_appid']== "" || $configpri['wx_mchid']== "" || $configpri['wx_key']== ""){
  70. $rs['code'] = 1002;
  71. $rs['msg'] = '微信未配置';
  72. return $rs;
  73. }
  74. $orderinfo=array(
  75. "uid"=>$uid,
  76. "touid"=>$uid,
  77. "money"=>$money,
  78. "coin"=>$coin,
  79. "orderno"=>$orderid,
  80. "type"=>$type,
  81. "status"=>0,
  82. "addtime"=>time()
  83. );
  84. $domain = new Domain_Charge();
  85. $info = $domain->getOrderId($changeid,$orderinfo);
  86. if($info==1003){
  87. $rs['code']=1003;
  88. $rs['msg']='订单信息有误,请重新提交';
  89. return $rs;
  90. }else if(!$info){
  91. $rs['code']=1001;
  92. $rs['msg']='订单生成失败';
  93. return $rs;
  94. }
  95. $noceStr = md5(rand(100,1000).time());//获取随机字符串
  96. $time = time();
  97. $paramarr = array(
  98. "appid" => $configpri['wx_appid'],
  99. "body" => "充值{$coin}虚拟币",
  100. "mch_id" => $configpri['wx_mchid'],
  101. "nonce_str" => $noceStr,
  102. "notify_url" => $configpub['site'].'/Appapi/pay/notify_wx',
  103. "out_trade_no"=> $orderid,
  104. "total_fee" => $money*100,
  105. "trade_type" => "APP"
  106. );
  107. $sign = $this -> sign($paramarr,$configpri['wx_key']);//生成签名
  108. $paramarr['sign'] = $sign;
  109. $paramXml = "<xml>";
  110. foreach($paramarr as $k => $v){
  111. $paramXml .= "<" . $k . ">" . $v . "</" . $k . ">";
  112. }
  113. $paramXml .= "</xml>";
  114. $ch = curl_init ();
  115. @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  116. @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
  117. @curl_setopt($ch, CURLOPT_URL, "https://api.mch.weixin.qq.com/pay/unifiedorder");
  118. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  119. @curl_setopt($ch, CURLOPT_POST, 1);
  120. @curl_setopt($ch, CURLOPT_POSTFIELDS, $paramXml);
  121. @$resultXmlStr = curl_exec($ch);
  122. if(curl_errno($ch)){
  123. //print curl_error($ch);
  124. file_put_contents('./wxpay.txt',date('y-m-d H:i:s').' 提交参数信息 ch:'.json_encode(curl_error($ch))."\r\n",FILE_APPEND);
  125. }
  126. curl_close($ch);
  127. $result2 = $this->xmlToArray($resultXmlStr);
  128. if($result2['return_code']=='FAIL'){
  129. $rs['code']=1005;
  130. $rs['msg']=$result2['return_msg'];
  131. return $rs;
  132. }
  133. $time2 = time();
  134. $prepayid = $result2['prepay_id'];
  135. $sign = "";
  136. $noceStr = md5(rand(100,1000).time());//获取随机字符串
  137. $paramarr2 = array(
  138. "appid" => $configpri['wx_appid'],
  139. "noncestr" => $noceStr,
  140. "package" => "Sign=WXPay",
  141. "partnerid" => $configpri['wx_mchid'],
  142. "prepayid" => $prepayid,
  143. "timestamp" => $time2
  144. );
  145. $paramarr2["sign"] = $this -> sign($paramarr2,$configpri['wx_key']);//生成签名
  146. $rs['info'][0]=$paramarr2;
  147. return $rs;
  148. }
  149. /**
  150. * sign拼装获取
  151. */
  152. protected function sign($param,$key){
  153. $sign = "";
  154. ksort($param);
  155. foreach($param as $k => $v){
  156. $sign .= $k."=".$v."&";
  157. }
  158. $sign .= "key=".$key;
  159. $sign = strtoupper(md5($sign));
  160. return $sign;
  161. }
  162. /**
  163. * xml转为数组
  164. */
  165. protected function xmlToArray($xmlStr){
  166. $msg = array();
  167. $postStr = $xmlStr;
  168. $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  169. return $msg;
  170. }
  171. /**
  172. * 支付宝支付获取订单号
  173. * @desc 用于支付宝支付获取订单号
  174. * @return int code 操作码,0表示成功
  175. * @return array info
  176. * @return string info[0].orderid 订单号
  177. * @return string msg 提示信息
  178. */
  179. public function getAliOrder() {
  180. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  181. $uid=checkNull($this->uid);
  182. $token=checkNull($this->token);
  183. $changeid=checkNull($this->changeid);
  184. $coin=checkNull($this->coin);
  185. $money=checkNull($this->money);
  186. $checkToken=checkToken($uid,$token);
  187. if($checkToken==700){
  188. $rs['code'] = $checkToken;
  189. $rs['msg'] = '您的登陆状态失效,请重新登陆!';
  190. return $rs;
  191. }
  192. $configpri=getConfigPri();
  193. if(!$configpri['aliapp_partner']||!$configpri['aliapp_seller_id']||!$configpri['aliapp_key_android']||!$configpri['aliapp_key_ios']){
  194. $rs['code']=1001;
  195. $rs['msg']='支付宝未配置';
  196. return $rs;
  197. }
  198. $orderid=$this->getOrderid($uid);
  199. $type=1;
  200. if($coin==0){
  201. $rs['code']=1002;
  202. $rs['msg']='信息错误';
  203. return $rs;
  204. }
  205. $orderinfo=array(
  206. "uid"=>$uid,
  207. "touid"=>$uid,
  208. "money"=>$money,
  209. "coin"=>$coin,
  210. "orderno"=>$orderid,
  211. "type"=>$type,
  212. "status"=>0,
  213. "addtime"=>time()
  214. );
  215. $domain = new Domain_Charge();
  216. $info = $domain->getOrderId($changeid,$orderinfo);
  217. if($info==1003){
  218. $rs['code']=1003;
  219. $rs['msg']='订单信息有误,请重新提交';
  220. }else if(!$info){
  221. $rs['code']=1001;
  222. $rs['msg']='订单生成失败';
  223. }
  224. $rs['info'][0]['orderid']=$orderid;
  225. return $rs;
  226. }
  227. }