laravel中生成支付寶 二維碼 掃碼支付


文檔教程模擬:

http://www.023xs.cn/Article/37/laravel5%E9%9B%86%E6%88%90%E6%94%AF%E4%BB%98%E5%AE%9Dalipay%E6%89%AB%E7%A0%81%E6%94%AF%E4%BB%98%E6%B5%81%E7%A8%8B%EF%BC%88Laravel%E6%94%AF%E4%BB%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%89

1、在對應的螞蟻金服,申請【網頁&移動應用】,將對應的應用上線,並簽約【電腦網站支付】和【手機網站支付】

2、在項目中composer對應的包【項目里面的vendor/latrell包上傳coding的話,需要復制本地后在上傳】

3、按照文檔修改對應的配置文件,其中各個支付的回調可以單獨配置

4、生成對應的地址后,將地址使用二維碼生成工具 生成二維碼圖片 返回即可

示例代碼:

訂單生成,提交訂單代碼示例:

public function ccsppUserDoVipOrderAlyPc(Request $request)
{
$data = $request->all();
$userId = $data['user_id'];
$user = \DB::table('user')
->where('id',$userId)
->first();
//此處訂單和支付一起生成
$money = 0.01;
$order['order_uid'] = $data['user_id'];
$order['order_uname'] = $user->user_name;
$order['order_type'] = 1;
$order['order_time'] = time();
$order['order_money'] = $money;
$order['order_status'] = 1;
$order['order_tel'] = $user->user_tel;
$order['order_sid'] = str_random(4) .time();
\DB::beginTransaction();
// 創建訂單
$res_order = \DB::table('order')
->insertGetId($order);
if(!$res_order){
\DB::rollback();
\DB::commit();
return ['code' => 1002, 'data' => ['message' => '訂單生成失敗!']];
}

//生成支付寶的訂單
$config = config('latrell-alipay-web');
$alipay = app('alipay.web');
$alipay->setOutTradeNo($order['order_sid']);
$alipay->setTotalFee($order['order_money']);
$alipay->setSubject('交易內容');
$alipay->setBody('交易內容');
$alipay->setNotifyUrl($config['notifyUrlJkAly']);//設置回調地址

$alipay->setQrPayMode('1'); //該設置為可選1-5,添加該參數設置,支持二維碼支付。
$basePath = base_path();
$basePath = $basePath."/public/uploads/code/";
$img_file = $basePath.'aly'.$order['order_sid'].'.png';
$alcode = 'aly'.$order['order_sid'].'.png';
$alycode = self::urlUsePic . $alcode;
$abc = \QrCode::format('png')->size(200)->margin(0)->generate($alipay->getPayLink(),$img_file);
// 跳轉到支付頁面。
$aliPayUrl = $alipay->getPayLink();
//訂單生成后給訂單生成對應的支付二維碼
\DB::commit();
return ['code' => 1000,'alycode' => $alycode];
}

回調配置代碼示例:

latrell-alipay:

return [

//合作身份者id,以2088開頭的16位純數字。
'partner_id' => '208****12752',

//賣家支付寶帳戶。
'seller_id' => 'g******l@da****.com'
];

latrell-alipay-web:

return [

// 安全檢驗碼,以數字和字母組成的32位字符。
'key' => 'bbpjx64******ggve61',

//簽名方式
'sign_type' => 'MD5',

// 服務器異步通知頁面路徑。支付1的回調
'notify_url' => 'https://test/notify_url',

// 服務器異步通知頁面路徑。支付2的回調
'notifyUrlJkAly' => 'https://test/notifyUrlJkAly',

// 服務器異步通知頁面路徑。支付3的回調
'notifyUrlVideoAly' => 'https://test/notifyUrlVideoAly',

// 服務器異步通知頁面路徑。支付4的回調(在訂單生成時選擇參數)
'notifyUrlMeetingAly' => 'https://test/notifyUrlMeetingAly',

// 頁面跳轉同步通知頁面路徑。
// 'return_url' => 'https://test/aliPayReturn'
'return_url' => ''
];

回調路由單個代碼示例:

官方示例:1

public function notify_url(Request $request)
{
// 驗證請求。
if (!app('alipay.web')->verify()) {
Log::notice('Alipay notify post data verification fail.', [
'data' => $request->instance()->getContent()
]);
return 'fail';
}
// 判斷通知類型。
switch ($request ->input('trade_status','')) {
case 'TRADE_SUCCESS':
case 'TRADE_FINISHED':
// TODO: 支付成功,取得訂單號進行其它相關操作。
Log::debug('Alipay notify post data verification success.', [
'out_trade_no' => $request -> input('out_trade_no',''),
'trade_no' => $request -> input('trade_no','')
]);
break;
}
$data = $request->all();
$time_now = time();
\DB::table('test')->where('id',1)->update(['name'=>$data['out_trade_no'],'time' => $time_now]);
return "success";
}

自己示例:2 已驗證,可以使用

public function notifyUrlJkAly(Request $request)
{
$data = $request->all();
$order_sid = $data['out_trade_no'];
$time_now = time();
//修改訂單用戶和狀態
\DB::table('order')->where('order_sid',$order_sid)->update(['order_status'=>2,'order_ptime' => $time_now]);
//同時根據訂單的類型修改用戶的會員類型和 版本到期時間 默認加一年
$order_res = \DB::table('order')->where('order_sid',$order_sid)->first();
if($order_res){
//如果是版本支付訂單,就修改用戶的支付狀態,和版本使用期限
$uid = $order_res->order_uid;
$date = date('Y',$time_now) + 1 . '-' . date('m-d H:i:s');//一年后日期
$ytime = strtotime($date);
$ress = \DB::table('user')->where('id',$uid)->update(['user_type' => 2,'user_btime' => $time_now,'user_etime' => $ytime]);
}
return "success";
}

 注:支付寶退款 時會訪問 調用支付成功回調時的接口,所以會產生 數據庫訂單狀態 修改不了的現象,處理辦法是在 支付成功回調中 對訂單進行狀態判斷,只有未支付的狀態才可以修改訂單為支付狀態

並且在回調中,處理完后要按照文檔上的 返回"success" ,不能自己胡亂寫

 

例如:

/**
* 購買會議活動報名回調 支付寶 url()
* param ()
*/
public function notifyUrlMeetingAly(Request $request)
{
$data = $request->all();
$order_sid = $data['out_trade_no'];
$time_now = time();

//修改訂單支付狀態
$order = \DB::table('order')->where('order_sid',$order_sid)->first();
$order_status = $order->order_status;
if($order_status == 1){
\DB::table('order')->where('order_sid',$order_sid)->update(['order_status'=>2,'order_ptime' => $time_now]);
//修改用戶報名關系表訂單狀態
\DB::table('morder')->where('morder_sid',$order_sid)->update(['morder_status'=>1]);
}

return 'success';
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM