jhernandes/ipag-sdk-php

PHP 1.0 版本的 iPag 集成包

v1.1.26 2024-09-17 14:12 UTC

README

警告 - SDK 已弃用

此 SDK 已弃用,将不再维护。要在您的商店中使用 iPag,请通过 iPag 的官方项目使用:[a href="https://github.com/ipagdevs/ipag-sdk-php" rel="nofollow noindex noopener external ugc">ipag-sdk-php。

您无需将当前项目迁移到新 SDK,因为它仍然可供使用。但如果您要开始一个新项目,请使用官方 SDK。

SDK 状态

Scrutinizer Code Quality Maintainability

Build Status

索引

依赖项

require

  • [PHP >= 5.6]

require-dev

  • [phpunit/phpunit]
  • [codacy/coverage]

安装

在您的 shell 中执行

composer require jhernandes/ipag-sdk-php

认证

通过基本认证

require 'vendor/autoload.php';

use Ipag\Ipag;
use Ipag\Classes\Authentication;
use Ipag\Classes\Endpoint;

$ipag = new Ipag(new Authentication('my_id_ipag', 'my_ipag_key'), Endpoint::SANDBOX);

客户端

客户端数据

$customer = $ipag->customer()
    ->setName('Fulano da Silva')
    ->setTaxpayerId('799.993.388-01')
    ->setPhone('11', '98888-3333')
    ->setEmail('fulanodasilva@gmail.com')
    ->setBirthdate('1989-03-28')
    ->setAddress($ipag->address()
        ->setStreet('Rua Júlio Gonzalez')
        ->setNumber('1000')
        ->setNeighborhood('Barra Funda')
        ->setCity('São Paulo')
        ->setState('SP')
        ->setZipCode('01156-060')
);

信用卡/借记卡

信用卡/借记卡数据

$creditCard = $ipag->creditCard()
    ->setNumber('4066553613548107')
    ->setHolder('FULANO')
    ->setExpiryMonth('10')
    ->setExpiryYear('2025')
    ->setCvc('123')
    ->setSave(true); //True para gerar o token do cartão (one-click-buy)

购物车

添加产品

// ...

$products = [
    // Nome do Produto, Valor Unitário, Quantidade, SKU (Código do Produto)
    ['Produto 1', 5.00, 1, 'ABDC1'],
    ['Produto 2', 3.50, 2, 'ABDC2'],
    ['Produto 3', 5.50, 1, 'ABDC3'],
    ['Produto 4', 8.50, 5, 'ABDC4']
];

// Deve-se usar o `splat operator`
$cart = $ipag->cart(...$products);

// ...

交易(支付)

信用卡交易

$transaction = $ipag->transaction();

$transaction->getOrder()
    ->setOrderId($orderId)
    ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
    ->setAmount(10.00)
    ->setInstallments(1)
    ->setPayment($ipag->payment()
        ->setMethod(Method::VISA)
        ->setCreditCard($creditCard)
    )->setCustomer($customer)
);

$response = $transaction->execute();

信用卡令牌交易

$transaction = $ipag->transaction();

$transaction->getOrder()
    ->setOrderId($orderId)
    ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
    ->setAmount(10.00)
    ->setInstallments(1)
    ->setPayment($ipag->payment()
        ->setMethod(Method::VISA)
        ->setCreditCard($ipag->creditCard()
            ->setToken('ABDC-ABCD-ABCD-ABDC')
        )
    )->setCustomer($customer)
);

$response = $transaction->execute();

支票交易

$transaction = $ipag->transaction();

$transaction->getOrder()
    ->setOrderId($orderId)
    ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
    ->setAmount(10.00)
    ->setInstallments(1)
    ->setExpiry('10/10/2017')
    ->setPayment($ipag->payment()
        ->setMethod(Method::BANKSLIP_ZOOP)
    )->setCustomer($customer)
);

$response = $transaction->execute();

Pix 交易

$transaction = $ipag->transaction();

$transaction->getOrder()
    ->setOrderId($orderId)
    ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
    ->setAmount(10.00)
    ->setInstallments(1)
    ->setPayment($ipag->payment()
        ->setMethod(Method::PIX)
        ->setPixExpiresIn(60)
    )->setCustomer($customer)
);

$response = $transaction->execute();

// PIX LINK DE PAGAMENTO (Usando o Checkout do iPag para finalizar)
$linkDePagamento = $response->pix->link;

// PIX Copia e Cola | QRCode (Utilizar a string retornada ou gerar um QrCode)
$qrCodeString = $response->pix->qrCode;

查询

$response = $ipag->transaction()->setTid('123456789')->consult();

捕获

$response = $ipag->transaction()->setTid('123456789')->capture();

部分捕获

$response = $ipag->transaction()->setTid('123456789')->setAmount(1.00)->capture();

取消

$response = $ipag->transaction()->setTid('123456789')->cancel();

