pagarme/pagarme-php

Pagar.Me PHP 库

安装次数: 1,033,475

依赖者: 17

建议者: 0

安全: 1

星标: 351

关注者: 119

分支: 149

开放性问题: 53

类型:lib


README

这个 SDK 是为了使其更加灵活而构建的,以便所有用户都可以使用所有版本的 API 中的所有功能。

您可以通过访问此 链接 来获取 Pagar.me 的官方文档。

目录

安装

使用以下命令安装库

composer require pagarme/pagarme-php

配置

要将其库包含到您的项目中,只需执行以下操作

<?php
require('vendor/autoload.php');

$pagarme = new PagarMe\Client('SUA_CHAVE_DE_API');

定义自定义头

  1. 如果需要,您可以定义自定义 HTTP 头来请求。为了这样做,只需在实例化 Client 对象时提供它们即可。
<?php
require('vendor/autoload.php');

$pagarme = new PagarMe\Client(
    'SUA_CHAVE_DE_API',
    ['headers' => ['MEU_HEADER_CUSTOMIZADO' => 'VALOR HEADER CUSTOMIZADO']]
); 

然后,您可以使用该客户端进行 Pagar.me 的请求,如下例所示。

交易

在本节中,将解释如何使用此库在 Pagar.me 中使用交易。

创建交易

<?php
$transaction = $pagarme->transactions()->create([
    'amount' => 1000,
    'payment_method' => 'credit_card',
    'card_holder_name' => 'Anakin Skywalker',
    'card_cvv' => '123',
    'card_number' => '4242424242424242',
    'card_expiration_date' => '1220',
    'customer' => [
        'external_id' => '1',
        'name' => 'Nome do cliente',
        'type' => 'individual',
        'country' => 'br',
        'documents' => [
          [
            'type' => 'cpf',
            'number' => '00000000000'
          ]
        ],
        'phone_numbers' => [ '+551199999999' ],
        'email' => '[email protected]'
    ],
    'billing' => [
        'name' => 'Nome do pagador',
        'address' => [
          'country' => 'br',
          'street' => 'Avenida Brigadeiro Faria Lima',
          'street_number' => '1811',
          'state' => 'sp',
          'city' => 'Sao Paulo',
          'neighborhood' => 'Jardim Paulistano',
          'zipcode' => '01451001'
        ]
    ],
    'shipping' => [
        'name' => 'Nome de quem receberá o produto',
        'fee' => 1020,
        'delivery_date' => '2018-09-22',
        'expedited' => false,
        'address' => [
          'country' => 'br',
          'street' => 'Avenida Brigadeiro Faria Lima',
          'street_number' => '1811',
          'state' => 'sp',
          'city' => 'Sao Paulo',
          'neighborhood' => 'Jardim Paulistano',
          'zipcode' => '01451001'
        ]
    ],
    'items' => [
        [
          'id' => '1',
          'title' => 'R2D2',
          'unit_price' => 300,
          'quantity' => 1,
          'tangible' => true
        ],
        [
          'id' => '2',
          'title' => 'C-3PO',
          'unit_price' => 700,
          'quantity' => 1,
          'tangible' => true
        ]
    ]
]);

捕获交易

<?php
$capturedTransaction = $pagarme->transactions()->capture([
    'id' => 'ID_OU_TOKEN_DA_TRANSAÇÃO',
    'amount' => VALOR_TOTAL_COM_CENTAVOS
]);

退款交易

<?php
$refundedTransaction = $pagarme->transactions()->refund([
    'id' => 'ID_OU_TOKEN_DA_TRANSAÇÃO',
]);

此功能还适用于部分退款或分割退款。例如

部分退款交易

<?php
$partialRefundedTransaction = $pagarme->transactions()->refund([
    'id' => 'ID_OU_TOKEN_DA_TRANSAÇÃO',
    'amount' => 'VALOR_PARCIAL_DO_ESTORNO',
]);

分割退款交易

