本DEMO演示了PHP支付宝和微信扫码在线支付,支付成功后,在回调地址显示支付相关信息。
操作步骤:
1.修改配置文件 Application/common/conf/config.php
$arr = array(
‘DB_TYPE‘ => ‘mysql‘,
‘DB_HOST‘ => "localhost",
‘DB_NAME‘ => ‘demo‘,
‘DB_USER‘ => "sucaihuo.com",
‘DB_PWD‘ => "sucaihuo.com",
‘DB_PORT‘ => 3306,
‘DB_PREFIX‘ => ‘‘,
);
2.修改支付宝和微信支付配置信息Application/home/conf/config.php
支付宝请到支付宝商户官网申请:https://b.alipay.com/?ynsrc=zhuzhanA,微信支付配置申请:https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN
$arr = array(
‘URL_ROUTER_ON‘ => true,
/* 支付设置 */
‘payment‘ => array(
‘alipay‘ => array(
// 收款账号邮箱
‘email‘ => ‘sucaihuo@126.com‘,
// 加密key,开通支付宝账户后给予
‘key‘ => ‘ggo084pb84gl43qnw82a39n9b7r1jq2m‘,
// 合作者ID,支付宝有该配置,开通易宝账户后给予
‘partner‘ => ‘2088901006538525‘,
//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
‘seller_id‘ => ‘2088901006538525‘,
//签名方式
‘sign_type‘ => strtoupper(‘MD5‘),
//字符编码格式 目前支持utf-8
‘input_charset‘ => strtolower(‘utf-8‘),
// 产品类型,无需修改
‘service‘ => ‘create_direct_pay_by_user‘,
// 支付类型 ,无需修改
‘payment_type‘ => ‘1‘,
),
‘alipaywap‘ => array(
// 收款账号邮箱
‘email‘ => ‘sucaihuo@126.com‘,
// 加密key,开通支付宝账户后给予
‘key‘ => ‘ggo084pb84gl43qnw82a39n9b7r1jq2m‘,
// 合作者ID,支付宝有该配置,开通易宝账户后给予
‘partner‘ => ‘2088901006538525‘,
//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
‘seller_id‘ => ‘2088901006538525‘,
//签名方式
‘sign_type‘ => strtoupper(‘MD5‘),
//字符编码格式 目前支持utf-8
‘input_charset‘ => strtolower(‘utf-8‘),
// 产品类型,无需修改
‘service‘ => ‘alipay.wap.create.direct.pay.by.user‘,
// 支付类型 ,无需修改
‘payment_type‘ => ‘1‘,
),
‘wechatjspai‘ => array(
‘APPID‘ => ‘wx422126b0b62bbfcfc‘,
‘MCHID‘ => ‘1349825901‘,
‘KEY‘ => ‘2088901006538525‘,
‘APPSECRET‘ => ‘45843e705995a12106155f4c26f716dc‘,
),
)
);
只要操作以上两个步骤,即可调通支付宝和微信在线支付,以下为支付代码教程。
订单生成代码如下: Application\Home\Controller\PayController.class.php
public function submit() {
$paytype = I("post.paytype");
$data[‘order_money‘] = I("post.money", 1);//订单金额
$data[‘order_no‘] = date("YmdHis") . rand(1000, 9999);//订单号
$data[‘pay_type‘] = $paytype;
$data[‘addtime‘] = time();
M("order")->add($data);
$site_url = ‘http://‘ . $_SERVER[‘HTTP_HOST‘] . $_SERVER[‘PHP_SELF‘];
$dir = dirname($site_url);
$data[‘url_notify‘] = $dir . "/Notify/pay_alipay";//回调地址
$data[‘url_return‘] = $dir . "/Pay/order_detail";//返回地址
$data[‘title‘] = "标题" . $data[‘order_no‘];
$data[‘body‘] = "主体内容" . $data[‘order_no‘];
if ($paytype == ‘alipay‘) {
$this->alipay_jump($data);
} elseif ($paytype == ‘wechat_code‘) {
$data[‘url_notify‘] = $dir . "/Notify/pay_weixin";
$this->wechat_jump($data);
}
}
支付成功回调,更改订单状态为已付款: Application\Home\Controller\NotifyController.class.php
public function pay_weixin() {
$simple = json_decode(json_encode(simplexml_load_string($GLOBALS[‘HTTP_RAW_POST_DATA‘], ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true);
$notify_data[‘order_no‘] = $notify_data[‘trade_no‘] = $simple[‘out_trade_no‘];
$notify_data[‘third_id‘] = $simple[‘transaction_id‘];
$notify_data[‘pay_money‘] = $simple[‘total_fee‘];
$notify_data[‘payment_method‘] = ‘weixin‘;
// $sign = $simple[‘sign‘];
// file_put_contents(‘ac_simple.txt‘, json_encode($simple));
// file_put_contents(‘ac_notify_data.txt‘, json_encode($notify_data));
$this->order_pay($notify_data);
}
public function pay_alipay() {
$notify_data[‘order_no‘] = $notify_data[‘trade_no‘] = I("post.out_trade_no");
$notify_data[‘third_id‘] = I("post.trade_no");
$notify_data[‘pay_money‘] = I("post.total_fee");
$notify_data[‘payment_method‘] = ‘alipay‘;
$this->order_pay($notify_data);
file_put_contents(‘ac_notify_data.txt‘, json_encode($_REQUEST));
}
/**
* 支付结果返回
*/
public function order_pay($data_order) {
$order_no = $data_order[‘order_no‘];
if ($order_no == ‘‘) {
return false;
}
$order_info = M(‘order‘)->where(array("order_no" => $order_no))->find();
if ($order_info[‘state‘] == 0) {
$data_order[‘update_time‘] = $_SERVER [‘REQUEST_TIME‘];
$data_order[‘state‘] = 1; // 已付款
M(‘order‘)->where(array("order_no" => $order_no))->save($data_order);
}
}