- 下载支付宝支付SDK放入extend文件下。
- 将需要用到的文件加入命名空间方便后续使用,如订单验签等…。
3. 将alipay下所有文件中带有Exception的地方前面加斜杠(\)不然无法捕获到错误。
- 添加测试数据返回唤起支付宝支付数据。
public function aliArousePay()
{
$order_id = $this->request->post('order_id/d', null);
$order_number = $this->request->post('order_number/s', null);
if (!$order_id and !$order_number) {
return self::fail('订单ID或订单号必须存在其中一个');
}
$order_map = array(
['uid', '=', $this->userinfo['account']],
['pay_state', '<>', 20]
);
if ($order_id) {
$order_map[] = ['order_id', '=', $order_id];
}
if ($order_number) {
$order_map[] = ['order_number', '=', $order_number];
}
$order_info = Order::where($order_map)->find();
if (!$order_info) {
return self::fail('该订单不存在或已支付');
}
if ($order_info['total_price'] <= 0) {
return self::fail('订单支付金额不合法!');
}
$AliPayModel = new AopClient();
$unifiedOrderBody = array_filter([
'total_amount' => $order_info['total_price'],
'body' => 'xxxxx',
'subject' => $order_info['remark'] ?? '订单',
'out_trade_no' => $order_info['order_number'],
'goods_type' => 0,
'merchant_order_no' => $order_info['order_number'],
]);
$param_data = array_filter([
'app_id' => config('alipay.app_id'),
'method' => 'alipay.trade.app.pay',
'charset' => 'utf-8',
'sign_type' => 'RSA2',
'timestamp' => date("Y-m-d H:i:s"),
'version' => 1.0,
'notify_url' => config('alipay.notify_url') . 'client/payment.xxxx/aliNotify',
'biz_content' => json_encode($unifiedOrderBody),
]);
$paramStr = $AliPayModel->getSignContent($param_data);
$sign = $AliPayModel->alonersaSign($paramStr, config('alipay.private_key'), 'RSA2');
$param_data['sign'] = $sign;
$result = $AliPayModel->getSignContentUrlencode($param_data);
return self::successful('success', $result);
}
- 测试可用
- 支付宝订单验签
public function aliNotify()
{
$data = $_POST;
$order_number = $data['out_trade_no'];
$trade_no = $data['trade_no'];
$result_data = $this->authAliOrder($order_number, $trade_no);
if (!$result_data) {
exit('FAIL');
}
$Order = new Order();
$Order::startTrans();
$order_info = $result_data;
try {
$chang_data = array(
'pay_state' => 20,
'pay_style' => 30,
'pay_time' => strtotime($data['gmt_payment']),
'payment_price' => $data['buyer_pay_amount'],
'transaction_id' => $data['trade_no'],
'notify_json' => $data,
'openid' => $data['buyer_id'],
);
$chang_data['need_price'] = bcsub($order_info['total_price'], $chang_data['payment_price'], 2);
$result = $Order->where([['order_id', '=', $order_info['order_id']]])->update($chang_data);
if ($result) {
$Order::commitTrans();
exit("SUCCESS");
}
throw new Exception('操作失败');
} catch (\Exception $exception) {
$Order::rollbackTrans();
exit("FAIL");
}
}
public function authAliOrder(string $out_trade_no, string $trade_no)
{
if (!$out_trade_no || !$trade_no) {
return false;
}
$AliPayModel = new AopClient ();
$Order = new Order();
$request = new AlipayTradeQueryRequest();
$order_info = $Order->where([['order_number', '=', $out_trade_no]])->find();
if (!$order_info) {
return false;
}
$AliPayModel->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$AliPayModel->appId = config('alipay.app_id');
$AliPayModel->rsaPrivateKey = config('alipay.private_key');
$AliPayModel->alipayrsaPublicKey = config('alipay.public_key');
$AliPayModel->apiVersion = '1.0';
$AliPayModel->signType = 'RSA2';
$AliPayModel->postCharset = 'utf-8';
$AliPayModel->format = 'json';
$request->setBizContent(json_encode(['out_trade_no' => $out_trade_no, 'trade_no' => $trade_no]));
$result = $AliPayModel->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
return $order_info;
}
return false;
}