支付宝支付
本文讲解支付宝支付功能服务端的接入,在接入支付包支付时,首先要开通支付宝账号,并创建支付应用获取appid及其他参数,这里以CI框架为例进行说明。支付宝支付sdk下载地址http://aopsdkdownload.cn-hangzhou.alipay-pub.aliyun-inc.com/demo/alipay.trade.wap.pay-PHP-UTF-8.zip
实例源码如下:
require_once BASEPATH .'../service/third_party/alipay_sdk/AopSdk.php'; //引入支付宝支付sdk
class Pay extends MY_Controller{
protected $aop;
public function __construct(){
parent::__construct();
$this->aop = new AopClient;
//支付网关
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
//角色身份PID
$this->aop->partner = '123456';
//支付宝商户号
$this->aop->sellerId = 'dichuanjun2017@163.com';
//应用ID
$this->aop->appId = '2017888888888';
//商户私钥,您的原始格式RSA私钥
$this->aop->rsaPrivateKey = 'xxxxx';
//支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥
$this->aop->alipayrsaPublicKey = 'xxxxxx';
$this->aop->apiVersion = '1.0';
//签名方式
$this->aop->signType = 'RSA2';
//编码格式
$this->aop->postCharset='utf-8';
$this->aop->format='json';
}
/**
* @todo 支付app(订单创建)
* @param int pay_channel 1:微信支付 2:支付宝支付
* @param int charge_type 1:充值余额 2:充值保证金
*/
public function order_create(){
//参数
$param = $this->input->get();
$user_info = $this->session->userdata(KEY_USER_INFO);
$usr_id=0;
if(!empty($user_info)){
$usr_id =$user_info['id'];
}
//参数验证
if(empty($usr_id)){
$this->api_response_result(11);
}
//共享活动id
if(!is_numeric($param['share_id']) || empty($param['share_id'])){
$this->api_response_result(5001);exit;
}
$this->load->model('M_share_pay');
$this->load->model('M_share_activity');
$pay_info = $this->M_share_pay->share_pay_detail($param['share_id']);
$share_car_info= $this->M_share_activity->share_info($param['share_id']); //共享信息
$param['amount']=$share_car_info['deposit']; //支付金额
$param['pay_channel']=2;//支付渠道
$param['charge_type']=2;//充值类型
$pay_order_no = $this->build_charge_order_id($param['pay_channel'],$param['charge_type']); //支付订单号
$data['pay_order_no']=$pay_order_no;
$data['pay_amount']=$param['amount'];
$data['pay_channel']=$param['pay_channel'];
$data['charge_type']=$param['charge_type'];
$data['ctime']=date('Y-m-d H:i:s',time());
$data['utime']=date('Y-m-d H:i:s',time());
if(!empty($pay_info)){
if($pay_info['pay_status']==PAY_STATUS_OK){
// $this->api_response_result(5000);exit;
echo "<script>alert('该共享订单已支付订金');</script>";exit;
}
$res=$this->M_share_pay->share_pay_update($pay_info['id'],$data);
}else{
$data['share_id']=$param['share_id'];
$res=$this->M_share_pay->share_pay_insert($data);
}
if(empty($pay_order_no)){
$this->api_response_result(-1,'由于系统故障,支付失败,请稍后再试');exit;
}
if($param['pay_channel'] == PAY_CHANNEL_APP_WECHAT){ //微信app
$data = array(
'out_trade_no'=>$res['out_trade_no']
);
}elseif($param['pay_channel'] == PAY_CHANNEL_APP_ALIPAY){ //支付宝app
$request = new AlipayTradeWapPayRequest();
$subject = '天天淘车购车订金';
$param = array(
'body'=>'充值',
'out_trade_no'=>$pay_order_no,
'timeout_express'=>'30m',
'total_amount'=>$param['amount'],
'subject'=>$subject,
'product_code'=>'QUICK_MSECURITY_PAY',
);
$request->setBizContent(json_encode($param));
$request->setNotifyUrl("http://".$_SERVER['HTTP_HOST']."/api/pay/alipay_notify");
$response = $this->aop->pageExecute($request);//H5支付支付参数链接,唤起支付链接
// $response = $this->aop->sdkExecute($request); //Android app 支付参数链接,唤起支付链接
}
echo $response;exit;
}
/**
* @todo 支付宝异步通知
* @return string
* 支付宝支付@服务器异步通知页面方法 http://www.thinkphp.cn/code/240.html
*/
public function alipay_notify(){
$param = $this->input->post();
//进行订单处理,并传送从支付宝返回的参数
if($param['trade_status'] == 'TRADE_SUCCESS'){ //支付成功
$this->load->model('M_share_pay');
$this->load->model('M_share_activity');
$this->load->model('M_user');
$this->load->model('M_coupon');
$data['pay_status']=PAY_STATUS_OK; //支付成功
$data['pay_no']=$param['trade_no']; //支付宝交易号
$data['utime']=$param['notify_time'];
$pay_info = $this->M_share_pay->pay_info_by_order_no($param['out_trade_no']);
$this->M_share_pay->share_pay_update($pay_info['id'],$data);
$data_progress['pay_no']=$param['trade_no'];
$data_progress['share_id']=$pay_info['share_id'];
$this->M_share_pay->share_pay_progress($data_progress);
echo "success";
}else{
echo "fail";
}
}
}