<?php
$refundedTransactionWithSplit = $pagarme->transactions()->refund([
    'id' => 'ID_OU_TOKEN_DA_TRANSAÇÃO',
    'amount' => '6153',
    'split_rules' => [
        [
            'id' => 'sr_cj41w9m4d01ta316d02edaqav',
            'amount' => '3000',
            'recipient_id' => 're_cj2wd5ul500d4946do7qtjrvk'
        ],
        [
            'id' => 'sr_cj41w9m4e01tb316dl2f2veyz',
            'amount' => '3153',
            'recipient_id' => 're_cj2wd5u2600fecw6eytgcbkd0',
            'charge_processing_fee' => 'true'
        ]
    ]
]);

返回交易

<?php
$transactions = $pagarme->transactions()->getList();

如有需要,您可以使用参数来过滤此搜索,例如,如果您只想过滤已支付的交易,则可以使用以下代码

<?php
$paidTransactions = $pagarme->transactions()->getList([
    'status' => 'paid'
]);

返回单个交易

<?php
$transactions = $pagarme->transactions()->get([
    'id' => 'ID_DA_TRANSAÇÃO' 
]);

返回交易应收款

<?php
$transactionPayables = $pagarme->transactions()->listPayables([
    'id' => 'ID_DA_TRANSAÇÃO'
]);

返回单个交易应收款

<?php
$transactionPayable = $pagarme->transactions()->getPayable([
    'transaction_id' => 'ID_DA_TRANSAÇÃO',
    'payable_id' => 'ID_DO_PAYABLE'
]);

返回交易操作历史

<?php
$transactionOperations = $pagarme->transactions()->listOperations([
    'id' => 'ID_DA_TRANSAÇÃO',
]);

通知客户支付支票

<?php
$transactionPaymentNotify = $pagarme->transactions()->collectPayment([
    'id' => 'ID_DA_TRANSAÇÃO',
    'email' = > '[email protected]'
]);

返回交易事件

<?php
$transactionEvents = $pagarme->transactions()->events([
    'id' => 4262049,
]);

计算分期付款

此路由并非必须使用。它只是计算 Pagar.me 的分期付款的一种方式。

为了解释目的,我们将使用以下值

amount: 1000, free_installments: 4, max_installments: 12, interest_rate: 3

free_installments 参数决定无息分期数量。也就是说,如果填入值为 4,则前四个分期不会改变其原始价值。

在此路由中,计算的是单利,进行以下计算

valorTotal = valorDaTransacao * ( 1 + ( taxaDeJuros * numeroDeParcelas ) / 100 )

然后,使用上述值,在第五期中,计算如下

valorTotal = 1000 * (1 + (3 * 5) / 100)

因此,第五期的付款金额将是购买金额的 15%,总计 1150。

如果您想使用此路由,可以使用以下代码

<?php
$calculateInstallments = $pagarme->transactions()->calculateInstallments([
    'amount' => 'VALOR_DA_TRANSAÇÃO_EM_CENTAVOS',
    'free_installments' => 'PARCELAS_SEM_JUROS',
    'max_installments' => 'MÁXIMO_DE_PARCELAS',
    'interest_rate' => 'TAXA_DE_JUROS_AO_MÊS'
]);

测试支票支付

<?php
$paidBoleto = $pagarme->transactions()->simulateStatus([
    'id' => 'ID_DA_TRANSAÇÃO',
    'status' => 'paid'
]);

退款

您可以通过以下代码查看您账户中发生的所有退款

<?php
$refunds = $pagarme->refunds()->getList();

如果您想查看特定交易的退款,可以使用过滤器,例如

<?php
$transactionRefunds = $pagarme->refunds()->getList([
    'transaction_id' => 'ID_DA_TRANSAÇÃO_ESTORNADA'
]);

退单

与退款类似,您还可以查看您账户中发生的所有退款

<?php
$transactionChargebacks = $pagarme->refunds()->getList();

卡片

每次您通过我们的 API 发起请求时,我们都会保存持卡人的信息,以便您将来可用于新的收费,甚至实现一键购买等功能。

创建卡片

