igrejanet/gerencianet

用于在 Laravel 框架中使用 GerenciaNet 的包装器

1.4.0 2018-03-28 16:52 UTC

This package is auto-updated.

Last update: 2024-09-13 22:50:08 UTC


README

Build Status

GerênciaNet 包是一个 wrapper,用于简化与 GerênciaNet 支付网关的集成。

安装

通过 composer 安装此包

$ composer require igrejanet/gerencianet

PS.: 该包已包含 自动发现包,可用于 Laravel。类 Gerencianet 将自动以 单例 形式实例化。

使用

要使用此包,我们将过程分为两部分:创建收款和检查收款

创建收款

通常,这一步在显示付款表单之前执行。

<?php

use Gerencianet\Gerencianet;
use Igrejanet\GerenciaNet\ChargeCreator;
use Igrejanet\GerenciaNet\Products;

$gerencianet = new Gerencianet([
    'client_id'     => 'meu_client_id',
    'client_secret' => 'meu_client_secret',
    'sandbox'       => true // Ou false, caso em produção
]);

$products = new Products();
$products->add('Carrinho Hot Wheels', 1500, 3);
$products->add('Bolacha Maizena', 185, 2);

$charge = new ChargeCreator($gerencianet);
$charge->setInvoiceId(1); // Gerado pelo seu sistema
$charge->setNotificationUrl('http://minhaloja.com');
$charge->setProducts($products);

// Guarde bem este valor, vamos precisar dele na segunda etapa
$charge_id = $charge->generateCharge();

检查流程

<?php

use Gerencianet\Gerencianet;
use Igrejanet\GerenciaNet\ChargeSender;
use Igrejanet\GerenciaNet\Customer\BillingAddress;
use Igrejanet\GerenciaNet\Customer\Customer;
use Igrejanet\GerenciaNet\Methods\BankingBillet;
use Igrejanet\GerenciaNet\Methods\CreditCard;
use Igrejanet\GerenciaNet\Methods\DiscountManager;

$gerencianet = new Gerencianet([
    'client_id'     => 'meu_client_id',
    'client_secret' => 'meu_client_secret',
    'sandbox'       => true // Ou false, caso em produção
]);

$customer = new Customer(
    'João Lopes',
    '32214653783',
    'joao@company.com',
    '3895281420',
    '1990-01-01'
);

$billingAddress = new BillingAddress(
    'Rua do Cachorro Toco',
    12,
    'São Pedo da Garça',
    '39400000',
    'Montes Claros',
    'MG'
);

// O desconto pode ser aplicado em centavos ou porcentagem.
//No caso de porcentagem, multiplicar o valor por 100
$discount = new DiscountManager(1000);

// Se o cliente pagar com boleto...
if($isBoleto) {
    $method = new BankingBillet();
    $method->setExpirationDate(7); // Em quantos dias o boleto deve vencer?
    $method->setCustomer($customer);
    
} else if($isCartao) {
    // Estas variáveis são repassadas pela tela de checkout
    $method = new CreditCard($paymentToken, $installments);
    
    $method->setCustomer($customer);
    
    // O End. de cobrança deve ser aplicado em pagamentos via CC
    $method->setBillingAddress($billingAddress);
    
    // O desconto pode ser aplicado em ambos os métodos
    $method->setDiscount($discount);
}

$chargeSender = new ChargeSender($gerencianet);

// A variável charge ID contém o valor definido na geração da cobrança
// Sem o charge_id, a cobrança não será enviada
// A variável response recebe um array contendo todos os dados da transação
$response = $chargeSender->sendPaymentRequest($method, $charge_id);

通知

GerenciaNet 系统在交易状态更改时向您的系统发送通知。要接收这些通知和新的收款状态,请执行以下操作:

<?php

use Gerencianet\Gerencianet;
use Igrejanet\GerenciaNet\Notifications;

$gerencianet = new Gerencianet([
    'client_id'     => 'meu_client_id',
    'client_secret' => 'meu_client_secret',
    'sandbox'       => true // Ou false, caso em produção
]);

$notifications = new Notifications($gerencianet);

// O token é repassado via POST pelo sistema de notificações do GerenciaNet
$status = $notifications->receiveNotification($token);

测试

存在两个测试需要分别执行,因为在执行它们之前,您必须在 phpunit.xml 文件中设置您的 client_id 和 client_secret。只有这样,您才能测试创建和向 GerênciaNet 的 sandbox 发送收款。

  • ChargeCreatorTest.php
  • ChargeSenderTest.php
  • IgrejanetTest.php

在执行 PHPUnit 时,仅执行不使用 API 访问的测试。

要测试上述文件,必须分别测试它们。

未来

目前,该包仅接受通过支票和信用卡进行的支付。计划扩展以支持 GerênciaNet 提供的其他支付方式。欢迎贡献。