引入官方sdk,composer.json加入
"autoload": {
"classmap": [
"app/libs/alipay" //我放到app/libs/alipay
]
},
composer dump-autoload //当在包中加入新的类,需要更新autoloader
设置引入命名空间
对AopClient与AlipayTradePagePayRequest分别加入
namespace App\libs\alipay\aop;
namespace App\libs\alipay\aop\request;
新建控制器(AlipayController)
引入AopClient与AlipayTradePagePayRequest文件
use App\libs\alipay\aop\AopClient;
use App\libs\alipay\aop\request\AlipayTradePagePayRequest;
<?php
namespace App\Api\Controllers;
use App\Api\Models\Order;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use App\libs\alipay\aop\AopClient;
use App\libs\alipay\aop\request\AlipayTradePagePayRequest;
class AliPayController extends Controller {
public function pay(Request $request){
$aop = new AopClient();
$aop->gatewayUrl = '';//支付宝网关
$aop->appId = ''; // 支付宝appid
$aop->rsaPrivateKey = ''; // 私钥
$aop->alipayrsaPublicKey= ''; //支付宝公钥
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
//订单信息
$object = new \stdClass();
$object->out_trade_no = time();
$object->total_amount = 100; //价格
$object->subject = '书币充值';
$object->product_code ='FAST_INSTANT_TRADE_PAY';
// $object->time_expire = date('Y-m-d H:i:s',time());
$json = json_encode($object);
//设置同步异步掉地址
$request = new AlipayTradePagePayRequest();
$request->setReturnUrl(''); //同步
$request->setNotifyUrl(''); //异步
$request->setBizContent($json);
// dd($request);
$result = $aop->pageExecute( $request);
//return $result;
return json_encode($result);
}
}
处理异步请求
public function notify(){
$aop = new AopClient();
$aop->alipayrsaPublicKey = ''; //支付宝公钥
$flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
if($flag){
//处理业务,并从$_POST中提取需要的参数内容
if($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED'){//处理交易完成或者支付成功的通知
// file_put_contents('/www/wwwroot/yidu/test.txt',"123",FILE_APPEND);
// file_put_contents('/www/wwwroot/yidu/test.txt',"<br />",FILE_APPEND);
//获取订单号
$orderId = $_POST['out_trade_no'];
//交易号
$trade_no = $_POST['trade_no'];
//此处编写回调处理逻辑
//处理成功一定要返回 success 这7个字符组成的字符串,
die('success');//响应success表示业务处理成功,告知支付宝无需在异步通知
}
}
}
|