Files
BESCMS/public/api/pay/weixin/refund.php
T

95 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 微信退款接口
* 由 Pay::payrefund() require 调用
*
* 可用变量:$data 流水记录,$config 接口配置,$money 退款金额
* 须设置:$return = dr_return_data(...)
*
* 注意:退款需商户号配置 API 证书(apiclient_cert.pem / apiclient_key.pem
*/
$money = abs(isset($money) ? $money : $data['value']);
$transaction_id = trim((string)$data['result']); // 支付成功时写入的微信交易号
$out_trade_no = trim((string)\Phpcmf\Service::C()->member_cache['pay']['prefix'])
. date('YmdHis', (int)$data['inputtime']) . '00' . $data['id'];
$out_refund_no = 'RF' . $data['id'] . date('YmdHis');
$total_fee = (int)round($money * 100);
$refund_fee = $total_fee;
if (!$config || empty($config['appid']) || empty($config['mchid']) || empty($config['key'])) {
$return = dr_return_data(0, dr_lang('微信退款配置不完整'));
} elseif (!$transaction_id && !$out_trade_no) {
$return = dr_return_data(0, dr_lang('缺少微信交易号,无法退款'));
} else {
// API 证书路径(退款必填),默认放在 weixin 目录下
$cert = dirname(__FILE__) . '/cert/apiclient_cert.pem';
$key = dirname(__FILE__) . '/cert/apiclient_key.pem';
if (!empty($config['sslcert_path'])) {
$cert = $config['sslcert_path'];
}
if (!empty($config['sslkey_path'])) {
$key = $config['sslkey_path'];
}
if (!is_file($cert) || !is_file($key)) {
$return = dr_return_data(0, dr_lang('微信退款证书未配置(请放置 apiclient_cert.pem / apiclient_key.pem'));
} else {
if (!defined('APPID')) {
define('APPID', $config['appid']);
}
if (!defined('APPSECRET')) {
define('APPSECRET', isset($config['appsecret']) ? $config['appsecret'] : '');
}
if (!defined('MCHID')) {
define('MCHID', $config['mchid']);
}
if (!defined('KEY')) {
define('KEY', $config['key']);
}
if (!defined('NOTIFY_URL')) {
define('NOTIFY_URL', ROOT_URL . 'api/pay/' . $data['type'] . '/notify_url.php');
}
if (!defined('REPORT_LEVENL')) {
define('REPORT_LEVENL', 0);
}
if (!defined('SSLCERT_PATH')) {
define('SSLCERT_PATH', $cert);
}
if (!defined('SSLKEY_PATH')) {
define('SSLKEY_PATH', $key);
}
require_once dirname(__FILE__) . '/WxPay.Data.php';
require_once dirname(__FILE__) . '/WxPay.Api.php';
$input = new WxPayRefund();
if ($transaction_id) {
$input->SetTransaction_id($transaction_id);
} else {
$input->SetOut_trade_no($out_trade_no);
}
$input->SetTotal_fee($total_fee);
$input->SetRefund_fee($refund_fee);
$input->SetOut_refund_no($out_refund_no);
$input->SetOp_user_id(MCHID);
$result = WxPayApi::refund($input);
if (isset($result['code']) && (int)$result['code'] === 0) {
$return = $result;
} elseif (isset($result['return_code']) && $result['return_code'] == 'SUCCESS'
&& isset($result['result_code']) && $result['result_code'] == 'SUCCESS') {
$return = dr_return_data(1, dr_lang('退款成功'), $result);
} else {
$msg = isset($result['err_code_des']) && $result['err_code_des']
? $result['err_code_des']
: (isset($result['return_msg']) ? $result['return_msg'] : dr_lang('微信退款失败'));
if (isset($result['msg']) && $result['msg']) {
$msg = $result['msg'];
}
$return = dr_return_data(0, $msg, is_array($result) ? $result : []);
}
}
}