<?php
$card = $pagarme->cards()->create([
    'holder_name' => 'Yoda',
    'number' => '4242424242424242',
    'expiration_date' => '1225',
    'cvv' => '123'
]);

返回卡片

<?php
$cards = $pagarme->cards()->getList();

如有需要,您可以根据卡的具体数据过滤,例如,以下代码将检索所有 visa 品牌的卡

<?php
$visaCards = $pagarme->cards()->getList([
    'brand' => 'visa'
]);

返回单个卡片

<?php
$card = $pagarme->cards()->get([
    'id' => 'ID_DO_CARTÃO'
]);

计划

表示客户可以订阅的周期性配置。这是定义周期性价格、名称和频率的实体

创建计划

<?php
$plan = $pagarme->plans()->create([
    'amount' => '15000',
    'days' => '30',
    'name' => 'The Pro Plan - Platinum - Best ever'
]);

返回计划

<?php
$plans = $pagarme->plans()->getList();

返回单个计划

<?php
$plan = $pagarme->plans()->get(['id' => '123456']);

更新计划

<?php
$updatedPlan = $pagarme->plans()->update([
    'id' => '365403',
    'name' => 'The Pro Plan - Susan',
    'trial_days' => '7',
]);

订阅

创建订阅

<?php
$substription = $pagarme->subscriptions()->create([
    'plan_id' => 123456,
    'payment_method' => 'credit_card',
    'card_number' => '4111111111111111',
    'card_holder_name' => 'UNIX TIME',
    'card_expiration_date' => '0722',
    'card_cvv' => '123',
    'postback_url' => 'http://postbacj.url',
    'customer' => [
        'email' => '[email protected]',
        'name' => 'Unix Time',
        'document_number' => '75948706036',
        'address' => [
            'street' => 'Rua de Teste',
            'street_number' => '100',
            'complementary' => 'Apto 666',
            'neighborhood' => 'Bairro de Teste',
            'zipcode' => '11111111'
        ],
        'phone' => [
            'ddd' => '01',
            'number' => '923456780'
        ],
        'sex' => 'other',
        'born_at' => '1970-01-01',
    ],
    'metadata' => [
        'foo' => 'bar'
    ]
]);

使用 card_id 创建订阅

<?php
// Criando o cartão
$card = $pagarme->cards()->create([
    'holder_name' => 'Yoda',
    'number' => '4242424242424242',
    'expiration_date' => '1225',
    'cvv' => '123'
]);

$substription = $pagarme->subscriptions()->create([
    'plan_id' => 365403,
    'card_id' => $card->id,
    'payment_method' => 'credit_card',
    'postback_url' => 'http://www.pudim.com.br',
    'customer' => [
        'email' => '[email protected]',
        'name' => 'Unix Time',
        'document_number' => '75948706036',
        'address' => [
            'street' => 'Rua de Teste',
            'street_number' => '100',
            'complementary' => 'Apto 666',
            'neighborhood' => 'Bairro de Teste',
            'zipcode' => '88370801'
        ],
        'phone' => [
            'ddd' => '01',
            'number' => '923456780'
        ],
        'sex' => 'other',
        'born_at' => '1970-01-01',
    ],
    'metadata' => [
        'foo' => 'bar'
    ]
]);

订阅分割

<?php
$substription = $pagarme->subscriptions()->create([
    'plan_id' => 123456,
    'card_id' => 'card_abc123456',
    'payment_method' => 'credit_card',
    'postback_url' => 'http://www.pudim.com.br',
    'customer' => [
        'email' => '[email protected]',
        'name' => 'Unix Time',
        'document_number' => '75948706036',
        'address' => [
            'street' => 'Rua de Teste',
            'street_number' => '100',
            'complementary' => 'Apto 666',
            'neighborhood' => 'Bairro de Teste',
            'zipcode' => '88370801'
        ],
        'phone' => [
            'ddd' => '01',
            'number' => '923456780'
        ],
        'sex' => 'other',
        'born_at' => '1970-01-01',
    ],
    'amount' => 10000,
    'split_rules' => [
        [
            'recipient_id' => 're_abc1234abc1234abc1234abc1',
            'percentage' => 20,
            'liable' => true,
            'charge_processing_fee' => true,
        ],
        [
            'recipient_id' => 're_abc1234abc1234abc1234abc1',
            'percentage' => 80,
            'liable' => true,
            'charge_processing_fee' => true,
        ]
    ],
    'metadata' => [
        'foo' => 'bar'
    ]
]);

