禾将支付宝支付流程(h5),在此记录一下
1在支付页面发起请求
core.json('lotterys/submit', {
type: 'alipay',//支付类型,支付宝
couponid: 0,
user_id:user_id,//用户id
lottery_id:lottery_id,
money:money,//金额
name:name,
phone:tel,
site:address,
}, function (rjson) {
if (rjson.status != 1) {
$('.btn-pay').removeAttr('submit');
FoxUI.toast.show(rjson.result.message);
return
}
if (core.ish5app()) {
appPay('alipay', rjson.result.logno, money, '1', null, true)
} else {
location.href = core.getUrl('order/pay_alipay', {//调起支付
orderid: rjson.result.logno,
type: 0,
url: rjson.result.alipay.url
})
}
}, true, true);
2.支付接口(可以直接复制,只需要修改 $log里的type)
public function submit()
{
global $_W, $_GPC;
$set = $_W['shopset'];
if (empty($set['trade']['minimumcharge'])||$set['trade']['minimumcharge']<=0) {
$minimumcharge = 1;
} else {
$minimumcharge = $set['trade']['minimumcharge'];
}
$money = floatval($_GPC['money']);
$sql = "DELETE FROM " . tablename('ewei_shop_member_log') . " WHERE openid= '{$_W['openid']}' AND status = 0 AND uniacid = {$_W['uniacid']} AND createtime < (unix_timestamp()-86400)";
pdo_fetch($sql);
$logno = m('common')->createNO('member_log', 'logno', 'RC');
$data['user_id']=$_GPC['user_id'];
$data['lottery_id']=$_GPC['lottery_id'];
$data['money']=$_GPC['money'];
$data['name']=$_GPC['name'];
$data['phone']=$_GPC['phone'];
$data['site']=$_GPC['site'];
$log = array(
'uniacid' => $_W['uniacid'],
'logno' => $logno,
'title' => $set['shop']['name'] . "参与抽奖",
'openid' => $_W['openid'],
'money'=>$money,
'type' => 34,
'createtime' => time(),
'status' => 0,
'data'=>serialize($data),
'couponid' => intval($_GPC['couponid'])
);
pdo_insert('ewei_shop_member_log', $log);
$logid = pdo_insertid();
$type = $_GPC['type'];
if (empty($logid) || (int)$logid<1) {
show_json(0, '抽奖订单创建失败请重试!');
}
if(is_h5app()){
show_json(1, array(
'logno'=>$logno,
'money'=>$money
));
}
$set = m('common')->getSysset(array('shop', 'pay'));
$set['pay']['weixin'] = !empty($set['pay']['weixin_sub']) ? 1 : $set['pay']['weixin'];
$set['pay']['weixin_jie'] = !empty($set['pay']['weixin_jie_sub']) ? 1 : $set['pay']['weixin_jie'];
if ($type == 'wechat') {
if (!is_weixin()) {
show_json(0, '非微信环境!');
}
if (empty($set['pay']['weixin'])&&empty($set['pay']['weixin_jie'])) {
show_json(0, '未开启微信支付!');
}
$wechat = array('success' => false);
$jie = intval($_GPC['jie']);
$params = array();
$params['tid'] = $log['logno'];
$params['user'] = $_W['openid'];
$params['fee'] = $money;
$params['title'] = $log['title'];
if (isset($set['pay']) && $set['pay']['weixin'] == 1&&$jie!==1) {
load()->model('payment');
$setting = uni_setting($_W['uniacid'], array('payment'));
$options = array();
if (is_array($setting['payment'])) {
$options = $setting['payment']['wechat'];
$options['appid'] = $_W['account']['key'];
$options['secret'] = $_W['account']['secret'];
}
$wechat = m('common')->wechat_build($params, $options, 1);
if (!is_error($wechat)) {
$wechat['success'] = true;
if (!empty($wechat['code_url'])){
$wechat['weixin_jie'] = true;
}else{
$wechat['weixin'] = true;
}
}
}
if ((isset($set['pay']) && $set['pay']['weixin_jie'] == 1&& !$wechat['success'])||$jie===1) {
$params['tid'] = $params['tid'].'_borrow';
$sec = m('common')->getSec();
$sec =iunserializer($sec['sec']);
$options = array();
$options['appid'] = $sec['appid'];
$options['mchid'] = $sec['mchid'];
$options['apikey'] = $sec['apikey'];
if (!empty($set['pay']['weixin_jie_sub']) && !empty($sec['sub_secret_jie_sub'])){
$wxuser = m('member')->wxuser($sec['sub_appid_jie_sub'],$sec['sub_secret_jie_sub']);
$params['openid'] = $wxuser['openid'];
}elseif(!empty($sec['secret'])){
$wxuser = m('member')->wxuser($sec['appid'],$sec['secret']);
$params['openid'] = $wxuser['openid'];
}
$wechat = m('common')->wechat_native_build($params, $options, 1);
if (!is_error($wechat)) {
$wechat['success'] = true;
if (!empty($params['openid'])){
$wechat['weixin'] = true;
}else{
$wechat['weixin_jie'] = true;
}
}
}
$wechat['jie'] = $jie;
if (!$wechat['success']) {
show_json(0, '微信支付参数错误!');
}
show_json(1, array(
'wechat' => $wechat,
'logid' => $logid
));
} else if ($type == 'alipay') {
$alipay = array('success' => false);
$params = array();
$params['tid'] = $log['logno'];
$params['user'] = $_W['openid'];
$params['fee'] = $money;
$params['title'] = $log['title'];
load()->func('communication');
load()->model('payment');
$setting = uni_setting($_W['uniacid'], array('payment'));
if (is_array($setting['payment'])) {
$options = $setting['payment']['alipay'];
$alipay = m('common')->alipay_build($params, $options, 1, $_W['openid']);
if (!empty($alipay['url'])) {
$alipay['url'] = urlencode($alipay['url']);
$alipay['success'] = true;
}
}
list(,$payment) = m('common')->public_build();
if ($payment['type'] == '4'){
$params = array(
'service' => 'pay.alipay.native',
'body' => $params['title'],
'out_trade_no' => $params['tid'],
'total_fee' => $money
);
if (!empty($order['ordersn2'])) {
$params['out_trade_no'] = $log['logno'] . '_B';
} else {
$params['out_trade_no'] = $log['logno'] . '_borrow';
}
$redis = redis();
if (!is_error($redis)) {
$cacheResult = $redis->get($params['tid']);
$cacheResult = json_decode($cacheResult, true);
if ($cacheResult['service'] == $params['service'] && $params['total_fee'] == $cacheResult['total_fee']) {
$AliPay = $cacheResult['result'];
}
}
if (empty($Alipay)) {
$AliPay = m('pay')->build($params, $payment, 1);
}
if (!empty($AliPay) && !is_error($AliPay)){
$alipay['url'] = urlencode($AliPay['code_url']);
$alipay['success'] = true;
}
}
show_json(1, array('alipay' => $alipay,'logid' => $logid, 'logno'=>$logno));
}
}
3.支付完成回调(在这个文件里修改)
public function recharge_complete()
{
global $_W;
global $_GPC;
$fromwechat = intval($_GPC['fromwechat']);
$logno = trim($_GPC['out_trade_no']);
$notify_id = trim($_GPC['notify_id']);
$sign = trim($_GPC['sign']);
$set = m('common')->getSysset(array('shop', 'pay'));
if (is_h5app()) {
$sec = m('common')->getSec();
$sec = iunserializer($sec['sec']);
$public_key = $sec['app_alipay']['public_key'];
if (empty($_GET['alidata'])) {
$this->message('支付出现错误,请重试(1)!', mobileUrl('member'));
}
if (empty($set['pay']['app_alipay']) || empty($public_key)) {
$this->message('支付出现错误,请重试(2)!', mobileUrl('order'));
}
$alidata = base64_decode($_GET['alidata']);
$alidata = json_decode($alidata, true);
$alisign = m('finance')->RSAVerify($alidata, $public_key, false);
$logno = $this->str($alidata['out_trade_no']);
if ($alisign == 0) {
$this->message('支付出现错误,请重试(3)!', mobileUrl('member'));
}
$transid = $alidata['trade_no'];
}
else {
if (empty($logno)) {
$this->message(array('message' => '支付出现错误,请重试(支付验证失败1)!', 'buttondisplay' => $fromwechat ? false : true), $fromwechat ? NULL : mobileUrl('member'));
}
if (empty($set['pay']['alipay'])) {
$this->message(array('message' => '支付出现错误,请重试(未开启支付宝支付)!', 'buttondisplay' => $fromwechat ? false : true), $fromwechat ? NULL : mobileUrl('member'));
}
if (!m('finance')->isAlipayNotify($_GET)) {
$log = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member_log') . ' WHERE `logno`=:logno and `uniacid`=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':logno' => $logno));
if (!empty($log) && !empty($log['status'])) {
if ($fromwechat) {
$this->message(array('message' => '请返回微信查看支付状态', 'title' => '支付成功!', 'buttondisplay' => false), NULL, 'success');
}
else {
$this->message(array('message' => '请返回商城查看支付状态', 'title' => '支付成功!'), mobileUrl('member'), 'success');
}
}
$this->message(array('message' => '支付出现错误,请重试(支付验证失败2)!', 'buttondisplay' => $fromwechat ? false : true), $fromwechat ? NULL : mobileUrl('member'));
}
$transid = $_GET['trade_no'];
}
$log = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member_log') . ' WHERE `logno`=:logno and `uniacid`=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':logno' => $logno));
if (!empty($log) && empty($log['status'])) {
pdo_update('ewei_shop_member_log', array('status' => 1, 'rechargetype' => 'alipay', 'apppay' => is_h5app() ? 1 : 0, 'transid' => $transid), array('id' => $log['id']));
m('member')->setCredit($log['openid'], 'credit2', $log['money'], array(0, $_W['shopset']['shop']['name'] . '会员充值:alipayreturn:credit2:' . $log['money']));
m('member')->setRechargeCredit($log['openid'], $log['money']);
com_run('sale::setRechargeActivity', $log);
com_run('coupon::useRechargeCoupon', $log);
m('notice')->sendMemberLogMessage($log['id']);
}
//---------------------------------------------抽奖支付(在此添加返回的逻辑,mobileUrl('lotterys',array('id'=>1)) lotterys是路由地址,还可传参)
if($log['type'] ==34){
$this->message(array('message' => '请返回商城查看支付状态', 'title' => '支付成功!'), mobileUrl('lotterys'), 'success');
}
//--------------------------------------------------------------------
if (is_h5app()) {
$url = mobileUrl('member', NULL, true);
exit('<script>top.window.location.href=\'' . $url . '\'</script>');
}
else if ($fromwechat) {
$this->message(array('message' => '请返回微信查看支付状态', 'title' => '支付成功!', 'buttondisplay' => false), NULL, 'success');
}
else {
$this->message(array('message' => '请返回商城查看支付状态', 'title' => '支付成功!'), mobileUrl('member'), 'success');
}
}