tmazza/wirecard-subscriptions-sdk

此包的最新版本(v0.1)没有可用的许可信息。

简化使用 Wirecard Subscriptions API 的软件开发套件。

v0.1 2020-07-21 02:14 UTC

This package is auto-updated.

Last update: 2024-09-19 22:39:17 UTC


README

SDK PHP 用于简化使用 Wirecard 订阅 API

使用方法

示例

<?php
use tmazza\WirecardSubscriptions\WirecardApi;

$wirecardApi = new WirecardApi();


# Lista de planos
$plans = $wirecardApi->plans->all();

foreach($plans as $plan) {
    // $plan->code
}

# Criação de usuário
$wirecardApi->customers->create([ ... ]); 

# Criação de assinatura
$wirecardApi->subscriptions->create([ ... ]); 

# Consulta de assinatura
$wirecardApi->subscriptions->get('code');

安装

composer require tmazza/wirecard-assinaturas-sdk-php

凭证

请设置环境变量 WIRECARD_SUBSCRIPTIONS_ENVWIRECARD_SUBSCRIPTIONS_TOKENWIRECARD_SUBSCRIPTIONS_KEY,分别指定环境、令牌和 Wirecard 集成密钥。默认使用 sandbox 环境。

或者,可以在创建 API 类时设置环境和凭证,传递以下参数:new WirecardApi('sandbox', 'token', 'key')。这些参数将优先于环境变量。

API

所有资源都包含 get()all()create()update() 方法,以及配置 API 中所有可用参数的特定方法。

错误处理

try {
  
  $wirecardApi = new WirecardApi();
  $wirecardApi->plans->create(['...']);

} catch (ValidationException $e) {

  // status 400 - reporte de erro da API Wirecard
  $e->errors(); 
  $e->alerts();
  $e->firstError();
  $e->firstAlert();

} catch (GuzzleHttp\Exception\ClientException $e) {
  
  // status 401 a 499  

} catch (GuzzleHttp\Exception\ServerException $e) {
  
  // status 500 a 599
  
} 

资源

订阅

支付

  • 账单

    • 列出订阅的账单
    • 查询账单详情
  • 支付

    • 列出账单的支付
    • 查询订阅支付的详情
  • 优惠券

    • 创建优惠券
    • 将优惠券关联到现有订阅
    • 将优惠券关联到新订阅
    • 查询优惠券
    • 列出所有优惠券
    • 激活和停用优惠券
    • 从订阅中删除优惠券
  • 重试

    • 账单支付重试
    • 为账单生成新的发票
    • 创建自动重试规则

通知

  • 通知偏好设置
    • 创建通知偏好设置(webhook)

计划

创建计划

<?php
$plan = $wirecardApi->plans->create([
  "code" => "plan101",
  "name" => "Plano Especial",
  "description" => "Descrição",
  "amount" => 990,
  "setup_fee" => 500,
  "max_qty" => 1,
  "interval" => [
    "length" => 1,
    "unit" => "MONTH"
  ],
  "billing_cycles" => 12,
  "trial" => [
    "days" => 30,
    "enabled" => true,
    "hold_setup_fee" => true
  ],
  "payment_method" => "CREDIT_CARD"
]);

echo $plan->name; // Plano Especial

列出计划

<?php
$plans = $wirecardApi->plans->all();

foreach($plans as $plan) {
    echo $plan->name; // Plano Especial
}

查询计划

<?php
$plan = $wirecardApi->plans->get('plan101');
echo $plan->name; // Plano Especial

激活计划

<?php
$plan = $wirecardApi->plans->activate('plan101');
echo $plan->status; // ACTIVE

停用计划

<?php
$plan = $wirecardApi->plans->inactivate('plan101');
echo $plan->status; // INACTIVE

修改计划

<?php
$plan = $wirecardApi->plans->update([
    'name' => 'Plano Especial Atualizado',
]);
echo $plan->name; // Plano Especial Atualizado

订阅者

创建订阅者

<?php