部分取消

$response = $ipag->transaction()->setTid('123456789')->setAmount(1.00)->cancel();

订阅

创建订阅

$transaction = $ipag->transaction();

$transaction->getOrder()
    ->setOrderId($orderId)
    ->setCallbackUrl(getenv('CALLBACK_URL'))
    ->setAmount(10.00)
    ->setInstallments(1)
    ->setPayment($ipag->payment()
        ->setMethod(Method::VISA)
        ->setCreditCard($creditCard)
    )->setCustomer($customer)
)->setSubscription($ipag->subscription()
    ->setProfileId('1000000')
    ->setFrequency(1)
    ->setInterval('month')
    ->setStart('10/10/2018')
);

$response = $transaction->execute();

完整交易示例

信用卡交易示例

<?php

require 'vendor/autoload.php';

use Ipag\Ipag;
use Ipag\Classes\Authentication;
use Ipag\Classes\Endpoint;
use Ipag\Classes\Enum\PaymentStatus;

try {
    $ipag = new Ipag(new Authentication('my_id_ipag', 'my_ipag_key'), Endpoint::SANDBOX);

    $customer = $ipag->customer()
        ->setName('Fulano da Silva')
        ->setTaxpayerId('799.993.388-01')
        ->setPhone('11', '98888-3333')
        ->setEmail('fulanodasilva@gmail.com')
        ->setAddress($ipag->address()
            ->setStreet('Rua Júlio Gonzalez')
            ->setNumber('1000')
            ->setNeighborhood('Barra Funda')
            ->setCity('São Paulo')
            ->setState('SP')
            ->setZipCode('01156-060')
    );
    $products = [
        ['Produto 1', 5.00, 1, 'ABDC1'],
        ['Produto 2', 2.50, 2, 'ABDC2']
    ];
    $cart = $ipag->cart(...$products);

    $creditCard = $ipag->creditCard()
        ->setNumber('4066553613548107')
        ->setHolder('FULANO')
        ->setExpiryMonth('10')
        ->setExpiryYear('2025')
        ->setCvc('123');

    $transaction = $ipag->transaction();

    $transaction->getOrder()
        ->setOrderId($orderId)
        ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
        ->setAmount(10.00)
        ->setInstallments(1)
        ->setPayment($ipag->payment()
            ->setMethod(Method::VISA)
            ->setCreditCard($creditCard)
        )
        ->setCustomer($customer)
        ->setCart($cart);

    $response = $transaction->execute();

    //Retornou algum erro?
    if (!empty($response->error)) {
        throw new \Exception($response->errorMessage);
    }

    //Pagamento Aprovado (5) ou Aprovado e Capturado(8) ?
    if (in_array($response->payment->status, [
        PaymentStatus::PRE_AUTHORIZED, PaymentStatus::CAPTURED
    ]) {
        //Faz alguma coisa...
        return $response;
    }
} catch(\Exception $e) {
    print_r($e->__toString());
}

带拆分规则的交易示例

信用卡交易示例

<?php

require 'vendor/autoload.php';

use Ipag\Ipag;
use Ipag\Classes\Authentication;
use Ipag\Classes\Endpoint;
use Ipag\Classes\Enum\PaymentStatus;

try {
    $ipag = new Ipag(new Authentication('my_id_ipag', 'my_ipag_key'), Endpoint::SANDBOX);

    $customer = $ipag->customer()
        ->setName('Fulano da Silva')
        ->setTaxpayerId('799.993.388-01')
        ->setPhone('11', '98888-3333')
        ->setEmail('fulanodasilva@gmail.com')
        ->setAddress($ipag->address()
            ->setStreet('Rua Júlio Gonzalez')
            ->setNumber('1000')
            ->setNeighborhood('Barra Funda')
            ->setCity('São Paulo')
            ->setState('SP')
            ->setZipCode('01156-060')
    );

    $products = [
        ['Produto 1', 5.00, 1, 'ABDC1'],
        ['Produto 2', 2.50, 2, 'ABDC2']
    ];
    $cart = $ipag->cart(...$products);

    $creditCard = $ipag->creditCard()
        ->setNumber('4066553613548107')
        ->setHolder('FULANO')
        ->setExpiryMonth('10')
        ->setExpiryYear('2025')
        ->setCvc('123');

    $payment = $ipag->payment()
            ->setMethod(Method::VISA)
            ->setCreditCard($creditCard);

    //Regra de Split 1 (com porcentagem %)
    $payment->addSplitRule($ipag->splitRule()
        ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
        ->setPercentage(15)
        ->setLiable(1)
    );

    //Regra de Split 2 (com valor absoluto R$)
    $payment->addSplitRule($ipag->splitRule()
        ->setSellerId('c66fabf44786459e81e3c65e339a4fc9')
        ->setAmount(5.00)
        ->setLiable(1)
    );

    $transaction = $ipag->transaction();
    $transaction->getOrder()
        ->setOrderId($orderId)
        ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
        ->setAmount(10.00)
        ->setInstallments(1)
        ->setPayment($payment)
        ->setCustomer($customer)
        ->setCart($cart);

    $response = $transaction->execute();

    //Retornou algum erro?
    if (!empty($response->error)) {
        throw new \Exception($response->errorMessage);
    }

    //Pagamento Aprovado (5) ou Aprovado e Capturado(8) ?
    if (in_array($response->payment->status, [
        PaymentStatus::PRE_AUTHORIZED, PaymentStatus::CAPTURED
    ]) {
        //Faz alguma coisa...
        return $response;
    }
} catch(\Exception $e) {
    print_r($e->__toString());
}

支票交易示例

<?php

require 'vendor/autoload.php';

use Ipag\Ipag;
use Ipag\Classes\Authentication;
use Ipag\Classes\Endpoint;
use Ipag\Classes\Enum\PaymentStatus;

try {
    $ipag = new Ipag(new Authentication('my_id_ipag', 'my_ipag_key'), Endpoint::SANDBOX);

    $customer = $ipag->customer()
        ->setName('Fulano da Silva')
        ->setTaxpayerId('799.993.388-01')
        ->setPhone('11', '98888-3333')
        ->setEmail('fulanodasilva@gmail.com')
        ->setAddress($ipag->address()
            ->setStreet('Rua Júlio Gonzalez')
            ->setNumber('1000')
            ->setNeighborhood('Barra Funda')
            ->setCity('São Paulo')
            ->setState('SP')
            ->setZipCode('01156-060')
    );
    $products = [
        ['Produto 1', 5.00, 1, 'ABDC1'],
        ['Produto 2', 2.50, 2, 'ABDC2']
    ];
    $cart = $ipag->cart(...$products);

    $transaction = $ipag->transaction();

    $transaction->getOrder()
        ->setOrderId($orderId)
        ->setCallbackUrl('https://minha_loja.com.br/ipag/callback')
        ->setAmount(10.00)
        ->setInstallments(1)
        ->setExpiry('10/07/2021')
        ->setPayment($ipag->payment()
            ->setMethod(Method::BANKSLIP_ZOOP)
        )->setCustomer($customer)
    );

    $response = $transaction->execute();

    //Retornou algum erro?
    if (!empty($response->error)) {
        throw new \Exception($response->errorMessage);
    }

    //Pagamento de Boleto Criado (1) ou Boleto Impresso (2) ?
    if (in_array($response->payment->status, [
        PaymentStatus::CREATED,
        PaymentStatus::PRINTED_BOLETO
    ])) {
        // Boleto Link
        //echo $response->urlAuthentication;

        return $response;
    }
} catch(\Exception $e) {
    print_r($e->__toString());
}

回调页面示例

<?php

require_once 'vendor/autoload.php';

use Ipag\Classes\Services\CallbackService;
use Ipag\Classes\Enum\PaymentStatus;

$postContent = file_get_contents('php://input');

$callbackService = new CallbackService();

// $response conterá os dados de retorno do iPag
// $postContent deverá conter o XML enviado pelo iPag
$response = $callbackService->getResponse($postContent);

// Verificar se o retorno tem erro
if (!empty($response->error)) {
    echo "Contem erro! {$response->error} - {$response->errorMessage}";
}

// Verificar se a transação foi aprovada e capturada:
if ($response->payment->status == PaymentStatus::CAPTURED) {
    echo 'Transação Aprovada e Capturada';
    // Atualize minha base de dados ...
}

响应

交易响应结构

  • transaction
    • id
    • tid
    • authId
    • amount
    • acquirer
    • acquirerMessage
    • urlAuthentication
    • urlCallback
    • createdAt
    • creditCard
      • holder
      • number
      • expiry
      • brand
      • token
    • subscription
      • id
      • profileId
    • payment
      • status
      • message
    • order
      • orderId
    • customer
      • name
      • email
      • phone
      • cpfCnpj
      • address
        • street
        • number
        • district
        • complement
        • city
        • state
        • zipCode
    • antifraud
      • id
      • score
      • status
      • message
    • splitRules
      • [0]
        • rule
        • seller_id
        • ipag_id
        • amount
        • amount
        • percentage
        • liable
        • charge_processing_fee
    • error
    • errorMessage
    • history
      • [0]
        • amount
        • operationType
        • status
        • responseCode
        • responseMessage
        • authorizationCode
        • authorizationId
        • authorizationNsu
        • createdAt

测试

单元测试是在 iPag 沙盒上进行的,配置文件(phpunit.xml)已预先填充了有限的沙盒访问权限。

需要安装 PHPUnit 才能进行测试。

许可

The MIT License

文档

官方文档

疑问 & 建议

如果您对 SDK 有疑问或建议,请创建一个新的问题