marceltk / pagseguro-api-laravel
PagSeguro支付网关集成库。
Requires
- php: >=5.4.0
- ext-curl: *
- lib-curl: *
- lib-libxml: *
- lib-openssl: *
- illuminate/validation: >=4.2
Requires (Dev)
- phpunit/phpunit: 4.0.*
README
laravel-pagseguro库消费PagSeguro API,提供了一个简单的方式来生成支付和通知交易。
用户创建和配置
在您使用Laravel PagSeguro之前,请确保您的PagSeguro用户设置正确,以便集成,请参阅用户配置URL:[https://pagseguro.uol.com.br/preferencias/integracoes.jhtml](https://pagseguro.uol.com.br/preferencias/integracoes.jhtml)
兼容性
PHP >= 5.4 Laravel 5.x
安装
打开composer.json
文件,并插入以下指令
"require": {
"marceltk/pagseguro-api-laravel": "dev-master"
}
在require中插入Laravel PagSeguro后,您需要执行以下命令
composer update
或者执行以下命令
composer require marceltk/pagseguro-api-laravel:dev-master
Service Provider配置
打开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' => 'sender@gmail.com', 'name' => 'Isaque de Souza Barbosa', 'documents' => [ [ 'number' => '01234567890', 'type' => 'CPF' ] ], 'phone' => '11985445522', '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 laravel\pagseguro\Checkout\Information\Information 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路由(在config中指定)
Route::post('/pagseguro/notification', [ 'uses' => '\laravel\pagseguro\Platform\Laravel5\NotificationController@notification', 'as' => 'pagseguro.notification', ]);
在您的laravelpagseguro.php配置中注册一个回调(callable)
'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)); }, ], ],
许可
Laravel PagSeguro使用MIT许可,更多信息请参阅链接:[MIT license](https://open-source.org.cn/licenses/MIT)