astrotechlabs/asaas-sdk

Asaas 支付集成包

1.1.0 2023-12-24 17:32 UTC

This package is auto-updated.

Last update: 2024-09-24 19:08:59 UTC


README

这是一个包含Asaas V3 API抽象的仓库,简化了PIX Copia e Cola的创建以及其他提供的服务的开发。

安装

最推荐的方式是通过composer安装。

要安装,只需执行以下命令:

$ php composer.phar require astrotechlabs/asaas-sdk

或者添加以下行到你的composer.json文件的require部分:

"astrotechlabs/asaas-sdk": "^1.0"

如何使用?

通过PIX创建存款

以下代码可以创建一个通过PIX的收费,返回了Chave Pix Cópia/ColacopyPasteUrl)的数据,以及一个已返回的作为前端应用的QRCode(qrCode)。

use AstrotechLabs\AsaasSdk\AssasGateway;
use AstrotechLabs\AsaasSdk\AssasGatewayParams;
use AstrotechLabs\AsaasSdk\Pix\CreatePixCharge\Dto\PixData;
use AstrotechLabs\AsaasSdk\Pix\Enum\BillingTypes;

$asaasGateway = new AssasGateway(new AssasGatewayParams(
    apiKey: 'xxxxxxxxxx',
    // isSandBox: true (opcional)
));

$pixChargeResponse = $asaasGateway->createPixCharge(new PixData(
    customer: new CustomerData(
        name: 'Joãozinho Barbosa',
        phone: '999999999',
        cpfCnpj: '01234567890'
    ),
    billingType: BillingTypes::PIX,
    value: 100.00,
    dueDate: "2023-12-20" // Deve ser informada uma data futura
));

print_r($pixChargeResponse);

输出

[
    'gatewayId': 'pay_kp6gqaovguxqr1od',
    'paymentUrl': 'https://sandbox.asaas.com/i/kp6gqaovguxqr1od',
    'copyPasteUrl': '00020101021226820014br.gov.bcb.pix2560qrpix-h.bradesco.com.br/xxxxxxxxx-xxxx......',
    'details' => [
        'object' => 'payment'
        'id' => 'pay_kp6gqaovguxqr1od'
        'dateCreated' => '2023-12-16'
        'customer' => 'cus_000005797885'
        'paymentLink' => 'https://sandbox.asaas.com/i/xxxxxxxxxxx',
        ..............
    ],
    'qrCode' => 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAYsAAA......'
]

Webhook

当通过PIX确认交易时,以下JSON格式的payload将通过Asaas后台办公室配置的URL发送到你的应用。

{
   "event":"PAYMENT_RECEIVED",
   "payment":{
      "object":"payment",
      "id":"pay_080225913252",
      "dateCreated":"2021-01-01",
      "customer":"cus_G7Dvo4iphUNk",
      "subscription":"sub_VXJBYgP2u0eO",  
      "installment":"2765d086-c7c5-5cca-898a-4262d212587c",
      "paymentLink":"123517639363",
      "dueDate":"2021-01-01",
      "originalDueDate":"2021-01-01",
      "value":100,
      "netValue":94.51,
      "originalValue":null,
      "interestValue":null,
      "nossoNumero": null,
      "description":"Pedido 056984",
      "externalReference":"056984",
      "billingType":"CREDIT_CARD",
      "status":"RECEIVED",
      "pixTransaction":null,
      "confirmedDate":"2021-01-01",
      "paymentDate":"2021-01-01",
      "clientPaymentDate":"2021-01-01",
      "installmentNumber": null,
      "creditDate":"2021-02-01",
      "custody": null,
      "estimatedCreditDate":"2021-02-01",
      "invoiceUrl":"https://www.asaas.com/i/080225913252",
      "bankSlipUrl":null,
      "transactionReceiptUrl":"https://www.asaas.com/comprovantes/4937311816045162",
      "invoiceNumber":"00005101",
      "deleted":false,
      "anticipated":false,
      "anticipable":false,
      "lastInvoiceViewedDate":"2021-01-01 12:54:56",
      "lastBankSlipViewedDate":null,
      "postalService":false,
      "creditCard":{
         "creditCardNumber":"8829",
         "creditCardBrand":"MASTERCARD",
         "creditCardToken":"a75a1d98-c52d-4a6b-a413-71e00b193c99"
      },
      "discount":{
         "value":0.00,
         "dueDateLimitDays":0,
         "limitedDate": null,
         "type":"FIXED"
      },
      "fine":{
         "value":0.00,
         "type":"FIXED"
      },
      "interest":{
         "value":0.00,
         "type":"PERCENTAGE"
      },
      "split":[
         {
            "walletId":"48548710-9baa-4ec1-a11f-9010193527c6",
            "fixedValue":20,
            "status":"PENDING",
            "refusalReason": null
         },
         {
            "walletId":"0b763922-aa88-4cbe-a567-e3fe8511fa06",
            "percentualValue":10,
            "status":"PENDING",
            "refusalReason": null
         }
      ],
      "chargeback": {
          "status": "REQUESTED",
          "reason": "PROCESS_ERROR"
      },
      "refunds": null
   }
}

有关webhook的更多详细信息,请参阅此处文档。

创建转账

以下代码可以创建通过PIX的转账。

use AstrotechLabs\AsaasSdk\AssasGateway;
use AstrotechLabs\AsaasSdk\AssasGatewayParams;
use AstrotechLabs\AsaasSdk\Transfer\CreateTransferCharge\Dto\TransferData;
use AstrotechLabs\AsaasSdk\Transfer\Enum\PixKeyTypes;

$asaasGateway = new AssasGateway(new AssasGatewayParams(
    apiKey: $_ENV['ASAAS_API_KEY'],
    isSandBox: false
));

$transferChargeResponse = $asaasGateway->createTransferCharge(new TransferData(
    value: 1,
    pixAddressKey: 'xxxxxxxx-xxxxx-xxxxx-xxxx',
    pixAddressKeyType: PixKeyTypes::RANDOM_KEY
));

print_r($transferChargeResponse);

输出

[
    'gatewayId' => 'fb408225-8d98-4afe-b193-894cbdf1db55'
    'status' => 'PENDING'
    'fee' => 0
    'value' => 1
    'authorized' => false
    'details' => [
        'object' => 'transfer'
        'id' => 'fb408225-8d98-4afe-b193-894cbdf1db55'
        'value' => 1.0,
        ........
    ]
]

Webhook

当通过转账确认交易时,以下JSON格式的payload将通过Asaas后台办公室配置的URL发送到你的应用。

{
    "event": "TRANSFER_CREATED",
    "transfer": {
        "object": "transfer",
        "id": "777eb7c8-b1a2-4356-8fd8-a1b0644b5282",
        "dateCreated": "2019-05-02",
        "status": "PENDING",
        "effectiveDate": null,
        "endToEndIdentifier": null,
        "type": "BANK_ACCOUNT",
        "value": 1000,
        "netValue": 1000,
        "transferFee": 0,
        "scheduleDate": "2019-05-02",
        "authorized": true,
        "failReason": null,
        "transactionReceiptUrl": null,
        "bankAccount": {
            "bank": {
                "ispb": "00000000",
                "code": "001",
                "name": "Banco do Brasil"
            },
            "accountName": "Conta Banco do Brasil",
            "ownerName": "Marcelo Almeida",
            "cpfCnpj": "***.143.689-**",
            "agency": "1263",
            "agencyDigit": "1",
            "account": "26544",
            "accountDigit": "1",
            "pixAddressKey": null
        },
        "operationType": "TED",
        "description": null
    }
}

有关webhook的更多详细信息,请参阅此处文档。

贡献

欢迎Pull Request。对于重要的更改,首先请创建一个issue来讨论你想要更改的内容。

确保根据需要更新测试。

许可

此包在MIT许可证下发布。有关详细信息,请参阅LICENSE包。