mage2pro / square-php-sdk
Square Connect v2 API的PHP客户端库
Requires
- php: >=5.3.3
- ext-curl: *
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ~4.0
- satooshi/php-coveralls: ~0.6.1
- squizlabs/php_codesniffer: ~2.0
README
本仓库包含为Square Connect APIs v2生成的PHP客户端SDK。查看我们用于生成此SDK的规范和模板文件的API规范仓库。
如果您正在寻找使用这些API的示例电子商务应用程序,请查看connect-api-examples
仓库。
要了解有关Square API的一般信息,请访问Square API文档
要求
PHP >= 5.4.0
- Square账户和开发者应用程序(用于授权)
安装
使用Composer
PHP SDK可在Packagist上找到。要将它添加到Composer中,只需运行
$ php composer.phar require square/connect
或在composer.json下的"require"部分添加此行
"require": {
...
"square/connect": "*",
...
}
然后使用以下命令安装您的composer依赖项
$ php composer.phar install
从GitHub
克隆此仓库,或将zip文件下载到项目的文件夹中,然后在您的代码中添加以下行
require('connect-php-sdk/autoload.php');
注意:您可能需要根据项目的文件夹结构更改路径。
使用方法
根据您要执行的操作,您将使用五个主要对象,每个对象都在下面进行了更详细的解释。
<?php
$transaction_api = new \SquareConnect\Api\TransactionApi();
$customerCard_api = new \SquareConnect\Api\CustomerCardApi();
$customer_api = new \SquareConnect\Api\CustomerApi();
$location_api = new \SquareConnect\Api\LocationApi();
$refund_api = new \SquareConnect\Api\RefundApi();
?>
位置
在执行大多数API调用之前,您需要列出Square账户的位置,因为大多数端点的URL中都包含location_id
。
$location_api = new \SquareConnect\Api\LocationApi();
列出位置
$location_api->listLocations($authorization);
$authorization
:您的访问令牌(沙盒、个人或OAuth)
响应:\SquareConnect\Model\ListLocationsResponse
$location_api->listLocations($authorization)->getErrors();
$location_api->listLocations($authorization)->getLocations();
查看您可以在位置模型中使用的所有函数
客户
客户是企业的最终用户,可以与交易关联。
$customer_api = new \SquareConnect\Api\CustomerApi();
列出客户
$customer_api->listCustomers($authorization, $cursor = null);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$cursor
:这是一个可选参数,指定要检索的客户页码。有关分页的更多信息,请参阅此处。
响应:\SquareConnect\Model\ListCustomersResponse
$customer_api->listCustomers($authorization)->getErrors();
$customer_api->listCustomers($authorization)->getCustomers();
$customer_api->listCustomers($authorization)->getCursor();
查看您可以在客户模型中使用的所有函数
创建客户
$customer_api->createCustomer($authorization, $body);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$body
:主体是一个SquareConnect\Model\CreateCustomerRequest对象,您需要使用客户信息构建它。
响应:\SquareConnect\Model\CreateCustomersResponse
$customer_api->createCustomer($authorization, $body)->getErrors();
$customer_api->createCustomer($authorization, $body)->getCustomer();
查看您可以在客户模型中使用的所有函数
检索客户
$customer_api->retrieveCustomer($authorization, $customer_id);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$customer_id
: 要检索的客户id
。在 客户对象 上使用->getId()
函数,获取检索到的客户的customer_id
。
响应: \SquareConnect\Model\RetrieveCustomerResponse
$customer_api->retrieveCustomer($authorization, $customer_id)->getErrors();
$customer_api->retrieveCustomer($authorization, $customer_id)->getCustomer();
查看所有可以在 ->getCustomer()
响应中使用的方法,请参考 客户模型。 更新客户
$customer_api->updateCustomer($authorization, $customer_id, $body)
$authorization
:您的访问令牌(沙盒、个人或OAuth)$customer_id
: 要更新的客户的id
。在 客户对象 上使用->getId()
函数,获取检索到的客户的customer_id
。$body
: 主体是一个 SquareConnect\Model\UpdateCustomerRequest 对象,您需要用客户的信息来构建它。
响应: \SquareConnect\Model\UpdateCustomerResponse
$customer_api->updateCustomer($authorization, $customer_id, $body)->getErrors();
$customer_api->updateCustomer($authorization, $customer_id, $body)->getCustomer();
查看所有可以在 客户模型 中使用的方法
删除客户
$customer_api->deleteCustomer($authorization, $customer_id)
$authorization
:您的访问令牌(沙盒、个人或OAuth)$customer_id
: 要检索的客户id
。在 客户对象 上使用->getId()
函数,获取检索到的客户的customer_id
。
响应: \SquareConnect\Model\DeleteCustomerResponse
$customer_api->deleteCustomer($authorization, $customer_id)->getErrors();
客户卡
客户卡允许商家在客户每次交易时不需提供他们的信用卡信息。了解更多信息: 保存客户信息
$customerCard_api = new \SquareConnect\Api\CustomerCardApi();
创建客户卡
$customerCard_api->createCustomerCard($authorization, $customer_id, $body);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$customer_id
: 要关联卡片的客户id
。$body
: 主体是一个 SquareConnect\Model\CreateCustomerCardRequest 对象,您需要用客户卡的信息来构建它。
响应: \SquareConnect\Model\CreateCustomerCardResponse
$customerCard_api->createCustomerCard($authorization, $customer_id, $body)->getErrors();
$customerCard_api->createCustomerCard($authorization, $customer_id, $body)->getCard();
查看所有可以在 卡片模型 中使用的方法
删除客户卡
$customerCard_api->deleteCustomerCard($authorization, $customer_id, $card_id)
$authorization
:您的访问令牌(沙盒、个人或OAuth)$customer_id
: 要删除的客户的id
。$card_id
: 要删除的卡id
。
响应: \SquareConnect\Model\DeleteCustomerCardResponse
$customerCard_api->deleteCustomerCard($authorization, $customer_id, $card_id)->getErrors();
交易
$transaction_api = new \SquareConnect\Api\TransactionApi();
列出交易
$transaction_api->listTransactions($authorization, $location_id, $begin_time = null, $end_time = null, $sort_order = null, $cursor = null)
$authorization
: 您的访问令牌(sandbox、oauth 或个人)$location_id
: 请求交易的地点id
。$begin_time
: 请求报告期的开始,采用 RFC 3339 格式。(可选)$end_time
请求报告期的结束,采用 RFC 3339 格式。(可选)$sort_order
响应中结果列表的顺序(ASC
为按时间顺序,DESC
为逆时间顺序)。(可选)$cursor
: 一个可选的分页游标,用于检索原始查询到端点的下一组结果。有关分页的更多信息,请参阅 此处。
响应: \SquareConnect\Model\ListTransactionsResponse
$transaction_api->listTransactions($authorization, $location_id)->getErrors();
$transaction_api->listTransactions($authorization, $location_id)->getTransactions();
$transaction_api->listTransactions($authorization, $location_id)->getCursor();
查看您可以使用 ->getTransactions()
响应的所有功能,请参阅 事务模型
收费(创建事务)
$transaction_api->charge($authorization, $location_id, $body);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$location_id
:您要为创建事务指定的位置ID。$body
:正文是一个 SquareConnect\Model\ChargeRequest 对象,您需要使用事务信息来构建该对象。
响应: \SquareConnect\Model\ChargeResponse
$transaction_api->charge($authorization, $location_id, $body)->getErrors();
$transaction_api->charge($authorization, $location_id, $body)->getTransaction();
查看您可以使用 ->getTransactions()
响应的所有功能,请参阅 事务模型
检索事务
$transaction_api->retrieveTransaction($authorization, $location_id, $transaction_id);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$location_id
:您要检索事务的位置ID。$transaction_id
:您要检索的事务的id
。使用 事务对象 上的->getId()
函数来获取transaction_id
响应: \SquareConnect\Model\RetrieveTransactionResponse
$transaction_api->retrieveTransaction($authorization, $location_id, $transaction_id)->getErrors();
$transaction_api->retrieveTransaction($authorization, $location_id, $transaction_id)->getTransaction();
查看您可以使用 ->getTransactions()
响应的所有功能,请参阅 事务模型
捕获事务 对之前已授权的卡片进行收费。有关更多信息,请参阅 延迟捕获事务。
$transaction_api->captureTransaction($authorization, $location_id, $transaction_id);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$location_id
:您要检索事务的位置ID。$transaction_id
:您要捕获的之前已授权事务的id
。
响应: \SquareConnect\Model\CaptureTransactionResponse
$transaction_api->captureTransaction($authorization, $location_id, $transaction_id)->getErrors();
取消事务 取消之前的卡片授权。有关更多信息,请参阅 延迟捕获事务。
$transaction_api->voidTransaction($authorization, $location_id, $transaction_id);
$authorization
:您的访问令牌(沙盒、个人或OAuth)$location_id
:您要检索事务的位置ID。$transaction_id
:您要取消的之前已授权事务的id
。
响应: \SquareConnect\Model\VoidTransactionResponse
$transaction_api->voidTransaction($authorization, $location_id, $transaction_id)->getErrors();
退款
$refund_api = new \SquareConnect\Api\RefundApi();
列出退款
$refund_api->listRefunds($authorization, $location_id, $begin_time = null, $end_time = null, $sort_order = null, $cursor = null)
$authorization
: 您的访问令牌(sandbox、oauth 或个人)$location_id
:您要请求退款的位置ID。$begin_time
: 请求报告期的开始,采用 RFC 3339 格式。(可选)$end_time
请求报告期的结束,采用 RFC 3339 格式。(可选)$sort_order
响应中结果列表的顺序(ASC
为按时间顺序,DESC
为逆时间顺序)。(可选)$cursor
: 一个可选的分页游标,用于检索原始查询到端点的下一组结果。有关分页的更多信息,请参阅 此处。
响应: \SquareConnect\Model\ListRefundsResponse
$refund_api->listRefunds($authorization, $location_id)->getErrors();
$refund_api->listRefunds($authorization, $location_id)->getCursor();
创建退款
$refund_api->createRefund($authorization, $location_id, $transaction_id, $body)
$authorization
: 您的访问令牌(sandbox、oauth 或个人)$location_id
:您要请求退款的位置ID。$transaction_id
:您要退款的之前已收费事务的id
。$body
:正文是一个 SquareConnect\Model\CreateRefundRequest 对象,您需要使用退款信息来构建该对象。
响应: \SquareConnect\Model\CreateRefundResponse
$refund_api->deleteCustomer($authorization, $customer_id)->getErrors();
$refund_api->deleteCustomer($authorization, $customer_id)->getRefund();
贡献
将错误报告、功能请求和代码贡献发送到 API规范存储库,因为这个存储库只包含生成的SDK代码。如果您注意到此SDK存在某些问题,请随时在此 处 提出问题。
许可证
Copyright 2016 Square, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.