YftPay.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by 傲慢与偏见.
  4. * OSUser: D-L
  5. * Date: 2017/10/12
  6. * Time: 21:08
  7. */
  8. namespace App\Controllers;
  9. use App\Models\User;
  10. use App\Models\YftOrder;
  11. use App\Services\Auth;
  12. use App\Utils\YftOrderNumUtil;
  13. class YftPay extends BaseController
  14. {
  15. private $user;
  16. public function __construct()
  17. {
  18. $this->user = Auth::getUser();
  19. }
  20. public function yft($request, $response, $args)
  21. {
  22. $price = $request->getParams()['price'];
  23. return $this->view()->assign('price', $price)->display('user/yft.tpl');
  24. }
  25. public function yftPay($request, $response, $args)
  26. {
  27. $yftLib = new QuickPayFunction();
  28. $pay_config = new PayConfig();
  29. $pay_config->init();
  30. /**************************请求参数**************************/
  31. //订单名称
  32. $subject = $request->getParams()['subject'];//必填
  33. //付款金额
  34. $total_fee = $request->getParams()['total_fee'];//必填 需为整数
  35. //服务器异步通知页面路径
  36. $notify_url = $request->getUri()->getScheme()."://".$request->getUri()->getHost().$pay_config->pay_config['notify_url'];
  37. //需http://格式的完整路径,不能加?id=123这类自定义参数
  38. //页面跳转同步通知页面路径
  39. $return_url = $request->getUri()->getScheme()."://".$request->getUri()->getHost().$pay_config->pay_config["return_url"];
  40. //需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/
  41. $secret = $pay_config->pay_config["secret"];
  42. $accesskey = $pay_config->pay_config["accesskey"];
  43. //生成订单号
  44. $ss_order_no = YftOrderNumUtil::generate_yftOrder(8);
  45. /************************************************************/
  46. //构造要请求的参数数组,无需改动
  47. $parameter = [];
  48. if ($pay_config->pay_config["type"] == "aliPay") {
  49. $parameter = [
  50. "total_fee" => $total_fee,
  51. "notify_url" => $notify_url,
  52. "return_url" => $return_url,
  53. "secret" => $secret,
  54. "out_trade_no" => $ss_order_no
  55. ];
  56. } else {
  57. $parameter = [
  58. "secret" => $secret,
  59. "notify_url" => $notify_url,
  60. "accesskey" => $accesskey,
  61. "return_url" => $return_url,
  62. "subject" => $subject,
  63. "total_fee" => $total_fee
  64. ];
  65. }
  66. //向数据库插入订单信息
  67. $yft_order_info = new YftOrder();
  68. $yft_order_info->user_id = $this->user->id;
  69. $yft_order_info->ss_order = $ss_order_no;
  70. $yft_order_info->price = $total_fee;
  71. $yft_order_info->state = 0;
  72. $yft_order_info->save();
  73. //建立请求
  74. $html_text = $yftLib->buildRequestForm($parameter, $ss_order_no,$pay_config);
  75. return $html_text;
  76. }
  77. public function yftPayResult($request, $response, $args)
  78. {
  79. $newResponse = $response->withStatus(302)->withHeader('Location', '/user/code');
  80. $yftLib = new QuickPayFunction();
  81. $pay_config = new PayConfig();
  82. $pay_config->init();
  83. //价格
  84. $total_fee = $request->getParams()['total_fee'];//必填
  85. //付款状态
  86. $trade_status = $request->getParams()['trade_status'];//必填
  87. //加密验证字符串
  88. $sign = $request->getParams()['sign'];//必填
  89. //易付通返回的订单号
  90. $yft_order_no = $request->getParams()['yft_order_no'];
  91. //面板生成的订单号
  92. $ss_order_no = $request->getParams()['ss_order_no'];//必填
  93. $verifyNotify = $yftLib->md5Verify(floatval($total_fee), $trade_status, $pay_config->pay_config['secret'], $pay_config->pay_config['accesskey'], $sign);
  94. if ($verifyNotify) {//验证成功
  95. if ($_REQUEST['trade_status'] == 'success') {
  96. /*
  97. 加入您的入库及判断代码;
  98. >>>>>>>!!!为了保证数据传达到回调地址,会请求4次。所以必须要先判断订单状态,然后再插入到数据库,这样后面即使请求3次,也不会造成订单重复!!!!<<<<<<<
  99. 判断返回金额与实金额是否想同;
  100. 判断订单当前状态;
  101. 完成以上才视为支付成功
  102. */
  103. $price = $request->getParams()['total_fee'];
  104. $payInfo = YftOrder::where('ss_order', '=', $ss_order_no)->orderBy('id', 'desc')->first();
  105. $user = User::where('id', '=', $payInfo->user_id)->orderBy('id', 'desc')->first();
  106. if ($payInfo != null && $payInfo->state == 0) {
  107. $old = $user->money;
  108. $user->money = $price + $old;
  109. $user->save();
  110. $payInfo->yft_order = $yft_order_no;
  111. $payInfo->state = 1;
  112. $payInfo->save();
  113. } else {
  114. echo "订单号异常!请联系管理员!";
  115. sleep(2);
  116. return $newResponse;
  117. }
  118. echo "支付成功";
  119. return $newResponse;
  120. } else {
  121. echo "支付失败";
  122. return $newResponse;
  123. }
  124. } else {
  125. //验证失败
  126. echo "订单信息异常!请联系管理员";
  127. sleep(2);
  128. return $newResponse;
  129. }
  130. }
  131. public function yftOrder($request, $response, $args)
  132. {
  133. $pageNum = 1;
  134. if (isset($request->getQueryParams()["page"])) {
  135. $pageNum = $request->getQueryParams()["page"];
  136. }
  137. $orderList = YftOrder::where("user_id", $this->user->id)->orderBy("id", "asc")->paginate(15, ['*'], 'page', $pageNum);
  138. $count = sizeof(YftOrder::where("user_id", $this->user->id)->get());
  139. $countPage = ceil($count / 15);
  140. $orderList->setPath('/user/yftOrder');
  141. return $this->view()->assign('orderList', $orderList)->assign('countPage', $countPage)->assign('currentPage', $pageNum)->display('user/yftOrder.tpl');
  142. }
  143. /**
  144. * @desc 管理员查看所有充值记录
  145. * @param $request
  146. * @param $response
  147. * @param $args
  148. * @return mixed
  149. */
  150. public function yftOrderForAdmin($request, $response, $args)
  151. {
  152. $pageNum = 1;
  153. if (isset($request->getQueryParams()["page"])) {
  154. $pageNum = $request->getQueryParams()["page"];
  155. }
  156. $orderList = YftOrder::where("price",">=", 0)->orderBy("id", "asc")->paginate(15, ['*'], 'page', $pageNum);
  157. $count = sizeof(YftOrder::where("price",">=", 0)->get());
  158. $countPage = ceil($count / 15);
  159. $orderList->setPath('/admin/yftOrder');
  160. return $this->view()->assign('orderList', $orderList)->assign('countPage', $countPage)->assign('currentPage', $pageNum)->display('admin/yftOrder.tpl');
  161. }
  162. }