返回单个订阅

<?php
$substription = $pagarme->subscriptions()->get([
    'id' => 123456
]);

返回订阅列表

<?php
$substription = $pagarme->subscriptions()->getList();

如有需要,您可以在搜索中应用过滤器。例如,如果您想获取特定计划的全部订阅,可以使用以下代码

<?php
$planSubstriptions = $pagarme->subscriptions()->getList([
    'plan_id' => 'ID_DO_PLANO'
]);

更新订阅

<?php
$updatedSubscription = $pagarme->subscriptions()->update([
    'id' => 1234,
    'plan_id' => 4321,
    'payment_method' => 'boleto'
]);

取消订阅

<?php
$canceledSubscription = $pagarme->subscriptions()->cancel([
    'id' => 12345
]);

订阅交易

<?php
$substriptionTransactions = $pagarme->subscriptions()->transactions([
    'subscription_id' => 1245
]);

跳过收费

<?php
$settledCharges = $pagarme->subscriptions()->settleCharges([
    'id' => 12345,
    'charges' => 5
]);

Postbacks

创建交易或订阅时,您可以选择在请求中传递 postback_url 参数。这是您的系统 URL,它将接收每次这些交易/订阅状态更改的通知。

要获取有关 postback 的信息,需要 3 项信息,分别是:modelmodel_idpostback_id

model:指生成该 POSTback 的对象。可以填入 transactionsubscription

model_id:指生成 POSTback 的对象的 ID,即您想访问 POSTback 的交易或订阅的 ID。

postback_id:指特定的通知。对于每次订阅或交易的每个状态更改,都会生成一个 POSTback。每个 POSTback 都可以有多个投递尝试,这些尝试可以通过 deliveries 字段识别,而尝试的 ID 以 pd_ 前缀标识。在此参数中应发送的字段是 POSTback 的 ID,该 ID 以 po_ 前缀标识。

返回 postbacks

<?php
$postbacks = $pagarme->postbacks()->getList([
    'model' => 'subscription',
    'model_id' => 'ID_DA_ASSINATURA'
]);

返回单个 postback

<?php
$postback = $pagarme->postbacks()->get([
    'model' => 'transaction',
    'model_id' => 'ID_DA_TRANSAÇÃO',
    'postback_id' => 'po_cjlzhftd2006xg573fwelfg9y'
]);

重新发送 postback

<?php
$postbackRedeliver = $pagarme->postbacks()->redeliver([
    'model' => 'subscription',
    'model_id' => 'ID_DA_ASSINATURA',
    'postback_id' => 'po_cjlzhftd2006xg573fwelfg9y'
]);

验证 postback 请求

<?php
$postbackPayload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$postbackIsValid = $pagarme->postbacks()->validate($postbackPayload, $signature);

注意:上面的代码仅作为验证过程的示例。我们建议您使用由库或框架提供的工具以更合适的方式检索这些信息。

主要收款人余额

要获取您账户的余额,可以使用以下代码

<?php
$balance = $pagarme->balances()->get();

余额操作

使用此对象,您可以跟踪 Pagar.me 账户中发生的所有财务活动。

操作历史

<?php
$balanceOperations = $pagarme->balanceOperations()->getList();

如有需要,您可以通过参数传递过滤器,例如

<?php
$balanceOperations = $pagarme->balanceOperations()->getList([
    'status' => 'available'
]);

特定操作历史

<?php
$balanceOperation = $pagarme->balanceOperations()->get([
    'id' => 'BALANCE_OPERATION_ID'
]);

应收款

包含收据数据的对象。收据(应付)在支付交易后自动生成。每个交易的部分都会生成一个收据,这些收据也可以按收款人(如果已进行拆分)进行划分。

