Unishop.php 7.1 KB

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