1.支付宝手机网站支付部分代码:
需要注意的是AlipayTradeService.php的aopclientRequestExecute()方法需要修改
if (!empty($oid)&& trim($oid)!=""){//商户订单号,商户网站订单系统中唯一订单号,必填$out_trade_no = $oid;//订单名称,必填$subject = "好多羊牧场充值";//付款金额,必填$total_amount = $orderInfo['money'];//商品描述,可空$body = '';//超时时间$timeout_express="1m";$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();$payRequestBuilder->setBody($body);$payRequestBuilder->setSubject($subject);$payRequestBuilder->setOutTradeNo($out_trade_no);$payRequestBuilder->setTotalAmount($total_amount);$payRequestBuilder->setTimeExpress($timeout_express);$payResponse = new \AlipayTradeService($config);$result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);return array("result"=>$result,"oid"=>$oid,"money"=>$money);// return ;}
2.服务器端代码:
//跳转充值页面if ($re) {switch ($type) {case '1':$_SESSION['wxpaysid'] = 0;$_SESSION['wxpayopenid'] = $_SESSION['WAP']['vip']['openid'];//追入会员openid$this->redirect('Home/Wxpaycz/pay', array('price' => $money, 'oid' => $data_log['opid']));break;case '2'://支付宝支付$_SESSION['wxpaysid'] = 0;$_SESSION['wxpayopenid'] = $_SESSION['WAP']['vip']['openid'];//追入会员openid$Alipay =A("Alipay");$info = $Alipay->alipay($data_log['opid'],$money);//这里传参 oid money$this->ajaxReturn($info);//主要是把上面的result返回给前台break;default:$this->error('支付方式未知!');break;}} else {$this->error('出错啦!');}
前端代码:
else if(zxtype == 2){//支付宝支付$("#surebtn").addClass("J-btn-submit btn mj-submit btn-strong btn-larger btn-block");$.ajax({type:'get',url:"{:U('App/Vip/zxczSet')}",dataType:'json',data:{type:zxtype,money:money,},success:function (re) {if(re){var datas = re.result;//result是支付宝支付接口返回form表单$("#paywindow").append(datas.split('</form>')[0]);//获取form表单var queryParam = '';//开始拼接参数Array.prototype.slice.call(document.querySelectorAll("input[type=hidden]")).forEach(function (ele) {queryParam += ele.name + "=" + encodeURIComponent(ele.value) + '&';});var gotoUrl = document.querySelector("#alipaysubmit").getAttribute('action')+'&'+queryParam;//gotourl 为:https://openapi.alipaydev.com/gateway.do?charset=UTF-8&=2&biz_content=%7B%…%2Fe4NdLEHxmoAJNdDoL5t0BnMlueFH2pSIhY1GyFFEK%2Frb%2FM1VTgG%2BzGXj8g%3D%3D&//调用ap.js方法_AP.pay(gotoUrl);}}});}
在微信公众平台无法使用支付宝收付款的解决方案下载demo;
demo文件为:
在支付页面的支付按钮引入ap.js
,点击支付的时候会跳转到ap.js文件中的location.href的路径中,并且传一个goto的参数,ap.js的href路径需要更改为自己项目控制器的方法的路径
在Vip控制器编写pay方法:
public function pay(){$goto = I("get.goto");$this->assign("goto",$goto);$this->display();}
将上面demo文件中的pay.htm复制为这个方法的模板(注意这个文件的ap.js的文件路径)
点击支付后如图所示: