第一步:使用composer安裝EasyWeChat
https://www.easywechat.com/docs/3.x/installation
第二步:在前面我們已經(jīng)講過(guò),初始化 SDK 的時(shí)候方法就是創(chuàng)建一個(gè) EasyWeChat\Foundation\Application 實(shí)例:
use EasyWeChat\Foundation\Application;
$options = [
// ...];
$app = new Application($options);
/**
* 如果想要在Application實(shí)例化完成之后, 修改某一個(gè)options的值,
* 比如服務(wù)商+子商戶(hù)支付回調(diào)場(chǎng)景, 所有子商戶(hù)訂單支付信息都是通過(guò)同一個(gè)服務(wù)商的$option 配置進(jìn)來(lái)的,
* 當(dāng)oauth在微信端驗(yàn)證完成之后, 可以通過(guò)動(dòng)態(tài)設(shè)置merchant_id來(lái)區(qū)分具體是哪個(gè)子商戶(hù)
*/$app['config']->set('oauth.callback','wechat/oauthcallback/'. $sub_merchant_id->id);
那么配置的具體選項(xiàng)有哪些,下面是一個(gè)完整的列表:
<?php
return [
/**
* Debug 模式,bool 值:true/false
*
* 當(dāng)值為 false 時(shí),所有的日志都不會(huì)記錄
*/
'debug' => true,
/**
* 賬號(hào)基本信息,請(qǐng)從微信公眾平臺(tái)/開(kāi)放平臺(tái)獲取
*/
'app_id' => 'your-app-id', // AppID
'secret' => 'your-app-secret', // AppSecret
'token' => 'your-token', // Token
'aes_key' => '', // EncodingAESKey,安全模式與兼容模式下請(qǐng)一定要填寫(xiě)?。?!
/**
* 日志配置
*
* level: 日志級(jí)別, 可選為:
* debug/info/notice/warning/error/critical/alert/emergency
* permission:日志文件權(quán)限(可選),默認(rèn)為null(若為null值,monolog會(huì)取0644)
* file:日志文件位置(絕對(duì)路徑!!!),要求可寫(xiě)權(quán)限
*/
'log' => [
'level' => 'debug',
'permission' => 0777,
'file' => '/tmp/easywechat.log',
],
/**
* OAuth 配置
*
* scopes:公眾平臺(tái)(snsapi_userinfo / snsapi_base),開(kāi)放平臺(tái):snsapi_login
* callback:OAuth授權(quán)完成后的回調(diào)頁(yè)地址
*/
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/examples/oauth_callback.php',
],
/**
* 微信支付
*/
'payment' => [
'merchant_id' => 'your-mch-id',
'key' => 'key-for-signature',
'cert_path' => 'path/to/your/cert.pem', // XXX: 絕對(duì)路徑?。。。?/span>
'key_path' => 'path/to/your/key', // XXX: 絕對(duì)路徑?。。?!
// 'device_info' => '013467007045764',
// 'sub_app_id' => '',
// 'sub_merchant_id' => '',
// ...
],
/**
* Guzzle 全局設(shè)置
*
* 更多請(qǐng)參考: http://docs.guzzlephp.org/en/latest/request-options.html
*/
'guzzle' => [
'timeout' => 3.0, // 超時(shí)時(shí)間(秒)
//'verify' => false, // 關(guān)掉 SSL 認(rèn)證(強(qiáng)烈不建議?。。。?/span>
],];
使用EasyWeChat可以完成支付功能
你在閱讀本文之前確認(rèn)你已經(jīng)仔細(xì)閱讀了:微信支付 | 企業(yè)付款文檔 。
配置在前面的例子中已經(jīng)提到過(guò)了,支付的相關(guān)配置如下:
<?php
use EasyWeChat\Foundation\Application;
$options = [
// 前面的appid什么的也得保留哦
'app_id' => 'xxxx',
// ...
// payment
'payment' => [
'merchant_id' => 'your-mch-id',
'key' => 'key-for-signature',
'cert_path' => 'path/to/your/cert.pem', // XXX: 絕對(duì)路徑!?。?!
'key_path' => 'path/to/your/key', // XXX: 絕對(duì)路徑!!?。?/span>
'notify_url' => '默認(rèn)的訂單回調(diào)地址', // 你也可以在下單時(shí)單獨(dú)設(shè)置來(lái)想覆蓋它
// 'device_info' => '013467007045764',
// 'sub_app_id' => '',
// 'sub_merchant_id' => '',
// ...
],];
$app = new Application($options);
$payment = $app->payment;
創(chuàng)建訂單
<?php
use EasyWeChat\Payment\Order;
$attributes = [
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
'body' => 'iPad mini 16G 白色',
'detail' => 'iPad mini 16G 白色',
'out_trade_no' => '1217752501201407033233368018',
'total_fee' => 5388, // 單位:分
'notify_url' => 'http://xxx.com/order-notify', // 支付結(jié)果通知網(wǎng)址,如果不設(shè)置則會(huì)使用配置里的默認(rèn)地址
'openid' => '當(dāng)前用戶(hù)的 openid', // trade_type=JSAPI,此參數(shù)必傳,用戶(hù)在商戶(hù)appid下的唯一標(biāo)識(shí),
// ...];
$order = new Order($attributes);
統(tǒng)一下單
$result = $payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
$prepayId = $result->prepay_id;
}
支付結(jié)果通知
在用戶(hù)成功支付后,微信服務(wù)器會(huì)向該 訂單中設(shè)置的回調(diào)URL 發(fā)起一個(gè) POST 請(qǐng)求,請(qǐng)求的內(nèi)容為一個(gè) XML。里面包含了所有的詳細(xì)信息,具體請(qǐng)參考: 支付結(jié)果通用通知
在本 SDK 中處理回調(diào)真的再簡(jiǎn)單不過(guò)了,請(qǐng)求驗(yàn)證你就不用管了,SDK 已經(jīng)為你做好了,你只需要關(guān)注業(yè)務(wù)即可:
$response = $app->payment->handleNotify(function($notify, $successful){
// 你的邏輯
return true; // 或者錯(cuò)誤消息});
$response->send(); // Laravel 里請(qǐng)使用:return $response;
使用EasyWeChat完成提現(xiàn)到零錢(qián)功能(企業(yè)付款到零錢(qián))
你在閱讀本文之前確認(rèn)你已經(jīng)仔細(xì)閱讀了:微信支付 | 企業(yè)付款文檔 。
與其他支付接口一樣,企業(yè)支付接口也需要配置如下參數(shù),需要特別注意的是,企業(yè)支付相關(guān)的全部接口 都需要使用 SSL 證書(shū),因此 cert_path 以及 cert_key 必須正確配置。
<?php
use EasyWeChat\Foundation\Application;
$options = [
'app_id' => 'your-app-id',
// payment
'payment' => [
'merchant_id' => 'your-mch-id',
'key' => 'key-for-signature',
'cert_path' => 'path/to/your/cert.pem',
'key_path' => 'path/to/your/key',
// ...
],];
$app = new Application($options);
$merchantPay = $app->merchant_pay;
企業(yè)付款
企業(yè)付款使用的余額跟微信支付的收款并非同一賬戶(hù),請(qǐng)注意充值。
<?php
$merchantPayData = [
'partner_trade_no' => str_random(16), //隨機(jī)字符串作為訂單號(hào),跟紅包和支付一個(gè)概念。
'openid' => $openid, //收款人的openid
'check_name' => 'NO_CHECK', //文檔中有三種校驗(yàn)實(shí)名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
're_user_name'=>'張三', //OPTION_CHECK FORCE_CHECK 校驗(yàn)實(shí)名的時(shí)候必須提交
'amount' => 100, //單位為分
'desc' => '企業(yè)付款',
'spbill_create_ip' => '192.168.0.1', //發(fā)起交易的IP地址
];
$result = $merchantPay->send($merchantPayData);
如同已上的兩個(gè)功能一樣,使用EasyWeChat就可以快速、簡(jiǎn)單的完成和微信相關(guān)的功能了。