支付宝小程序 所涉及的 支付宝支付 支付宝退款 流程
贴上详细代码 以供参考
支付宝支付
先下载官方的PHPSDK包 小程序官方SDK
先引入Vendor 然后如下配置
控制器配置
支付宝的支付控制器
public function doalipay(){
vendor('aop.AopClient');
Vendor('aop.request.AlipayTradeCreateRequest');
$aop = new \AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->timestamp = date("Y-m-d H:i:s",time());
$aop->appId = '2015555555555';
$aop->method = 'alipay.trade.create';
$aop->rsaPrivateKey = '商户私钥';
// $aop->alipayrsaPublicKey =C("alipayrsaPublicKey");
$aop->alipayrsaPublicKey='支付宝公钥';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayTradeCreateRequest ();
//商户外网可以访问的异步地址 这一步 必须要设置 官网没说 但是很重要
$request->setNotifyUrl(U('Api/V6/Alipay/ali_notify', '', true, true));
//请求的串 买家aliuser_id 没有的话 会报错哦! 比微信的要简单的多 就是这几个参数
$content = array();
$content['out_trade_no'] = $order_num; // 商户订单号
$content['total_amount'] = sprintf('%.2f',$allmoney); // 订单总金额
$content['subject'] = '户外租聘'; // 订单标题
$content['buyer_id'] = I('aliuser_id'); // 买家aliuser_id
$content=json_encode($content);
$request->setBizContent($content);
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resinfo['code'] = $result->$responseNode->code;
if(!empty($resinfo['code'])&&$resinfo['code'] == 10000){
$this->set_success('成功',$resinfo);
}else{
$this->set_error('失败',$resinfo);
}
}
支付宝的回调如何获取 参数 这个应该很简单 我顺口提一下 还应该验签 保证安全 结束回调 记得要返回
echo “success”;
} else {
echo “fail”;
if(IS_POST) {
//获取参数
$data = $_POST;
//验签
vendor('aop.AopClient');
$aop = new \AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->timestamp = date("Y-m-d H:i:s",time());
$aop->appId = '00000000000';
//$aop->notify_url = 'http://test.cnsunrun.com/sharelease/Api/V6/Alipay/ali_notify';
// $aop->rsaPrivateKey =C("rsaPrivateKey");
$aop->rsaPrivateKey = '';
// $aop->alipayrsaPublicKey =C("alipayrsaPublicKey");
$aop->alipayrsaPublicKey='';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$flag = $aop->rsaCheckV1($data, NULL, "RSA2");
if(!$flag || $data['trade_status'] != 'TRADE_SUCCESS'){
echo "fail";exit;
}
大家其实可以把参数 包装一下 然后 好看一些 。。
支付宝退款 和某信比 支付宝的文档简直是太良心了 而且参数也很少 没有乱七八糟的 某信的坑 简直吐了 连官方文档 里的接口名字都打乱了 各大社区去看才找到正确方法
public function ali_refund($out_trade_no){
vendor('aop.AopClient');
Vendor('aop.request.AlipayTradeRefundRequest');
$aop = new \AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->timestamp = date("Y-m-d H:i:s",time());
$aop->appId = '222222222222224';
$aop->method = 'alipay.trade.refund';
//$aop->notify_url = 'http://test.cnsunrun.com/sharelease/Api/V6/Alipay/ali_notify';
// $aop->rsaPrivateKey =C("rsaPrivateKey");
$aop->rsaPrivateKey = '';
// $aop->alipayrsaPublicKey =C("alipayrsaPublicKey");
$aop->alipayrsaPublicKey='';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayTradeRefundRequest ();
$content = array();
$content['out_trade_no'] = $out_trade_no; // 订单支付时传入的商户订单号,不能和 trade_no同时为空。
// $content['refund_currency'] = 'whzp'.rand(10000,99999); // 标识一次退款请求,同一笔交易多次退款需要保证唯一
// $content['refund_currency'] = 'RMB';
$content['refund_amount'] =sprintf('%.2f',$allmoney);; // 订单总金额
$content['refund_reason'] ='正常退款'; // 订单总金额
$content=json_encode($content);
$request->setBizContent($content);
$result = $aop->execute ($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resinfo['code'] = $result->$responseNode->code;
$resinfo['msg'] = $result->$responseNode->msg;
$resinfo['out_trade_no'] = $result->$responseNode->out_trade_no;
$resinfo['trade_no'] = $result->$responseNode->trade_no;
$resinfo['sub_msg'] = $result->$responseNode->sub_msg;
$resinfo['refund_amount'] = $result->$responseNode->refund_amount;
if(!empty($resinfo['code'])&&$resinfo['code'] == 10000){
return 1;
}else{
}
}
以上