需要首先下载支付宝SDK。
<?php
require_once ('AopSdk.php');
class Alipay {
/**
* 应用ID
*/
const APPID = 'aaaa';
/**
*请填写开发者私钥去头去尾去回车,一行字符串
*/
const RSA_PRIVATE_KEY = 'bbbbb';
/**
*请填写支付宝公钥,一行字符串
*/
const ALIPAY_RSA_PUBLIC_KEY = "ccccc";
/**
* 支付宝服务器主动通知商户服务器里指定的页面
* @var string
*/
private $notify = "http://xxx/alipay/notify.php";
/**
*生成APP支付订单信息
* @param string $orderId 商品订单ID
* @param string $goodsId 支付商品的ID
* @param string $price 支付商品价格
* @param float $goodsName 商品名称
*/
public function genetate($orderId,$goodsId,$price,$goodsName,$paysrc){
try{
$aop = new AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = self::APPID;
$aop->rsaPrivateKey = self::RSA_PRIVATE_KEY;
$aop->format = "json";
$aop->postCharset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey = self::ALIPAY_RSA_PUBLIC_KEY;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$bizcontent = "{\"out_trade_no\": \"${orderId}\","
. "\"body\": \"${goodsId}\","
. "\"subject\": \"${goodsName}\","
. "\"passback_params\": \"${goodsId}\","
. "\"total_amount\": \"${price}\","
. "\"product_code\":\"QUICK_MSECURITY_PAY\""
. "}";
$request->setNotifyUrl($this->notify);
$request->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
return $response;//htmlspecialchars(就是orderString 可以直接给客户端请求,无需再做处理。
}catch (Exception $e){
return false;
}
}
//查询订单
public function orderQuery($out_trade_no,$trade_no){
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = self::APPID;
$aop->rsaPrivateKey = self::RSA_PRIVATE_KEY;
$aop->alipayrsaPublicKey = self::ALIPAY_RSA_PUBLIC_KEY;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new AlipayTradeQueryRequest();
$bizcontent = "{\"out_trade_no\": \"${out_trade_no}\","
. "\"trade_no\": \"${trade_no}\","
. "}";
$request->setBizContent($bizcontent);
$result = $aop->execute($request);
return $result->alipay_trade_query_response;
}
}