Unishop.php 7.4 KB

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