shulyak/mercadopago

该软件包最新版本(1.0.0)没有可用的许可信息。

MercadoPago SDK模块,用于支付集成。

1.0.0 2021-11-10 12:02 UTC

This package is auto-updated.

Last update: 2024-09-10 18:10:21 UTC


README

安装

使用Composer

从命令行

composer require shulyak/mercadopago:1.0.0

作为项目composer.json中的依赖项

{
    "require": {
        "shulyak/mercadopago": "1.0.0"
    }
}

下载

  1. 克隆/下载此仓库
  2. lib/mercadopago.php 复制到项目所需的文件夹。

基本结账

配置您的凭证

require_once ('mercadopago.php');

$mp = new MP ("CLIENT_ID", "CLIENT_SECRET");

首选项

获取现有的结账首选项

$preference = $mp->get_preference("PREFERENCE_ID");

print_r ($preference);

创建结账首选项

$preference_data = array (
    "items" => array (
        array (
            "title" => "Test",
            "quantity" => 1,
            "currency_id" => "USD",
            "unit_price" => 10.4
        )
    )
);

$preference = $mp->create_preference($preference_data);

print_r ($preference);

更新现有的结账首选项

$preference_data = array (
    "items" => array (
        array (
            "title" => "Test Modified",
            "quantity" => 1,
            "currency_id" => "USD",
            "unit_price" => 20.4
        )
    )
);

$preference = $mp->update_preference("PREFERENCE_ID", $preference_data);

print_r ($preference);

支付/收款

搜索支付

$filters = array (
        "id"=>null,
        "site_id"=>null,
        "external_reference"=>null
    );

$searchResult = $mp->search_payment ($filters);

print_r ($searchResult);

获取支付数据

require_once ('mercadopago.php');

$mp = new MP ("CLIENT_ID", "CLIENT_SECRET");
$paymentInfo = $mp->get_payment ("PAYMENT_ID");

print_r ($paymentInfo);

取消(仅限待处理支付)

$result = $mp->cancel_payment("PAYMENT_ID");

print_r ($result);

退款(仅限已授权支付)

$result = $mp->refund_payment("PAYMENT_ID");

print_r ($result);

自定义结账

配置您的凭证

require_once ('mercadopago.php');

$mp = new MP ("ACCESS_TOKEN");

创建支付

$mp->post (
    array(
        "uri" => "/v1/payments",
        "data" => [payment_data]
    )
);

创建客户

$mp->post (
    array(
        "uri" => "/v1/customers",
        "data" => array(
            "email" => "email@test.com"
        )
    )
);

获取客户

$mp->get (
    array(
        "uri" => "/v1/customers/CUSTOMER_ID"
    )
);

通用方法

您可以使用通用方法访问MercadoPago API中的任何资源。基本结构是

$mp->[method]($request)

其中,request可以是

array(
    "uri" => "The resource URI, relative to https://api.mercadopago.com",
    "params" => "Optional. Key=>Value array with parameters to be appended to the URL",
    "data" => "Optional. Object or String to be sent in POST and PUT requests",
    "headers" => "Optional. Key => Value array with custom headers, like content-type: application/x-www-form-urlencoded",
    "authenticate" => "Optional. Boolean to specify if the GET method has to authenticate with credentials before request. Set it to false when accessing public APIs"
)

示例

// Get a resource, with optional URL params. Also you can disable authentication for public APIs
$mp->get (
    array(
        "uri" => "/resource/uri",
        "params" => array(
            "param" => "value"
        ),
        "headers" => array(
            "header" => "value"
        ),
        "authenticate" => true
    )
);

// Create a resource with "data" and optional URL params.
$mp->post (
    array(
        "uri" => "/resource/uri",
        "params" => array(
            "param" => "value"
        ),
        "headers" => array(
            "header" => "value"
        ),
        "data" => [data]
    )
);

// Update a resource with "data" and optional URL params.
$mp->put (
    array(
        "uri" => "/resource/uri",
        "params" => array(
            "param" => "value"
        ),
        "headers" => array(
            "header" => "value"
        ),
        "data" => [data]
    )
);

// Delete a resource with optional URL params.
$mp->delete (
    array(
        "uri" => "/resource/uri",
        "params" => array(
            "param" => "value"
        ),
        "headers" => array(
            "header" => "value"
        )
    )
);