准备工作
在 composer.json 中添加
"ignited/laravel-omnipay": "2.*",
"lokielse/omnipay-alipay": "dev-master",
"lokielse/omnipay-wechatpay": "dev-master"
然后运行
composer update
安装完成后在config/app.php
中的 providers 数组中添加
Ignited\LaravelOmnipay\LaravelOmnipayServiceProvider::class
在
config/app.php
中的 aliases 数组中添加
'Omnipay' => Ignited\LaravelOmnipay\Facades\OmnipayFacade::class
然后通过 php artisan vendor:publish 发布配置文件
以下是微信/支付宝 支付 控制器的相关内容
public function PayString(Request $request)
{
$code = $request->get('code'); // 支付类型
$order_sn = $request->get('order_sn'); //订单号
$order = Order::where('order_sn',$order_sn)->where('status',1)->first(); //订单信息
if (!empty($order)){
switch ($code){
case 'alipay':
$gateway = Omnipay::create('Alipay_AopApp');
$gateway->setSignType('RSA'); //RSA/RSA2
$gateway->setAppId(''); // 支付宝的APPid
$gateway->setPrivateKey(app_path(''));
$gateway->setAlipayPublicKey(app_path(''));
$gateway->setNotifyUrl(url()); // 支付宝的回调地址
$request = $gateway->purchase();
$request->setBizContent([
'subject' => '支付主体内容',
'out_trade_no' => 订单号,
'total_amount' => 订单金额,
'product_code' => 'QUICK_MSECURITY_PAY',
]);
$response = $request->send();
$orderString = $response->getOrderString();
return response()->json(['code' => 200, 'msg' => 'ok', 'data' => ['code'=>$code,'data' => $orderString]]);
break;
case 'wxpay':
$gateway = Omnipay::create('WechatPay_App');
$gateway->setAppId(''); // 微信appId
$gateway->setMchId(''); // 商铺号
$gateway->setApiKey(''); // apikey 需要在微信商户平台自行设置
$order = [
'body' => '轻享赚订单支付',
'out_trade_no' => 订单号,
'total_fee' => 订单金额, //=0.01
'spbill_create_ip' => 支付的ip地址,
'fee_type' => 'CNY',
'notify_url' => url(''); // 微信支付的回调地址
];
$request = $gateway->purchase($order);
$response = $request->send();
return response()->json(['code' => 200, 'msg' => 'ok', 'data' => ['code'=>$code,'data' => $response->getAppOrderData()]]);
break;
default:
return response()->json(['code'=> 404,'msg'=>'支付方式不存在']);
}
}else{
return response()->json(['code'=>404,'msg'=>'资源不存在']);
}
}
以下是回调接口的相关资料
// 微信订单回调
public function OrderWxPayNotify(Request $request)
{
$gateway = Omnipay::create('WechatPay_App');
$gateway->setAppId(''); // 微信APPID
$gateway->setMchId(''); // 商铺号 可在申请微信支付成功之后的邮件中找到
$gateway->setApiKey(''); // 微信apikey 可在商铺平台自行设置
$response = $gateway->completePurchase([
'request_params' => file_get_contents('php://input')
])->send();
if ($response->isSuccessful()) {
$payData = $response->getRequestData();
\Log::info(json_encode($payData));
$order = Order::where('order_sn', $payData['out_trade_no'])->first();
if (empty($order)) return;
if ($response->isPaid()) {
if ($order->pay_id == 0) {
$orderPay = new OrderPay();
$orderPay->order_id = $order->id;
$orderPay->pay_code = 'wxpay';
$orderPay->pay_sn = $payData['transaction_id'];
$orderPay->pay_total = $payData['total_fee'] / 100;
$orderPay->status = 2;
$orderPay->pay_detail = serialize($payData);
$orderPay->save();
$order->pay_id = $orderPay->id;
$order->status = 2;
$order->paid = $orderPay->pay_total;
$order->save();
} else {
$orderPay = $order->Pay;
$orderPay->pay_code = 'wxpay';
$orderPay->pay_sn = $payData['transaction_id'];
$orderPay->pay_total = $payData['total_fee'] / 100;
$orderPay->status = 2;
$orderPay->pay_detail = serialize($payData);
$orderPay->save();
$order->status = 2;
$order->paid = $orderPay->pay_total;
$order->save();
}
}
}
}
// 支付宝订单回调
public function OrderAliPayNotify(Request $request)
{
$gateway = Omnipay::create('Alipay_AopApp');
$gateway->setSignType('RSA'); //RSA/RSA2
$gateway->setAppId(''); // 支付宝应用appid
$gateway->setPrivateKey(app_path(''));
$gateway->setAlipayPublicKey(app_path(''));
$gateway->setNotifyUrl('https://www.example.com/notify');
$gatewayRequest = $gateway->completePurchase();
$gatewayRequest->setParams($request->all());//Optional
try {
$response = $gatewayRequest->send();
if ($response->isSuccessful()) {
$payData = $response->getData();
\Log::info(json_encode($payData));
$order = Order::where('order_sn', $payData['out_trade_no'])->first();
if (empty($order)) return response()->json(['code' => 404]);
// \Log::info(serialize($payData));
if ($response->isPaid()) {
if ($order->pay_id == 0) {
$orderPay = new OrderPay();
$orderPay->order_id = $order->id;
$orderPay->pay_code = 'alipay';
$orderPay->pay_sn = $payData['trade_no'];
$orderPay->pay_total = $payData['total_amount'];
$orderPay->status = 2;
$orderPay->pay_detail = serialize($payData);
$orderPay->save();
$order->pay_id = $orderPay->id;
$order->status = 2;
$order->paid = $orderPay->pay_total;
$order->save();
} else {
$orderPay = $order->Pay;
$orderPay->pay_code = 'alipay';
$orderPay->pay_sn = $payData['trade_no'];
$orderPay->pay_total = $payData['total_amount'];
$orderPay->status = 2;
$orderPay->pay_detail = serialize($payData);
$orderPay->save();
$order->status = 2;
$order->paid = $orderPay->pay_total;
$order->save();
}
}
}
} catch (Exception $e) {
return '';
}
}