lunalabs-srl / zuora-api
一个简单的PHP包,用于集成Zuora REST API。
0.3.3
2021-03-05 08:45 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ~7.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
一个简单的PHP包,用于在您的项目中集成Zuora REST API。
安装
您可以直接使用以下命令安装包
composer require lunalabs-srl/zuora-api
测试包
创建一个包含以下行的简单PHP文件
<?php require_once __DIR__ . '/vendor/autoload.php'; use LunaLabs\ZuoraApi\ZuoraApi; use LunaLabs\ZuoraApi\ZuoraCallout; use LunaLabs\ZuoraApi\Exceptions\ZuoraApiException; // Zuora credentials. $zuoraConfig = [ 'clientId' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'clientSecret' => 'XXXXX=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=XXX', 'baseUri' => 'https://rest.sandbox.eu.zuora.com', 'apiVersion' => 'v1' ]; try { // Instance. $zuora = new ZuoraApi($zuoraConfig); // Retrieves all the stored accounts. $response = $zuora->account()->all(); // Prints the result as JSON. header('Content-Type: application/json'); echo json_encode($response); } catch (ZuoraApiException $e) { // In case of error, you can handle the formatted object response through some useful properties. header('Content-Type: application/json'); echo json_encode($e->errorFormatter()); }
Account实体上的可用方法
$accountData = [ 'name' => 'John Doe', 'notes' => 'Optional notes', 'currency' => 'EUR', 'paymentTerm' => 'Due Upon Receipt', 'autoPay' => false, 'billCycleDay' => 0, 'billToContact' => [ 'firstName' => 'John', 'lastName' => 'Doe', 'workEmail' => 'john.doe@domain.com', 'address1' => 'Strange street 69', 'city' => 'CityName', 'country' => 'CountryName', 'state' => 'ST', 'zipCode' => '01234', ], 'invoiceDeliveryPrefsEmail' => false, 'invoiceDeliveryPrefsPrint' => false ]; $accountNewData = [ 'name' => 'John Doe Jr', 'billToContact' => [ 'lastName' => 'Doe Jr', ], 'additionalEmailAddresses' => [ "john.doe.jri@domain.com", ] ]; try { $response = $zuora->account()->all(); $response = $zuora->account()->get('A00000001'); $response = $zuora->account()->create($accountData); $response = $zuora->account()->update('A00000001', $accountNewData); $response = $zuora->account()->delete('1adcc59c723b9c3f01723d09813c2965'); $response = $zuora->account()->query('select Id, Name, AccountNumber, Balance from Account'); $response = $zuora->account()->fields(); $response = $zuora->account()->field('AccountNumber'); $response = $zuora->account()->prepareFieldsQuery($response, ['SequenceSetId', 'TaxExemptEntityUseCode', 'TotalDebitMemoBalance', 'UnappliedCreditMemoAmount']); $response = $zuora->account()->query('select '.$response.' from account'); } catch (ZuoraApiException $e) { // In case of error, you can handle the formatted object response through some useful properties. header('Content-Type: application/json'); echo json_encode($e->errorFormatter()); }
PaymentMethod实体上的可用方法
// If you want to associate a tokenized credit card to an existing Zuora account, you have to use a payload like this: $tokenizedCard = [ 'AccountId' => 'ZUORA_ACCOUNT_ID', 'Type' => 'CreditCardReferenceTransaction', 'UseDefaultRetryRule' => true, 'TokenId' => 'CARD_ID_FROM_YOUR_PAYMENT_GATEWAY', 'SecondTokenId' => 'REFERENCE_ID_FROM_YOUR_PAYMENT_GATEWAY' ]; $tokenizedSepa = [ 'AccountId' => 'ZUORA_ACCOUNT_ID', 'Type' => 'BankTransfer', 'BankTransferType' => 'SEPA', 'UseDefaultRetryRule' => true, 'BankTransferAccountNumber' => 'IT4000000000000000000000000', // The user's IBAN 'MandateReceived' => 'Yes', 'ExistingMandate' => 'Yes', 'MandateID' => 'SLMP000000000' ]; try { // Retrieves the payment method details by its ID. $response = $zuora->paymentMethod()->get('PAYMENT_METHOD_ID'); // Creates the given payment method (credit card or SEPA). // This response will return the payment ID to use to set the default payment. $response = $zuora->paymentMethod()->create($tokenizedCard); // Updates the desired payment method by its ID with the new data. $response = $zuora->paymentMethod()->update('PAYMENT_METHOD_ID', $newData); // Deletes the given payment method ID (you can not delete the default payment method). $response = $zuora->paymentMethod()->delete('PAYMENT_METHOD_ID_TO_DELETE'); // Associates the given payment method to the given existing account ID, marking it as default payment method. $response = $zuora->paymentMethod()->setDefaultPayment('ZUORA_ACCOUNT_ID', 'PAYMENT_METHOD_ID'); } catch (ZuoraApiException $e) { // ... }
此调用将返回一个包含属性 Success = true 和 注册支付的唯一ID 的对象。
Product实体上的可用方法
try { // Retrieves the product details by its ID. $response = $zuora->product()->get('PRODUCT_ID'); // Retrieves all the rate plans of the given product ID (to be used during the order creation). $response = $zuora->product()->getRatePlans('PRODUCT_ID'); } catch (ZuoraApiException $e) { // ... }
Order实体上的可用方法
$orderData = [ 'description' => 'Test order description', 'existingAccountNumber' => 'ZUORA_ACCOUNT_ID', 'orderDate' => '2020-01-01', 'subscriptions' => [ ... ] ]; try { // Retrieves the order details by the order number. $response = $zuora->order()->get('ORDER_NUMBER'); // Creates the order and the subscription for the desired account. $response = $zuora->order()->create($orderData); // Deletes the given order by the order number. $response = $zuora->order()->delete('ORDER_NUMBER'); } catch (ZuoraApiException $e) { // ... }
其他实体上的可用方法
其他实体提供了类似的方法来检索、保存、更新和删除记录。只需打开所需的实体类,查看它覆盖的内容。
如果您需要在实体上非现有方法上进行特定调用,您可以使用此处描述的通用 getResource() 方法。
$response = $zuora->account()->getResource('POST', '/accounts/ZUORA_ACCOUNT_ID', $data);
调用
可以通过为每个调用指定自定义用户名和密码来处理 Zuora调用。要实例化调用处理程序,请编写以下行
$callout = new ZuoraCallout(['username' => 'customUsername', 'password' => 'customPassword']); $response = $callout->getResponse();
如果您想 记录调用响应,可以将您自定义的记录系统作为第二个参数注入。请注意,您的记录器必须在内部具有一个 "write()" 方法,如下面的简单示例所示。
class Log { public function write($input) { $path = "./callout.log"; error_log(json_encode($input), 3, $path); } } $customLog = new Log; $callout = new ZuoraCallout(['username' => 'customUsername', 'password' => 'customPassword'], $customLog); $response = $callout->getResponse();
待办事项
工厂实体
- accounts
- catalog
- contacts
- invoices
- orders
- paymentgateways
- paymentmethod
- payments
- product
- rate-plan
- refunds
- subscriptions
- transactions
测试
- 单元测试
分页
- 处理分页