返回应收款

<?php
$payables = $pagarme->payables()->getList();

如果需要,您可以在搜索应付项时应用筛选器,例如,您可以检索所有交易中的应付项

<?php
$transactionPayables = $pagarme->payables()->getList([
    'transaction_id' => 'ID_DA_TRANSAÇÃO'
]);

返回单个应收款

<?php
$payable = $pagarme->payables()->get([
    'id' => 'ID_DO_PAYABLE'
]);

转账

转账代表从您的账户中提款。

创建转账

<?php
$transfer = $pagarme->transfers()->create([
    'amount' => 1000,
    'recipient_id' => 're_cjeptpdyg03u3cb6elj68p5ej'
]);

返回转账

<?php
$transfers = $pagarme->transfers()->getList();

如果需要,您可以在搜索时应用筛选器,例如

<?php
$recipientTransfers = $pagarme->transfers()->getList([
    'recipient_id' => 'ID_DO_RECEBEDOR'
]);

返回单个转账

<?php
$transfer = $pagarme->transfers()->get([
    'id' => 'ID_DA_TRANSFERÊNCIA'
]);

取消转账

<?php
$canceledTransfer = $pagarme->transfers()->cancel([
    'id' => 'ID_DA_TRANSFERÊNCIA'
]);

预付款

要了解预付款是什么,您必须访问此链接

创建预付款

<?php
$anticipation = $pagarme->bulkAnticipations()->create([
    'recipient_id' => 're_cjeptpdyg03u3cb6elj68p5ej',
    'payment_date' => '1536883200000',
    'requested_amount' => '300000',
    'timeframe' => 'start'
]);

获取预付款限额

<?php
$anticipationLimits = $pagarme->bulkAnticipations()->getLimits([
    'recipient_id' => 'ID_DO_RECEBEDOR',
    'payment_date' => '1536883200000',
    'timeframe' => 'start'
]);

取消待处理的预付款

<?php
$canceledAnticipation = $pagarme->bulkAnticipations()->cancel([
    'recipient_id' => 'ID_DO_RECEBEDOR',
    'bulk_anticipation_id' => 'ID_DA_ANTECIPAÇÃO',
]);

返回预付款

<?php
$anticipations = $pagarme->bulkAnticipations()->getList([
    'recipient_id' => 'ID_DO_RECEBEDOR'
]);

如果需要,您可以在搜索时应用筛选器,例如,通过预付款金额进行筛选

<?php
$anticipations = $pagarme->bulkAnticipations()->getList([
    'recipient_id' => 'ID_DO_RECEBEDOR',
    'amount' => 'VALOR_ANTECIPADO'
]);

银行账户

银行账户标识了未来支付款项的汇款去向。

创建银行账户

<?php
$bankAccount = $pagarme->bankAccounts()->create([
    'bank_code' => '341',
    'agencia' => '0932',
    'agencia_dv' => '5',
    'conta' => '58054',
    'conta_dv' => '1',
    'document_number' => '26268738888',
    'legal_name' => 'API BANK ACCOUNT'
]);

返回单个银行账户

<?php
$bankAccount = $pagarme->bankAccounts()->get([
    'id' => 'ID_DA_CONTA_BANCÁRIA'
]);

返回银行账户列表

<?php
$bankAccounts = $pagarme->bankAccounts()->getList();

如果您想,您可以在搜索银行账户时应用筛选器,例如,通过银行代码进行筛选

<?php
$bankAccounts = $pagarme->bankAccounts()->getList([
    'bank_code' => '341'
]);

收款人

为了在多个实体之间分割交易,为每个实体都需要有一个收款人。收款人包含将款项汇入的银行账户信息,并具有其他信息,以了解其可以提前多少,或何时自动从您的账户中提取款项。

创建收款人

<?php
$recipient = $pagarme->recipients()->create([
    'anticipatable_volume_percentage' => '85', 
    'automatic_anticipation_enabled' => 'true', 
    'bank_account_id' => '17899179', 
    'transfer_day' => '5', 
    'transfer_enabled' => 'true', 
    'transfer_interval' => 'weekly'
]);

