最近做了微信的手机登陆、手机支付, 手机微信登陆前面已经介绍过了, 现在介绍下微信支付。
这个接口是完全适应于ecshop, 以一个插件的形式(用的jsapi), 但是在其他地方的道理是一样的。
先来看下在ecshop中的目录结构
其中 wxpay这个文件是官网提供的, 放在ecshop的根目录下面的 includes/modules下面。
插件代码在payment文件下面, 命名为 wxpay.php (注意这个命名,不要改)。
然后看下 wxpay.php 这个的具体的代码。
<?php
/**
* ECSHOP 微信插件
* ============================================================================
* 微信支付
* author: ZHANGLIN
* ============================================================================
*/
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
$payment_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/payment/wxpay.php';
if (file_exists($payment_lang))
{
global $_LANG;
include_once($payment_lang);
}
/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
$i = isset($modules) ? count($modules) : 0;
/* 代码 */
$modules[$i]['code'] = basename(__FILE__, '.php');
/* 描述对应的语言项 */
$modules[$i]['desc'] = 'wxpay_desc';
/* 是否支持货到付款 */
$modules[$i]['is_cod'] = '0';
/* 是否支持在线支付 */
$modules[$i]['is_online'] = '1';
/* 作者 */
$modules[$i]['author'] = 'ZHANGLIN';
/* 网址 */
$modules[$i]['website'] = 'https://mp.weixin.qq.com';
/* 版本号 */
$modules[$i]['version'] = '1.0.0';
/* 配置信息 */
$modules[$i]['config'] = array(
array('name' => 'app_id', 'type' => 'text', 'value' => ''),
array('name' => 'paysign_key', 'type' => 'text', 'value' => ''),
array('name' => 'sign_type', 'type' => 'text', 'value' => 'sha1'),
array('name' => 'partner_key', 'type' => 'text', 'value' => ''),
array('name' => 'app_sercert', 'type' => 'text', 'value' => ''),
array('name' => 'parener_id', 'type' => 'text', 'value' => ''),
);
return;
}
/**
* 类
*/
class wxpay
{
/**
* 构造函数
*
* @access public
* @param
*
* @return void
*/
function wxpay()
{
}
function __construct()
{
$this->wxpay();
}
/**
* 生成支付代码
* @param array $order 订单信息
* @param array $payment 支付方式信息
*/
function get_code($order, $payment)
{
if (!defined('EC_CHARSET'))
{
$charset = 'utf-8';
}
else
{
$charset = EC_CHARSET;
}
//定义配置常量
define('APPID' , $payment['app_id']); //appid
define('APPKEY' , $payment['paysign_key']); //paysign key
define('SIGNTYPE', $payment['sign_type']); //method
define('PARTNERKEY', $payment['partner_key']);//通加密串
define('APPSERCERT', $payment['app_sercert']);
define('PARTNERID', $payment['parener_id']);
include_once(ROOT_PATH . "includes/modules/wxpay/WxPayHelper.php");
$commonUtil = new CommonUtil();
$wxPayHelper = new WxPayHelper();
$wxPayHelper->setParameter("bank_type", "WX");
$wxPayHelper->setParameter("body", '马尼尔商品');
$wxPayHelper->setParameter("partner", PARTNERID);
$wxPayHelper->setParameter("out_trade_no", $order['order_sn']);
$wxPayHelper->setParameter("total_fee", $order['order_amount']*100);
$wxPayHelper->setParameter("fee_type", "1");
$wxPayHelper->setParameter("notify_url", return_url(basename(__FILE__, '.php')));
$wxPayHelper->setParameter("spbill_create_ip", real_ip());
$wxPayHelper->setParameter("input_charset", "GBK");
$package = $wxPayHelper->create_biz_package();
echo '<script language="javascript">
function callpay()
{
WeixinJSBridge.invoke("getBrandWCPayRequest",'.$package.',function(res){
WeixinJSBridge.log(res.err_msg);
alert(res.err_code+res.err_desc+res.err_msg);
});
}
</script> ';
$button = '<button type="button" class="btn_white" onclick="callpay()">微信支付</button>';
return $button;
}
/**
* 响应操作
*/
function respond()
{
if ( isset($_GET) )
{
//支付信息
$sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('payment') .
" WHERE pay_code = 'wxpay' AND enabled = 1";
$payment = $GLOBALS['db']->getRow($sql);
$payment = unserialize_config($payment['pay_config']);
//=======================
// 注意这里要作一个判断, 返回回来的值是否是微信支付成功后返回的值
//========================
$order_sn = trim($_GET['out_trade_no']);
$order_id = $GLOBALS['db']->getOne("SELECT order_id FROM " . $GLOBALS['ecs']->table('order_info') . " WHERE order_sn = '$order_sn'");
$log_id = $GLOBALS['db']->getOne("SELECT log_id FROM " . $GLOBALS['ecs']->table('pay_log') . " WHERE order_id = '$order_id'");
/* 改变订单状态 */
order_paid($log_id, 2);
return true;
}
else
{
return false;
}
}
}
?>
文中涉及到的附件下载: