Pay.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use think\Exception;
  7. /**
  8. * 充值配置与充值订单
  9. */
  10. class Pay extends Api
  11. {
  12. protected $noNeedLogin = ['order_notify_base'];
  13. protected $noNeedRight = ['*'];
  14. //支付订单
  15. //微信小程序、微信app下单使用。
  16. public function pay_order(){
  17. $pay_type = input('pay_type','wechat');
  18. $platform = input('platform','miniapp');
  19. $orderid = input('orderid','0');
  20. $uid = $this->auth->id;
  21. $orderinfo = Db::name('order')->where('id',$orderid)->where('user_id',$uid)->find();
  22. //创建订单
  23. $data['user_id'] = $uid;
  24. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  25. $data['order_amount'] = $orderinfo['pay_fee'];
  26. $data['createtime'] = time();
  27. $data['pay_type'] = $pay_type;
  28. $data['order_status'] = 0;
  29. $data['table_name'] = 'order';
  30. $data['table_id'] = $orderid;
  31. $orderid = Db::name('pay_order')->insertGetId($data);
  32. $openid = $this->auth->mini_openid;
  33. //下单
  34. $params = [
  35. 'type' => $pay_type,
  36. 'orderid' => $data['out_trade_no'],
  37. 'title' => '支付订单',
  38. 'amount' => $data['order_amount'],
  39. 'method' => $platform,
  40. 'openid' => $openid,
  41. 'notifyurl' => config('pay_notify_url').'/api/pay/order_notify_base/paytype/'.$pay_type,
  42. 'returnurl' => '',
  43. ];
  44. $res = Service::submitOrder($params);
  45. if($pay_type == 'wechat'){
  46. $this->success('success',json_decode($res,true));
  47. }else{
  48. $this->success('success',$res);
  49. }
  50. }
  51. //异步回调对外方法
  52. public function order_notify_base(){
  53. //验签
  54. $paytype = input('paytype','wechat');
  55. $func = input('func','order_notify_do');
  56. $notify_file = $this->notify_log_start($paytype);
  57. $pay = Service::checkNotify($paytype);
  58. if (!$pay) {
  59. echo '签名错误';
  60. exit;
  61. }
  62. //验证,拿订单号等信息
  63. $data = $pay->verify();
  64. $out_trade_no = $data['out_trade_no'];
  65. //订单查询
  66. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  67. if(empty($info)){
  68. return $pay->success()->send();
  69. exit;
  70. }
  71. if($info['order_status'] != 0)
  72. {
  73. return $pay->success()->send();
  74. exit;
  75. }
  76. //$out_trade_no = 'R230605144624823142892';
  77. //你可以在此编写订单逻辑
  78. $rs = $this->$func($out_trade_no);
  79. if($rs === false){
  80. //不论结果都应返回success
  81. return $pay->success()->send();
  82. exit;
  83. }else{
  84. //不论结果都应返回success
  85. return $pay->success()->send();
  86. exit;
  87. }
  88. //默认
  89. return $pay->success()->send();
  90. exit;
  91. }
  92. //异步逻辑
  93. private function order_notify_do($out_trade_no){
  94. Db::startTrans();
  95. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  96. if (empty($orderInfo)) {
  97. Db::rollback();
  98. return false;
  99. }
  100. if($orderInfo['order_status'] != 0){
  101. Db::rollback();
  102. return false;
  103. }
  104. //逻辑开始
  105. $update = [
  106. 'status'=>10,
  107. 'paytime'=>time(),
  108. 'pay_type'=>$orderInfo['pay_type'],
  109. 'pay_out_trade_no'=>$out_trade_no,
  110. ];
  111. $rs_order = Db::name('order')->where('id',$orderInfo['table_id'])->update($update);
  112. if($rs_order === false){
  113. Db::rollback();
  114. return false;
  115. }
  116. //逻辑结束
  117. //状态
  118. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  119. if($ros === false) {
  120. Db::rollback();
  121. return false;
  122. }
  123. //默认提交
  124. Db::commit();
  125. return true;
  126. }
  127. //异步日志
  128. private function notify_log_start($paytype = 'wechat'){
  129. //记录支付回调数据
  130. ignore_user_abort(); // run script in background
  131. set_time_limit(30);
  132. // 日志文件 start
  133. $log_base_dir = '../paylog/'.$paytype.'/';
  134. if (!is_dir($log_base_dir))
  135. {
  136. mkdir($log_base_dir, 0770, true);
  137. @chmod($log_base_dir, 0770);
  138. }
  139. $notify_file = $log_base_dir.'notify.txt';
  140. if(!file_exists($notify_file)) {
  141. @touch($notify_file);
  142. @chmod($notify_file, 0770);
  143. }
  144. if(filesize($notify_file)>5242880)//大于5M自动切换
  145. {
  146. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  147. }
  148. if(!file_exists($notify_file)) {
  149. @touch($notify_file);
  150. @chmod($notify_file, 0770);
  151. }
  152. // 日志文件 end
  153. //开始写入
  154. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  155. if($_REQUEST && $paytype == 'alipay') {
  156. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  157. } else {
  158. $xml = file_get_contents("php://input");
  159. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  160. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  161. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  162. }
  163. ini_set('display_errors','On');
  164. return $notify_file;
  165. }
  166. //////////////
  167. //微信小程序充值
  168. public function pay_recharge()
  169. {
  170. Db::startTrans();
  171. try {
  172. $pay_type = input('pay_type','wechat');
  173. $platform = input('platform','miniapp');
  174. $id = input('id','0');
  175. $amounts = input('amounts','0.00');
  176. $uid = $this->auth->id;
  177. $companyId = $this->auth->company_id;
  178. if (empty($id) && empty($amounts)) {
  179. throw new Exception('参数错误');
  180. }
  181. $gift_amount = 0.00;
  182. if (!empty($id)) {//验证充值配置
  183. $where['status'] = 1;//上架
  184. $orderinfo = Db::name('recharge_config')->where('id',$id)->where($where)->find();
  185. if (empty($orderinfo)) {
  186. throw new Exception('未获取到充值套餐信息');
  187. }
  188. $order_amount = $orderinfo['price'];
  189. $gift_amount = $orderinfo['giftprice'];
  190. } else {
  191. $isInt = is_int($amounts);
  192. if (!$isInt) {
  193. throw new Exception('请输入整数');
  194. }
  195. if ($amounts < 1) {
  196. throw new Exception('充值金额有误');
  197. }
  198. $order_amount = $amounts;
  199. }
  200. //创建订单
  201. $data['company_id'] = $companyId;
  202. $data['user_id'] = $uid;
  203. $data['out_trade_no'] = createUniqueNo('R',$uid); // 数据库订单号加密
  204. $data['order_amount'] = $order_amount;
  205. $data['gift_amount'] = $gift_amount;
  206. $data['createtime'] = time();
  207. $data['pay_type'] = $pay_type;
  208. $data['order_status'] = 0;
  209. $data['table_name'] = 'recharge_config';
  210. $data['table_id'] = $id;
  211. //$orderid = Db::name('pay_order')->insertGetId($data);
  212. $openid = $this->auth->mini_openid;
  213. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  214. //下单
  215. $params = [
  216. 'type' => $pay_type,
  217. 'orderid' => $data['out_trade_no'],
  218. 'title' => '充值',
  219. 'amount' => $data['order_amount'],
  220. 'method' => $platform,
  221. 'openid' => $openid,
  222. 'notifyurl' => $this->httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/recharge',
  223. 'returnurl' => '',
  224. ];
  225. $res = Service::submitOrder($params);
  226. Db::commit();
  227. if($pay_type == 'wechat'){
  228. $this->success('success',json_decode($res,true));
  229. }else{
  230. $this->success('success',$res);
  231. }
  232. } catch (Exception $e) {
  233. Db::rollback();
  234. $this->error($e->getMessage());
  235. }
  236. }
  237. /**
  238. * 充值到账
  239. * @param $out_trade_no
  240. * @return bool
  241. */
  242. private function recharge($out_trade_no)
  243. {
  244. Db::startTrans();
  245. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  246. if (empty($orderInfo)) {
  247. Db::rollback();
  248. return false;
  249. }
  250. if($orderInfo['order_status'] != 0){
  251. Db::rollback();
  252. return false;
  253. }
  254. $userWalletWhere['user_id'] = $orderInfo['user_id'];
  255. $userWalletWhere['company_id'] = $orderInfo['company_id'];
  256. $userWalletData = Db::name('user_wallet')->where($userWalletWhere)->find();
  257. $before = isset($userWalletData['money']) ? $userWalletData['money'] : 0.00;
  258. $changeValue = bcadd($orderInfo['order_amount'],$orderInfo['gift_amount'],2);
  259. $remain = bcadd($before,$changeValue,2);
  260. $time = time();
  261. //逻辑开始
  262. $userMoneyLogData = [
  263. 'user_id' => $orderInfo['user_id'],
  264. 'company_id' => $orderInfo['company_id'],
  265. 'log_type' => 104, //日志类型 104
  266. 'before' => $before, //之前余额
  267. 'change_value' => $changeValue, //变动金额
  268. 'remain' => $remain, //剩余金额
  269. 'table' => 'pay_order', //数据来源
  270. 'table_id' => $orderInfo['id'], //数据来源ID
  271. 'remark' => '充值', //remark
  272. 'createtime' => $time,
  273. ];
  274. $userMoneyLogRes = Db::name('user_money_log')->insertGetId($userMoneyLogData);
  275. if (!$userMoneyLogRes) {
  276. throw new Exception('充值记录失败');
  277. }
  278. $update = [
  279. 'money' => $remain,
  280. 'updatetime' => $time,
  281. ];
  282. $rs_order = Db::name('user_wallet')->where($userWalletWhere)->update($update);
  283. if(!$rs_order){
  284. Db::rollback();
  285. return false;
  286. }
  287. //逻辑结束
  288. //状态
  289. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  290. if($ros === false) {
  291. Db::rollback();
  292. return false;
  293. }
  294. //默认提交
  295. Db::commit();
  296. return true;
  297. }
  298. }