记录一下laravel接入支付宝sdk;
1,直接从支付宝官网下载sdk https://docs.open.alipay.com/54/103419
2,把下载好的sdk复制到一个目录下面,我的是放到了app/Packages下面,
把下载的sdk目录改成alipay,目录如下图
3,在config目录下新建alipay.php,在composer.json中加入下面的内容
"app/Packages/alipay" //然后运行 composer dumpautoload
$config = array ( //应用ID,您的APPID。 'app_id' => env('ALIPAY_APPID'), //商户私钥,您的原始格式RSA私钥 'merchant_private_key' => env('PRIVATE_KEY'), //异步通知地址 'notify_url' => env('NOTIFY_URL'),//"http://工程公网访问地址/alipay.trade.wap.pay-PHP-UTF-8/notify_url.php", //同步跳转 'return_url' => 'http://www.xiegaosheng.com/alicallback',//"http://mitsein.com/alipay.trade.wap.pay-PHP-UTF-8/return_url.php", //编码格式 'charset' => "UTF-8", //签名方式 'sign_type'=>"RSA", //支付宝网关 'gatewayUrl' => "https://openapi.alipay.com/gateway.do", //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 'alipay_public_key' =>env('PUBLIC_KEY'), ); return $config;
4,在alipay目录下面新建一个alipay.php的类
<?php /** * Created by PhpStorm. * User: xiaoxie * Date: 2018/9/2 * Time: 14:13 **/ namespace app\Packages\alipay; require ('AopSdk.php'); class Alipay { public static function alipays($data) { if (!$data) { exit('param is numm'); } $out_trade_no = $data['order_no']; //订单名称,必填 $subject = $data['order_name']; //付款金额,必填 $total_amount = $data['total_num']; //商品描述,可空 $body = $data['body']; //超时时间 $timeout_express="1m"; $payRequestBuilder = new \AlipayTradeWapPayContentBuilder(); $payRequestBuilder->setBody($body); $payRequestBuilder->setSubject($subject); $payRequestBuilder->setOutTradeNo($out_trade_no); $payRequestBuilder->setTotalAmount($total_amount); $payRequestBuilder->setTimeExpress($timeout_express); $config = config('alipay'); $payResponse = new \AlipayTradeService($config); $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']); return ; } }
5,新建controller,里面调用
//控制器中新建一个方法 public function alipay(Request $request) { $total_fee = $request->input('total_fee'); $out_trade_no = $request->input('order_no'); $name = $request->input('name'); $body = '上网套餐'; $data['order_no'] = $out_trade_no; $data['order_name'] = $name; $data['body'] = $body; $data['total_num'] = sprintf("%1\$.2f",$total_fee/100); Alipay::alipays($data); }
7,支付宝的sdk中的encrypt和decrypt函数和laravel中的相同了,所以要修改一下sdk中的函数
aop/AopEncrypt.php aop/AopClient.php lotusphp_runtime/Cookie/Cookie.php 在文件中查找encrypt/decrypt替换为alipayEncrypt/alipayDecrypt即可。