Unishop.php 7.0 KB

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