// Criar assinante
$customer = $wirecardApi->customers->create([
  "code" => "cliente01",
  "email" => "nome@exemplo.com.br",
  "fullname" => "Nome Sobrenome",
  "cpf" => "22222222222",
  "phone_area_code" => "11",
  "phone_number" => "934343434",
  "birthdate_day" => "26",
  "birthdate_month" => "04",
  "birthdate_year" => "1980",
  "address" => [
    "street" => "Rua Nome da Rua",
    "number" => "100",
    "complement" => "Casa",
    "district" => "Nome do Bairro",
    "city" => "São Paulo",
    "state" => "SP",
    "country" => "BRA",
    "zipcode" => "05015010"
  ]
]);

// Cadastrar o cartão do assinante
$wirecardApi->customers->setCard(
    $customer->code,
    [
      "holder_name" => "Nome Completo",
      "number" => "4111111111111111",
      "expiration_month" => "06",
      "expiration_year" => "22"
    ]
);

// new_vault pode ser habilitado enableNewVault()
$customer = $wirecardApi->customers
    ->enableNewVault()
    ->create([/*...*/])

// customer e billing_info criados juntos
$customer = $wirecardApi->customers->create([
  "code" => "cliente02",
  "email" => "nome@exemplo.com.br",
  "fullname" => "Nome Sobrenome",
  "cpf" => "22222222222",
  "phone_area_code" => "11",
  "phone_number" => "934343434",
  "birthdate_day" => "26",
  "birthdate_month" => "04",
  "birthdate_year" => "1980",
  "address" => [
    "street" => "Rua Nome da Rua",
    "number" => "100",
    "complement" => "Casa",
    "district" => "Nome do Bairro",
    "city" => "São Paulo",
    "state" => "SP",
    "country" => "BRA",
    "zipcode" => "05015010"
  ],
  "billing_info" => [
    "credit_card" => [
      "holder_name" => "Nome Completo",
      "number" => "4111111111111111",
      "expiration_month" => "06",
      "expiration_year" => "22"
    ]
  ]
]);

echo $customer->code; // cliente02

列出订阅者

<?php
$customers = $wirecardApi->customers->all();

foreach($customers as $customer) {
  echo $customer->code; // cliente01
}

查询订阅者

<?php
$customer = $wirecardApi->customers->get('client01');
echo $customer->email; // nome@exemplo.com.br

修改订阅者

<?php
$customer = $wirecardApi->customers->update([
  'name' => 'Novo nome',
]);
echo $customer->name; // Novo nome 

更新订阅者卡

<?php
$customer = $wirecardApi->customers->setCard([
  'holder_name' => 'Nome Completo',
  'number' => '4222222222222222',
  'expiration_month' => '06',
  'expiration_year' => '22'
]);
echo $customer->billing_info->credit_card->number; // 4222222222222222

订阅

创建订阅

<?php
$subscription = $wirecardApi->subscriptions->create([
  'code' => 'assinatura01',
  'amount' => '9990',
  'payment_method' => 'CREDIT_CARD',
  'plan'  => [
    'code'  => 'plano01'
  ],
  'customer'  => [
   'code'  => 'cliente01'
  ]
]);
echo $subscription->code; // assinatura01

如果创建订阅时提供了客户信息,则可用 enableNewUser() 参数。

<?php
$subscription = $wirecardApi->subscriptions->enableNewUser()->create([
  'code' => 'assinatura01',
  'amount' => '9990',
  'payment_method' => 'CREDIT_CARD',
  'plan'  => [
    'code'  => 'plano01'
  ],
  'customer'  => [
   'code'  => 'novoCLiente'
   // Informações de customer
  ]
]);
echo $subscription->code; // assinatura01

列出所有订阅

<?php
$subscriptions = $wirecardApi->subscriptions->all();

foreach($subscriptions as $subscription) {
  echo $subscription->code; // assinatura01
}

查询订阅详情

<?php
$subscription = $wirecardApi->subscriptions->get('assinatura01');
echo $subscription->amount; // 9990