forked from netcccyun/alipay-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagepay.php
36 lines (31 loc) · 1.25 KB
/
pagepay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
* 支付宝电脑&手机网站支付示例
*/
require __DIR__.'/../vendor/autoload.php';
@header('Content-Type: text/html; charset=UTF-8');
$hostInfo = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'];
//引入配置文件
$alipay_config = require('config.php');
//异步回调地址和同步跳转地址
$alipay_config['notify_url'] = $hostInfo.dirname($_SERVER['SCRIPT_NAME']).'/notify.php';
$alipay_config['return_url'] = $hostInfo.dirname($_SERVER['SCRIPT_NAME']).'/return.php';
//构造业务参数bizContent
$bizContent = [
'out_trade_no' => date("YmdHis").rand(111,999), //商户订单号
'total_amount' => '0.15', //订单金额,单位为元
'subject' => 'sample subject', //商品的标题
];
//发起支付请求
try{
$aop = new \Alipay\AlipayTradeService($alipay_config);
//$aop->directPayParams($bizContent); //互联网平台直付通补充业务参数
if(preg_match('/(android|iphone|ipod|windows phone)/i', $_SERVER['HTTP_USER_AGENT'])){ //判断UA为手机浏览器
$html = $aop->wapPay($bizContent);
}else{
$html = $aop->pagePay($bizContent);
}
echo $html;
}catch(Exception $e){
echo '支付宝下单失败!'.$e->getMessage();
}