crizprz/laravelmercadopago
Composer 安装命令:
composer require crizprz/laravelmercadopago
包简介
This package integrates Mercado Pago's Checkout Pro easily
README 文档
README
This package integrates Mercado Pago's Checkout Pro very easily
REQUIREMENTS
Composer v.2 is recommended
guzzlehttp/guzzle
INSTALLATION
Install the package through Composer.
composer require crizprz/laravelmercadopago
Or add the following to your composer.json file :
"require": { "crizprz/laravelmercadopago": "^1.0", },
CONFIGURATION
- Open
config/app.phpand add this line to your Service Providers Array.
crizprz\laravelmercadopago\Providers\LaravelMercadoPagoProvider::class,
- Open
config/app.phpand add this line to your aliases Array.
'MercadoPago' => crizprz\laravelmercadopago\Facades\MercadoPago::class,
- publish the assets.
php artisan vendor:publish --provider="crizprz\laravelmercadopago\Providers\LaravelMercadoPagoProvider"
HOW TO USE
Create User Test
- To create users we must log in to Mercado Pago developers with a real account
- Go to the top right section, click and select your integrations
- once in your integrations click on test credentials, copy the Access Token and put it in the
.envfile in the variable calledACCESS_TOKEN_MP = - paste this code somewhere in your code either in a route or a running controller, this code will print two test users, the seller and the buyer
use crizprz\laravelmercadopago\Facades\MercadoPago;
MercadoPago::CreateUsersTest();
This will return an array with the two types of users, we must save these users very well since the paid market only allows creating 10 test users per account.
Note: Run only once.
example of what the previous code returns
array:2 [▼ 0 => array:5 [▼ "id" => 752892812 "nickname" => "TESTE2VYDAPX" "email" => "test_user_84921269@testuser.com" "password" => "qatest3393" "type User" => "seller" ] 1 => array:5 [▼ "id" => 752890064 "nickname" => "TETE9884170" "email" => "test_user_45814566@testuser.com" "password" => "qatest6713" "type User" => "payer" ] ]
create payment preference with test user
- login with the test user in Mercado Pago developers
- Go to the top right section, click and select your integrations
- create new integration fill in the form and click on create application, click on production keys, copy the Access Token and put it in the
.envfile in the variable calledACCESS_TOKEN_MP =
Create Preference
1.paste the following code somewhere in your project where you want to implement the payment button
use crizprz\laravelmercadopago\Facades\MercadoPago; $preference = MercadoPago::CreatePreference([ 'auto_return' => 'approved', 'binary_mode' => true, 'back_urls' => [ "success" => route('mpsuccess'), "failure" => route('mpfailure'), "pending" => route('mppending') ], 'notification_url' => route('webhook'), 'items' => [ 'id' => '001', 'title' => 'prod 1', 'picture_url' => 'https://www.tusitio.com/images/products/prod1.jpg', 'description' => 'this is descriptions', 'category_id' => 'food', 'quantity' => 1, 'price' => 1000, ], 'payment_methods' => [ 'excluded_payment_methods' => array( array('id' => 'master'), // array('id' => 'visa'), ), 'excluded_payment_types' => array( array('id' => 'ticket') ), 'installments' => 12 ], 'payer' => [ 'name' => 'Cristian', 'last_name' => 'Lira Perez', 'email' => 'al221711754@gmail.com', 'phone' => [ 'area_code' => +52, 'number' => '7224738425' ], 'address' => [ 'zip_code' => 23492, 'street_name' => 'Calle cerrada de juerez', 'street_number' => '104' ] ], 'shipments' => [ 'cost' => 100, ], 'statement_descriptor' => 'Mi tienda online', ]);
- visit the api reference, Create a preference to know what each of the properties of the array means
- change the array values to those of your project
create payment button
- return the variable
$preferenceto some blade view, this variable has saved all the payment prefence For Example
return view('checkout',['preference' => $preference]);
- in the view render the default button of the package
{{ MercadoPago::ButtonPay($preference->init_point) }}
or create a custom payment button and pass it the init_point, which is stored in the $ preference variable For Example
<a class="btn btn-info mt-5" href="{{ $preference->init_point }}">Pay Now</a>
Test your integration
- read the Test your integration documentation to get the test cards it provides
Payment status responses
payment success
The code that is executed when returning to your website from a successful payment is in the App\Http\Controllers\MercadoPagoController controller in the success method
public function success(Request $request) { // dd($request->all()); if ($request->status === 'approved') { $response = Http::get("https://api.mercadopago.com/v1/payments/{$request->payment_id}", [ 'access_token' => env('ACCESS_TOKEN_MP'), ])->json(); dd($response); } }
the payment returns a get request to your server so we will use the Request $request where all the payment data comes, what we are interested in is the payiment_id
sample answer
array:11 [▼ "collection_id" => "14694550173" "collection_status" => "approved" "payment_id" => "14694550173" "status" => "approved" "external_reference" => "null" "payment_type" => "credit_card" "merchant_order_id" => "2621979593" "preference_id" => "752892812-d318bf82-4eea-4748-af60-cd4ade381491" "site_id" => "MLM" "processing_mode" => "aggregator" "merchant_account_id" => "null" ]
We are going to do a get request with guzzle / huzzlehttp to find the payment that was made and thus obtain the details, such as amount, type of currency, buyer data etc ...
if ($request->status === 'approved') { $response = Http::get("https://api.mercadopago.com/v1/payments/{$request->payment_id}", [ 'access_token' => env('ACCESS_TOKEN_MP'), ])->json(); dd($response); }
In the $ response variable we have all the data of the payment made successfully, with this information we can send emails, push notifications, show session messages in laravel, etc.
crizprz/laravelmercadopago 适用场景与选型建议
crizprz/laravelmercadopago 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「mercadopago」 「Web Checkout」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 crizprz/laravelmercadopago 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crizprz/laravelmercadopago 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 crizprz/laravelmercadopago 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model of a web-based resource
Mercado Pago API SDK for Laravel
Retrieve a WebResourceInterface instance over HTTP
Mercado Pago API SDK for Laravel
Selectize Theme for Bootstrap 4
Provides command to manage a web library directory
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-02