You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
2.3 KiB

  1. <?php
  2. ini_set('date.timezone', 'Asia/Shanghai');
  3. error_reporting(E_ERROR);
  4. defined('ROOT_PATH') || define('ROOT_PATH', dirname(dirname(dirname(__DIR__))));
  5. require_once ROOT_PATH . "/wechat/WxPay/lib/WxPay.Api.php";
  6. require_once 'log.php';
  7. require_once ROOT_PATH . '/wechat/Common_fx/Mysql.php';
  8. //初始化日志
  9. $logHandler = new CLogFileHandler("../logs/" . date('Y-m-d') . '.log');
  10. $log = Log::Init($logHandler, 15);
  11. function printf_info($data)
  12. {
  13. foreach ($data as $key => $value) {
  14. echo "<font color='#f00;'>$key</font> : $value <br/>";
  15. }
  16. }
  17. //链接数据库查询需要退款的订单信息
  18. $siteContentConfig = require ROOT_PATH . '/config/moduleConfig/module/site.contants.config.php';
  19. $pdo = conn();
  20. $sql = '' . "SELECT a.*
  21. from order_refund a
  22. join base_supplier b on a.org_id = b.ID
  23. where b.MAIN_CORP_ID in ({$siteContentConfig['main_corp_id']},0)
  24. and a.yet = 0 and a.PAY_SERIAL_NUMBER != ''";
  25. //$sql = "select * from order_refund where yet = 0 and PAY_SERIAL_NUMBER<>''"; //PAY_SERIAL_NUMBER:支付串号(商户单号)
  26. $rs = $pdo->query($sql);
  27. $rowset = $rs->fetchAll(PDO::FETCH_ASSOC);
  28. foreach ($rowset as $v) {
  29. $list[] = $v;
  30. }
  31. //遍历全部订单并退款 此为全额退款 金额不同会退款失败
  32. foreach ($list as $v) {
  33. $out_trade_no = (string)$v['PAY_SERIAL_NUMBER'];
  34. $total_fee = (string)$v['price'] * 100;
  35. $refund_fee = (string)$v['price'] * 100;
  36. $input = new WxPayRefund();
  37. $input->SetOut_trade_no($out_trade_no);
  38. $input->SetTotal_fee($total_fee);
  39. $input->SetRefund_fee($refund_fee);
  40. $input->SetOut_refund_no(WxPayConfig::MCHID . date("YmdHis"));
  41. $input->SetOp_user_id(WxPayConfig::MCHID);
  42. $rs = WxPayApi::refund($input);
  43. //退款给成功后设置退款状态位
  44. if (!isset($rs['err_code'])) {
  45. $sql = '' . "update order_refund set yet = 1 where order_id =" . $v['order_id'];
  46. $rs2 = $pdo->exec($sql);
  47. $info = serialize($rs) . ' ' . $v['MAIN_CORP_ID'];//$out_trade_no.'success\n';
  48. } else {
  49. $info = $out_trade_no . 'error:' . $rs['err_code'] . ' ' . $v['MAIN_CORP_ID'] . '\n';
  50. }
  51. success_notify($info."||".json_encode($rs));
  52. echo $info;
  53. }
  54. function success_notify($result)
  55. {
  56. file_put_contents('./refund_wechat.txt', date("Y-m-d H:i:s") . " " . $result . PHP_EOL, FILE_APPEND);//订单成功后通知后台日志
  57. }