$value) { echo "$key : $value
"; } } //链接数据库查询需要退款的订单信息 $siteContentConfig = require ROOT_PATH . '/config/moduleConfig/module/site.contants.config.php'; $pdo = conn(); $sql = '' . "SELECT a.* from order_refund a join base_supplier b on a.org_id = b.ID where b.MAIN_CORP_ID in ({$siteContentConfig['main_corp_id']},0) and a.yet = 0 and a.PAY_SERIAL_NUMBER != ''"; //$sql = "select * from order_refund where yet = 0 and PAY_SERIAL_NUMBER<>''"; //PAY_SERIAL_NUMBER:支付串号(商户单号) $rs = $pdo->query($sql); $rowset = $rs->fetchAll(PDO::FETCH_ASSOC); foreach ($rowset as $v) { $list[] = $v; } //遍历全部订单并退款 此为全额退款 金额不同会退款失败 foreach ($list as $v) { $out_trade_no = (string)$v['PAY_SERIAL_NUMBER']; $total_fee = (string)$v['price'] * 100; $refund_fee = (string)$v['price'] * 100; $input = new WxPayRefund(); $input->SetOut_trade_no($out_trade_no); $input->SetTotal_fee($total_fee); $input->SetRefund_fee($refund_fee); $input->SetOut_refund_no(WxPayConfig::MCHID . date("YmdHis")); $input->SetOp_user_id(WxPayConfig::MCHID); $rs = WxPayApi::refund($input); //退款给成功后设置退款状态位 if (!isset($rs['err_code'])) { $sql = '' . "update order_refund set yet = 1 where order_id =" . $v['order_id']; $rs2 = $pdo->exec($sql); $info = serialize($rs) . ' ' . $v['MAIN_CORP_ID'];//$out_trade_no.'success\n'; } else { $info = $out_trade_no . 'error:' . $rs['err_code'] . ' ' . $v['MAIN_CORP_ID'] . '\n'; } success_notify($info."||".json_encode($rs)); echo $info; } function success_notify($result) { file_put_contents('./refund_wechat.txt', date("Y-m-d H:i:s") . " " . $result . PHP_EOL, FILE_APPEND);//订单成功后通知后台日志 }