laravel-pagseguro 使用 PagSeguro API 並提供一種簡單的方法來產生付款並通知您的交易。
在使用laravel pagseguro請務必檢查您的 PagSeguro 使用者名稱是否正確以進行整合。
PHP >= 5.4 Laravel 5.x
開啟composer.json
檔案並輸入以下語句:
"require": {
"michael/laravelpagseguro": "dev-master"
}
注意:對於 laravel 5.1 或更低版本,請指定版本0.4.1而不是使用dev-master
將laravel pagseguro
插入 require 後,必須執行指令:
composer update
或運行命令:
composer require michael/laravelpagseguro:dev-master
開啟config/app.php
檔案並將以下語句加入到providers
陣列中:
laravel pagseguro Platform Laravel5 ServiceProvider::class
在config/app.php
檔案中,將以下語句加入到aliases
陣列中:
' PagSeguro ' => laravel pagseguro Platform Laravel5 PagSeguro::class
現在您將運行命令:
php artisan vendor:publish
如果一切順利,將顯示以下訊息:
Copied File [/vendor/michael/laravelpagseguro/src/laravel/pagseguro/Config/laravelpagseguro.php] To [/config/laravelpagseguro.php]
開啟config/laravelpagseguro.php
檔案並更改token
以及通知您商店的e-mail
:
' credentials ' => array ( //SETA AS CREDENCIAIS DE SUA LOJA
' token ' => null ,
' email ' => null ,
)
如果您需要代理程式來使用laravel pagseguro請取消註解並設定 http 適配器行:
' http ' => [
' adapter ' => [
' type ' => ' curl ' ,
' options ' => [
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0 ,
// CURLOPT_PROXY => 'http://user:pass@host:port', // PROXY OPTION <<--
]
],
],
發送數組必須依照以下結構組裝:
$ data = [
' items ' => [
[
' id ' => ' 18 ' ,
' description ' => ' Item Um ' ,
' quantity ' => ' 1 ' ,
' amount ' => ' 1.15 ' ,
' weight ' => ' 45 ' ,
' shippingCost ' => ' 3.5 ' ,
' width ' => ' 50 ' ,
' height ' => ' 45 ' ,
' length ' => ' 60 ' ,
],
[
' id ' => ' 19 ' ,
' description ' => ' Item Dois ' ,
' quantity ' => ' 1 ' ,
' amount ' => ' 3.15 ' ,
' weight ' => ' 50 ' ,
' shippingCost ' => ' 8.5 ' ,
' width ' => ' 40 ' ,
' height ' => ' 50 ' ,
' length ' => ' 80 ' ,
],
],
' shipping ' => [
' address ' => [
' postalCode ' => ' 06410030 ' ,
' street ' => ' Rua Leonardo Arruda ' ,
' number ' => ' 12 ' ,
' district ' => ' Jardim dos Camargos ' ,
' city ' => ' Barueri ' ,
' state ' => ' SP ' ,
' country ' => ' BRA ' ,
],
' type ' => 2 ,
' cost ' => 30.4 ,
],
' sender ' => [
' email ' => ' [email protected] ' ,
' name ' => ' Isaque de Souza Barbosa ' ,
' documents ' => [
[
' number ' => ' 01234567890 ' ,
' type ' => ' CPF '
]
],
' phone ' => [
' number ' => ' 985445522 ' ,
' areaCode ' => ' 11 ' ,
],
' bornDate ' => ' 1988-03-21 ' ,
]
];
取得資料後,使用createFromArray
方法建立結帳物件:
$ checkout = PagSeguro:: checkout ()-> createFromArray ( $ data );
確認發送,使用方法: send
如下:
$ checkout = PagSeguro:: checkout ()-> createFromArray ( $ data );
$ credentials = PagSeguro:: credentials ()-> get ();
$ information = $ checkout -> send ( $ credentials ); // Retorna um objeto de laravelpagseguroCheckoutInformationInformation
if ( $ information ) {
print_r ( $ information -> getCode ());
print_r ( $ information -> getDate ());
print_r ( $ information -> getLink ());
}
上報手機儲值元數據:
// ....
$ data [ ' cellphone_charger ' ] = ' +5511980810000 ' ;
$ checkout = PagSeguro:: checkout ()-> createFromArray ( $ data );
旅行數據的報告元數據:
// ....
$ data [ ' travel ' ] = [
' passengers ' => [
[
' name ' => ' Isaque de Souza ' ,
' cpf ' => ' 40404040411 ' ,
' passport ' => ' 4564897987 '
],
[
' name ' => ' Michael Douglas ' ,
' cpf ' => ' 80808080822 ' ,
]
],
' origin ' => [
' city ' => ' SAO PAULO - SP ' ,
' airportCode ' => ' CGH ' , // Congonhas
],
' destination ' => [
' city ' => ' RIO DE JANEIRO - RJ ' ,
' airportCode ' => ' SDU ' , // Santos Dumont
]
];
$ checkout = PagSeguro:: checkout ()-> createFromArray ( $ data );
報告遊戲元數據:
// ....
$ data [ ' game ' ] = [
' gameName ' => ' PS LEGEND ' ,
' playerId ' => ' BR561546S4 ' ,
' timeInGameDays ' => 360 ,
];
$ checkout = PagSeguro:: checkout ()-> createFromArray ( $ data );
要從文件中檢索預設憑證,您可以使用:
$ credentials = PagSeguro:: credentials ()-> get ();
或使用替代憑證
$ credentials = PagSeguro:: credentials ()-> create ( $ token , $ email );
$ credentials = PagSeguro:: credentials ()-> get ();
$ transaction = PagSeguro:: transaction ()-> get ( $ code , $ credentials );
$ information = $ transaction -> getInformation ();
建立一個名為「pagseguro.notification」的 POST 路由(這在設定中)
Route:: post ( ' /pagseguro/notification ' , [
' uses ' => ' laravelpagseguroPlatformLaravel5NotificationController@notification ' ,
' as ' => ' pagseguro.notification ' ,
]);
在 laravelpagseguro.php 設定中註冊回調(可呼叫)
' routes ' => [
' notification ' => [
' callback ' => [ ' MyNotificationClass ' , ' myMethod ' ], // Callable
' credential ' => ' default ' ,
' route-name ' => ' pagseguro.notification ' , // Nome da rota
],
],
或者....
' routes ' => [
' notification ' => [
' callback ' => function ( $ information ) { // Callable
Log:: debug ( print_r ( $ information , 1 ));
},
],
],
在設定檔中,您應該將其保留如下:
' notification ' => [
' callback ' => [ ' AppControllersPagSeguroController ' , ' Notification ' ], // Callable callback to Notification function (notificationInfo) : void {}
' credential ' => ' default ' , // Callable resolve credential function (notificationCode) : Credentials {}
' route-name ' => ' pagseguro.notification ' , // Criar uma rota com este nome
],
並且在控制器中您必須建立方法,例如通知:
public static function Notification ( $ information )
{
Log:: debug ( print_r ( $ information -> getStatus ()-> getCode (), 1 ));
}
建立定期付款計劃從建立計劃開始,為此,您必須建立以下陣列:
如果您想查看請求對象:https://dev.pagseguro.uol.com.br/v1.0/reference#criar-plano
$ plan = [
' body ' => [
' reference ' => ' plano laravel pagseguro ' ,
],
' preApproval ' => [
' name ' => ' Plano ouro - mensal ' ,
' charge ' => ' AUTO ' , // outro valor pode ser MANUAL
' period ' => ' MONTHLY ' , //WEEKLY, BIMONTHLY, TRIMONTHLY, SEMIANNUALLY, YEARLY
' amountPerPayment ' => ' 125.00 ' , // obrigatório para o charge AUTO - mais que 1.00, menos que 2000.00
' membershipFee ' => ' 50.00 ' , //opcional - cobrado com primeira parcela
' trialPeriodDuration ' => 30 , //opcional
' details ' => ' Decrição do plano ' , //opcional
' expiration ' => [ // opcional
' value ' => 1 , // obrigatório de 1 a 1000000
' unit ' => ' YEARLY ' , // obrigatório
],
]
];
然後你必須呼叫計劃創建方法:
$ plan = PagSeguro:: plan ()-> createFromArray ( $ plan );
$ credentials = PagSeguro:: credentials ()-> get ();
$ information = $ plan -> send ( $ credentials ); // Retorna um objeto de laravelpagseguroCheckoutInformationInformation
if ( $ information ) {
print_r ( $ information -> getCode ());
print_r ( $ information -> getDate ());
print_r ( $ information -> getLink ());
}
laravel pagseguro使用 MIT 許可證,要了解更多信息,請閱讀連結:MIT 許可證