Unishop.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. use think\Db;
  6. use addons\epay\library\Service;
  7. use think\Exception;
  8. use addons\unishop\extend\Hashids;
  9. /**
  10. * 支付订单,回调订单
  11. */
  12. class Unishop extends Api
  13. {
  14. protected $noNeedLogin = ['order_notify_base'];
  15. protected $noNeedRight = ['*'];
  16. //支付订单
  17. public function pay_order(){
  18. $apilimit = $this->apiLimit();
  19. if(!$apilimit){
  20. $this->error('操作频繁');
  21. }
  22. $pay_type = input('pay_type','wechat');
  23. $platform = input('platform','mini');
  24. $orderid = input('order_id',0);
  25. if(empty($orderid)){
  26. $this->error();
  27. }
  28. $orderid = Hashids::decodeHex($orderid);
  29. if(!in_array($pay_type,['wechat','alipay','wallet'])){
  30. $this->error();
  31. }
  32. $uid = $this->auth->id;
  33. $map = [
  34. 'id' => $orderid,
  35. 'user_id' => $uid,
  36. 'status' => 1,
  37. 'have_paid'=> 0,
  38. ];
  39. $orderinfo = Db::name('unishop_order')->where($map)->find();
  40. if(empty($orderinfo)){
  41. $this->error('请刷新重试');
  42. }
  43. //余额支付
  44. if($pay_type == 'wallet'){
  45. Db::startTrans();
  46. //扣钱
  47. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',-$orderinfo['total_price'],31,'商城购物','unishop_order',$orderid);
  48. if($rs_wallet['status'] == false){
  49. Db::rollback();
  50. $this->error($rs_wallet['msg']);
  51. }
  52. //订单改为已支付
  53. $order_update['pay_type'] = 2;
  54. $order_update['have_paid'] = time();
  55. //水饺项目自动发货
  56. $order_update['have_delivered'] = time();
  57. $update_rs = Db::name('unishop_order')->where($map)->update($order_update);
  58. if(!$update_rs){
  59. Db::rollback();
  60. $this->error('支付失败,重试一下吧');
  61. }
  62. Db::commit();
  63. $result = [
  64. 'pay_type' => $pay_type,
  65. 'pay_params' => '',
  66. ];
  67. $this->success(1,$result);
  68. }
  69. //创建订单
  70. $data = [];
  71. $data['user_id'] = $uid;
  72. $data['out_trade_no'] = createUniqueNo('U',$uid); // 数据库订单号加密
  73. $data['order_amount'] = $orderinfo['total_price'];
  74. $data['createtime'] = time();
  75. $data['pay_type'] = $pay_type;
  76. $data['platform'] = $platform;
  77. $data['order_status'] = 0;
  78. $data['table_name'] = 'unishop_order';
  79. $data['table_id'] = $orderid;
  80. $orderid = Db::name('pay_order')->insertGetId($data);
  81. //下单
  82. $params = [
  83. 'type' => $pay_type,
  84. 'orderid' => $data['out_trade_no'],
  85. 'title' => '支付订单',
  86. 'amount' => $data['order_amount'],
  87. // 'amount' => 0.01,
  88. 'method' => $platform,
  89. 'notifyurl' => config('pay_notify_url').'/api/unishop/order_notify_base/paytype/'.$pay_type,
  90. 'returnurl' => '',
  91. 'openid' => $this->auth->wxmini_openid,
  92. ];
  93. $res = Service::submitOrder($params);
  94. $result = [
  95. 'pay_type'=> $pay_type,
  96. 'pay_params'=> $pay_type
  97. ];
  98. if($pay_type == 'wechat'){
  99. $result['pay_params'] = json_decode($res,true);
  100. }else{
  101. $result['pay_params'] = $res;
  102. }
  103. $this->success(1,$result);
  104. }
  105. public function test(){
  106. $out_trade_no = input('out_trade_no','');
  107. $paytype = 'wechat';
  108. $rs = $this->order_notify_do($out_trade_no,$paytype);
  109. echo '完成';
  110. }
  111. //异步回调对外方法
  112. public function order_notify_base(){
  113. //验签
  114. $paytype = input('paytype','alipay');
  115. $paytype = 'wechat';
  116. $notify_file = $this->notify_log_start($paytype);
  117. $pay = Service::checkNotify($paytype);
  118. if (!$pay) {
  119. echo '签名错误';
  120. exit;
  121. }
  122. //验证,拿订单号等信息
  123. $data = $pay->verify();
  124. $out_trade_no = $data['out_trade_no'];
  125. //订单查询
  126. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  127. if(empty($info)){
  128. return $pay->success()->send();
  129. exit;
  130. }
  131. if($info['order_status'] != 0)
  132. {
  133. return $pay->success()->send();
  134. exit;
  135. }
  136. //你可以在此编写订单逻辑
  137. $rs = $this->order_notify_do($out_trade_no,$paytype);
  138. if($rs === false){
  139. //不论结果都应返回success
  140. return $pay->success()->send();
  141. exit;
  142. }else{
  143. //不论结果都应返回success
  144. return $pay->success()->send();
  145. exit;
  146. }
  147. //默认
  148. return $pay->success()->send();
  149. exit;
  150. }
  151. //异步逻辑
  152. private function order_notify_do($out_trade_no,$paytype){
  153. Db::startTrans();
  154. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  155. if (empty($orderInfo)) {
  156. Db::rollback();
  157. return false;
  158. }
  159. if($orderInfo['order_status'] != 0){
  160. Db::rollback();
  161. return false;
  162. }
  163. //逻辑开始
  164. $nowtime = time();
  165. $paytype_enum = [
  166. 'wechat' => 3,
  167. 'alipay' => 4,
  168. ];
  169. //订单改为已支付
  170. $update = [
  171. 'have_paid'=>$nowtime,
  172. 'pay_type' =>$paytype_enum[$paytype],
  173. 'pay_out_trade_no' =>$out_trade_no,
  174. //水饺项目自动发货
  175. 'have_delivered' => $nowtime
  176. ];
  177. $rs_order = Db::name('unishop_order')->where('id',$orderInfo['table_id'])->update($update);
  178. if($rs_order === false){
  179. Db::rollback();
  180. return false;
  181. }
  182. //逻辑结束
  183. //状态
  184. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  185. if($ros === false) {
  186. Db::rollback();
  187. return false;
  188. }
  189. //默认提交
  190. Db::commit();
  191. return true;
  192. }
  193. //异步日志
  194. private function notify_log_start($paytype = 'wechat'){
  195. //记录支付回调数据
  196. ignore_user_abort(); // run script in background
  197. set_time_limit(30);
  198. // 日志文件 start
  199. $log_base_dir = '../epaylog/'.$paytype.'/';
  200. if (!is_dir($log_base_dir))
  201. {
  202. mkdir($log_base_dir, 0770, true);
  203. @chmod($log_base_dir, 0770);
  204. }
  205. $notify_file = $log_base_dir.'notify.txt';
  206. if(!file_exists($notify_file)) {
  207. @touch($notify_file);
  208. @chmod($notify_file, 0770);
  209. }
  210. if(filesize($notify_file)>5242880)//大于5M自动切换
  211. {
  212. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  213. }
  214. if(!file_exists($notify_file)) {
  215. @touch($notify_file);
  216. @chmod($notify_file, 0770);
  217. }
  218. // 日志文件 end
  219. //开始写入
  220. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  221. if($_REQUEST && $paytype == 'alipay') {
  222. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  223. } else {
  224. $xml = file_get_contents("php://input");
  225. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  226. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  227. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  228. }
  229. ini_set('display_errors','On');
  230. return $notify_file;
  231. }
  232. }