返回收款人

<?php
$recipients = $pagarme->recipients()->getList();

如果需要,您可以在搜索时应用筛选器。例如,如果您想返回所有已启用转账功能的收款人,可以使用此代码

<?php
$transferEnabledRecipients = $pagarme->recipients()->getList([
    'transfer_enabled' => true
]);

返回单个收款人

<?php
$recipient = $pagarme->recipients()->get([
    'id' => 'ID_DO_RECEBEDOR'
]);

更新收款人

<?php
$updatedRecipient = $pagarme->recipients()->update([
    'id' => 'ID_DO_RECEBEDOR',
    'anticipatable_volume_percentage' => 80,
    'transfer_day' => 4
]);

收款人余额

<?php
$recipientBalance = $pagarme->recipients()->getBalance([
    'recipient_id' => 'ID_DO_RECEBEDOR',
]);

收款人余额操作

<?php
$recipientBalanceOperations = $pagarme->recipients()->listBalanceOperation([
    'recipient_id' => 'ID_DO_RECEBEDOR'
]);

收款人特定余额操作

<?php
$recipientBalanceOperation = $pagarme->recipients()->getBalanceOperation([
    'recipient_id' => 'ID_DO_RECEBEDOR',
    'balance_operation_id' => 'ID_DA_OPERAÇÃO'
]);

客户

客户代表您的商店或业务中的用户。此对象包含有关他们的信息,如姓名、电子邮件和电话,以及其他字段。

创建客户

<?php
$customer = $pagarme->customers()->create([
    'external_id' => '#123456789',
    'name' => 'João das Neves',
    'type' => 'individual',
    'country' => 'br',
    'email' => '[email protected]',
    'documents' => [
      [
        'type' => 'cpf',
        'number' => '11111111111'
      ]
    ],
    'phone_numbers' => [
      '+5511999999999',
      '+5511888888888'
    ],
    'birthday' => '1985-01-01'
]);

返回客户

<?php
$customers = $pagarme->customers()->getList();

返回单个客户

<?php
$customer = $pagarme->customers()->get([
    'id' => 'ID_DO_CLIENTE'
]);

支付链接

创建支付链接

<?php
$paymentLink = $pagarme->paymentLinks()->create([
    'amount' => 10000,
    'items' => [
        [
            'id' => '1',
            'title' => "Fighter's Sword",
            'unit_price' => 4000,
            'quantity' => 1,
            'tangible' => true,
            'category' => 'weapon',
            'venue' => 'A Link To The Past',
            'date' => '1991-11-21'
        ],
        [
            'id' => '2',
            'title' => 'Kokiri Sword',
            'unit_price' => 6000,
            'quantity' => 1,
            'tangible' => true,
            'category' => 'weapon',
            'venue' => "Majora's Mask",
            'date' => '2000-04-27'
        ],
    ],
    'payment_config' => [
        'boleto' => [
            'enabled' => true,
            'expires_in' => 20
        ],
        'credit_card' => [
            'enabled' => true,
            'free_installments' => 4,
            'interest_rate' => 25,
            'max_installments' => 12
        ],
        'default_payment_method' => 'boleto'
    ],
    'max_orders' => 1,
    'expires_in' => 60
]);

返回支付链接

<?php
$paymentLinks = $pagarme->paymentLinks()->getList();

返回单个支付链接

<?php
$paymentLink = $pagarme->paymentLinks()->get([
    'id' => 'ID_DO_LINK_DE_PAGAMENTO'
]);

取消支付链接

<?php
$canceledPaymentLink = $pagarme->paymentLinks()->cancel([
    'id' => 'ID_DO_LINK_DE_PAGAMENTO'
]);

高级搜索(Elasticsearch)

执行搜索

<?php
$search = $pagarme->search()->get([                
    "type" => "transaction",
    "query" => [
        "query" => [
            "terms" => [
                "items.id" => [8, 9] // Busca transações com itens de ID 8 e 9
            ]
        ]
    ]
]);