DedeCMS 是一个基于 PHP+MySQL 的网站内容管理系统(CMS),它提供了丰富的功能和灵活的扩展性,适用于各种类型的网站。微信支付是腾讯公司推出的一种在线支付方式,用户可以通过微信客户端完成支付操作。
原因:DedeCMS 默认不支持微信支付,需要通过插件或自定义代码来实现集成。
解决方法:
<?php
// 调用微信支付 API
function wechat_pay($order_id, $total_fee) {
// 微信支付配置
$appid = 'your_appid';
$mch_id = 'your_mch_id';
$key = 'your_api_key';
// 构造请求参数
$data = array(
'appid' => $appid,
'mch_id' => $mch_id,
'nonce_str' => md5(uniqid()),
'body' => 'Order Payment',
'out_trade_no' => $order_id,
'total_fee' => $total_fee * 100, // 单位为分
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => 'http://yourdomain.com/notify.php',
'trade_type' => 'JSAPI'
);
// 签名
$data['sign'] = get_sign($data, $key);
// 发送请求
$xml = array_to_xml($data);
$response = post_xml('https://api.mch.weixin.qq.com/pay/unifiedorder', $xml);
$result = xml_to_array($response);
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
// 生成前端调用微信支付的参数
$jsApiParameters = $result['prepay_id'];
return $jsApiParameters;
} else {
return false;
}
}
// 辅助函数
function get_sign($data, $key) {
ksort($data);
$str = '';
foreach ($data as $k => $v) {
$str .= "&$k=$v";
}
$str .= "&key=$key";
return strtoupper(md5($str));
}
function array_to_xml($data) {
$xml = "<xml>";
foreach ($data as $k => $v) {
$xml .= "<$k><![CDATA[$v]]></$k>";
}
$xml .= "</xml>";
return $xml;
}
function post_xml($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch => CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function xml_to_array($xml) {
libxml_disable_entity_loader(true);
$result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $result;
}
?>
参考链接:
通过以上步骤,你可以在 DedeCMS 中成功集成微信支付功能。