chemistrymarketing/zoho-subscriptions-sdk

v0.0.3-alpha 2018-09-12 14:54 UTC

This package is auto-updated.

Last update: 2024-09-13 04:39:23 UTC


README

针对 Zoho Subscriptions 的部分 PHP SDK

用法

要实例化客户端,请使用带有您组织 ID 和 Zoho 的 Auth 令牌的静态 build 方法。

use ZohoSubscription\Client as ZohoClient;

$client = new ZohoClient::build($id, $token);

客户

如果您正在创建客户,大多数客户数据在技术上不是必需的(除了电子邮件地址),因此我们有设置方法在提供时添加它

$customer = new \ZohoSubscription\Resources\Customers\Customer($email);
$customer->setName($firstName, $lastName, $salutation);
$customer->setCompanyName($companyName);
$customer->setCurrencyCode($currencyCode);
$customer->setDisplayName($displayName);
$customer->setVatRegistration($countryCode, $vatNumber);

您可以这样为客户添加自定义字段

$customer->addCustomField('label', 'value', 'data type');
$customer->addCustomField('label 2', 'value', 'data type');

然后,在您准备好保存客户时,将其发送到客户端的方法,该方法返回客户 ID

$id = $client->send($client)->getId();

客户地址

向客户添加地址需要您创建地址对象

$address = new  new \ZohoSubscription\Resources\Customers\Address();
$address->setRegion($country, $state, $zip);
$address->setLocale($street, $city, $attention);

然后您可以在发送请求之前将其添加到客户的账单地址或送货地址

$customer->setBillingAddress($address);

$customer->setShippingAddress($address);

托管页面:订阅

创建订阅最简单的方法是创建一个订阅实体并将其发送到客户端

$subscription = new  new \ZohoSubscription\Resources\HostedPages\Subscription($customerId, $planId);

$url = $client->send($subscription)->getId();

这会创建一个托管页面,并返回用户需要重定向以完成订阅的 URL。

在发送请求之前,您可以将重定向 URL 添加到订阅中,以让 Zoho 知道订单完成后将用户发送到何处

$subscription->addRedirectUrl($redirectUrl);

区域

Zoho Subscriptions 可在不同的区域使用,这些区域有不同的 URL 用于访问 API。

要切换到欧盟区域,您可以在 Client 类中使用 setApiRegionEU() 方法,并使用 setApiRegionCOM() 返回

$client = ZohoSubscription\Client::build();

$client->setApiRegionEU();
$client->setApiRegionCOM();

$request = new ZohoSubscription\Resources\Customers\Customer('test@example.com');

$client->send($request);

创建新的 API 方法

我目前只为当前项目所需的函数创建了 API 类。如果您需要其他内容,您可以通过实现 ZohoSubscription\Contracts\Requestable 接口并可选地使用 ZohoSubscription\Partials\HasRequestables 特性来轻松做到这一点,例如这是一个非常基本的支付 API 实现

namespace MyCo\Zoho\Resources\Payments;

use ZohoSubscription\Contracts\Requestable;
use ZohoSubscription\Partials\HasRequestables;

class Payment implements Requestable
{
    use HasRequestables;
    
    public function __construct(string $customerId, int $amount, string $paymentMode)
    {
        $this->attributes['customer_id'] = $customerId;
        $this->attributes['amount'] = $amount;
        $this->attributes['payment_mode'] = $paymentMode;
    }

    /**
     * @return string
     * @throws \Exception
     */
    public function getUri(): string
    {
        return 'payments';
    }

    /**
     * @return string
     * @throws \Exception
     */
    public function getId(): string
    {
        if (is_null($this->response)) {
            throw new \Exception('Trying to get ID when request not sent yet');
        }
        return json_decode($this->response->getBody())->payment->payment_id;
    }
}

您可以将它构建起来并传递给客户端的 send 方法。

$payment = new MyCo\Zoho\Resources\Payments($customerId, $amount, 'cash');

$paymentId = $client->send($payment)->getId();