Pay.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\Request;
  5. use app\BaseController;
  6. use app\model\api\PayTrade;
  7. use library\services\UtilService;
  8. use library\utils\WxpayV2 as wxpayApi;
  9. use library\utils\weixinPay as wxpayApiV3;
  10. use app\model\api\Sys as SysModel;
  11. use WeChatPay\Transformer;
  12. use app\lib\OrderLib;
  13. use think\Exception;
  14. class Pay extends BaseController{
  15. /**
  16. * 微信支付V2异步反馈
  17. * @param Request $request
  18. * @return boolean
  19. */
  20. public function wxpayNotify(Request $request){
  21. $logfile = app()->getRootPath().'public/log/wxnotify.log';
  22. $xmlData = $request->getInput();
  23. if(empty($xmlData)){
  24. return false;
  25. }
  26. // $weixinConfig = (new SysModel)->getWeixinConfig();
  27. // file_put_contents($logfile, PHP_EOL.date("Y-m-d H:i:s").PHP_EOL.$xmlData.PHP_EOL,FILE_APPEND | LOCK_EX);
  28. $wxpay = new wxpayApi();
  29. // $wxpay = new wxpayApi($weixinConfig);
  30. //回调验签
  31. $res = $wxpay->notifyCheckSign($xmlData);
  32. if(!$res || empty($res["data"])){
  33. return false;
  34. }
  35. $result=$res["data"];
  36. //验证返回信息
  37. if(empty($result["return_code"]) || $result["return_code"]!="SUCCESS"){
  38. return false;
  39. }
  40. //验证支付信息
  41. if(empty($result["result_code"]) || $result["result_code"]!="SUCCESS"){
  42. return false;
  43. }
  44. //查询订单
  45. $out_trade_no = $result["out_trade_no"];
  46. $data = $wxpay->searchOrderQuery($out_trade_no);
  47. if(empty($data)){
  48. return false;
  49. }
  50. //支付成功
  51. if($data["trade_state"]=="SUCCESS"){
  52. $payDataInfo = [
  53. 'totalMoney' =>empty($data["total_fee"]) ? "未返回" : $data["total_fee"]/100,
  54. 'payMoney' =>empty($data["cash_fee"]) ? "未返回" : $data["cash_fee"]/100,
  55. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  56. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  57. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  58. "payTime" =>empty($data["time_end"]) ? "未返回" : $data["time_end"],
  59. "attach" =>empty($data["attach"]) ? "未返回" : $data["attach"],
  60. ];
  61. $pay_json = json_encode($payDataInfo);
  62. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  63. }
  64. }
  65. /**
  66. * 微信支付V3异步反馈
  67. * @param Request $request
  68. * @return boolean
  69. */
  70. public function wxpayNotifyV3(Request $request){
  71. $post = UtilService::getMore([
  72. ['id', ''],
  73. ['create_time', ''],
  74. ['event_type',''],
  75. ['resource_type', ''],
  76. ['resource',[]],
  77. ['summary', ''],
  78. ], $request);
  79. if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
  80. return false;
  81. }
  82. if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
  83. return false;
  84. }
  85. if(empty($post["resource"])){
  86. return false;
  87. }
  88. $wxpay = new wxpayApiV3();
  89. $result = $wxpay->aesGcmDecrypt([
  90. "associated_data"=>$post["resource"]["associated_data"],
  91. "nonce"=>$post["resource"]["nonce"],
  92. "ciphertext"=>$post["resource"]["ciphertext"],
  93. ]);
  94. if(!$result){
  95. return false;
  96. }
  97. //商家内部订单号
  98. $out_trade_no = $result["out_trade_no"];
  99. $res = $wxpay->searchOrder($out_trade_no);
  100. if(!$res){
  101. return false;
  102. }
  103. $data = json_decode($res,true);
  104. //支付成功
  105. if($data["trade_state"]=="SUCCESS"){
  106. $payDataInfo = [
  107. 'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"])) ?"未返回":$data["amount"]["total"]/100,
  108. 'payMoney' =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
  109. 'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
  110. 'outTradeNo' =>empty($data["out_trade_no"]) ? "未返回" : $data["out_trade_no"],
  111. "tradeStatus"=>empty($data["trade_state"]) ? "未返回" : $data["trade_state"],
  112. "payTime" =>empty($data["success_time"]) ? "未返回" : $data["success_time"],
  113. ];
  114. $pay_json = json_encode($payDataInfo);
  115. (new OrderLib)->orderPay($out_trade_no, $pay_json);
  116. }
  117. }
  118. }