支付宝支付
1、先申请支付注册签约开通APP支付等。
2、引入PHP SDK包
3、注意几个事项,编码转换,签名类型,对比签名,密钥
4、代码
//生成支付订单
function create(){
$total = input('total');
$aop = new AopClient;
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = "2017051707261724";
$aop->rsaPrivateKey = "MIICWwIBAAKBgQCuMno8RxNNxYAmtZylQyD0uEo0clUJsF+xHZ449EWgpfwOLeQwCjVg8Cy1peKnRlqeYdXI3iz/b3elQNgcD3hi5ERWfKlJJlbLrIUhSBbb12n6jgyat9yoRv0hNP+pexI16eyNvo/xo+1+ufyxRKdnAy3P8EDeWSCwkY4jkfn2nwIDAQABAoGAYXq50HVtpf25gu/qYsi37noafFLmp29dfT23xuNa+1UnQcAIi6GK0V5Vhqj3tue12h+hhLBfuelkqMMqrDk0AiZD3jRC7xOu0q3nOX/8X2NOJjgCtzYs3r1U9Fsi6pGMgLx3Go/lDEN3mLJ/QlGrtHZ2RVc0PpkLaLRVj9BiGWECQQDfj/JSr47HDit49OBaWNnhMAh9/19Si8h5YcURVql/W1Xgr75t1VHwF0YMKCF0I1TEFoXIODZnYUXsyVWvzFunAkEAx3jmnPocDUrX0PyUedODARAtk6BJed2e/tlSAMqCDZ5fMtseXl8Ybqlrfu+ncZiQmtm5Rj97TXFHjEBtLVgMSQI/Bhzpg1lJjifVCHmpqM7FJmMp2EgjUx5/XNJPkSnNeK4Gqx4jFt2+X9JDGBIA2CwfJT14awEee7UtNExNC4GLAkB+iLYZnBrRX8m5m5AAG+k9cmrG++XANZLXbTo0oEGRrAYNt6vzB3XzNOuo0ROx3qrUYArEgh6QiKxvTqszGWhpAkEAtfj9qJJw4OBdYJ+jfjdLp2wUoR7ORD9H65luUVardoKIyAwvkY53zMftegSG6AupzbwpaQdWcwnHCd3cO6YIQw==";
//私钥
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA";//签名类型
//支付宝公钥
$aop->alipayrsaPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$bizcontent = "{\"body\":\"我是测试数据\","
. "\"subject\": \"App支付测试\","
. "\"out_trade_no\": \"20170125test03\","
. "\"timeout_express\": \"30m\","
. "\"total_amount\": $total,"
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}";
$request->setNotifyUrl(url('notify_url'));
$request->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
// echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
echo $response;//就是orderString 可以直接给客户端请求,无需再做处理。
}
//异步通知\
function notify_url(Request $request){
$aop = new AopClient;
$aop->alipayrsaPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
trace($request,'异步通知');
// echo 'success';
}