pagamastarde/orders-api-client

此软件包已被废弃且不再维护。作者建议使用 pagantis/orders-api-client 软件包。

PHP版的Pagantis订单API客户端。使用简单方法和使用OOP(面向对象编程)。

v2.1.5 2020-03-31 06:05 UTC

README

CircleCI: CircleCI

Latest Stable Version composer.lock Scrutinizer Code Quality

订单API客户端为与Pagantis合作的商家提供了一种无需进行完整开发即可使用API服务的方法。该库为API中的每种类型的对象和方法调用提供了存根。API支持的所有方法都在此客户端中实现,并在代码和此处进行了文档说明。

所有代码都经过外部服务的测试和审查。

如何使用

通过以下方式安装库

https://github.com/pagantis/orders-api-client/releases/latest

  • 使用Composer
composer require pagantis/orders-api-client

最后,请确保包含自动加载器

require_once '/path/to/your-project/vendor/autoload.php';

一旦库准备就绪并位于项目中,存根对象将可用,ordersApiClient也将可用。

//Create a OrdersApiClient object, for example:
$ordersApiClient = new OrdersApiClient($publicKey, $privateKey);

//Example: get an existing Order status:
$order = $ordersApiClient->getOrder($pagantisOrderId); //$pmOrderId is the id of the order
if ($order instanceof Pagantis\OrdersApiClient\Model\Order) {
    $orderStatus = $order->getStatus();
    echo $orderStatus;
}

// You can investigate the rest of the methods. And find all the documentation of the API here:
// https://developer.pagantis.com/api/

示例 / 演示工具

PHP示例演示工具中找到完整的集成此处

对象

API内部使用的对象已经定义为具有所需属性的类。每个对象都有设置器和获取器,以便于验证和OOP。

src/Model中找到定义的订单对象。在订单文件夹中,可以查看主订单对象拥有的每个元素。

使用API客户端时始终使用定义的对象。例如,在创建退款时

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

//Create a Refund object and set the amounts:
$refund = new Pagantis\OrdersApiClient\Model\Order\Refund();
$refund
    ->setPromotedAmount(0)
    ->setTotalAmount(10)
;

//Then use the API client to generate a the refund:
$refundCreated = $apiClient->refundOrder($orderId, $refund);
?>

@Exception Handling

use Try|Catch when using the API methods, since it can cause HTTP exceptions.

API方法

创建订单

要使用API客户端创建订单,从一个空的订单对象开始,创建子对象并设置必需的信息。

然后使用API客户端将API调用发送给Pagantis。结果是与剩余字段完整填充的相同的订单对象。状态为已创建

存储Pagantis订单ID和商家订单ID之间的关系,以便在创建后识别订单。

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

$order = new \Pagantis\OrdersApiClient\Model\Order();
$order
    ->setConfiguration($configurationObject)
    ->setShoppingCart($shoppingCartObject)
    ->setUser($userObject)
    ->setMetadata($metadataObject)
;

//Notice, Create and fill with information the 4 objects

//To see the JSON result of a order Object:
echo json_encode(
  $order->export(),
  JSON_PRETTY_PRINT |
  JSON_UNESCAPED_SLASHES |
  JSON_UNESCAPED_UNICODE
);

//Finally create the order by using the client:
$orderCreated = $orderApiClient->createOrder($order);



/*
 * The Order object is defined inside the library and is prepared for OOP.
 * The attributes work with GETTERS and SETTERS
 *
 * The Response is parsed within the Client and is transformed into a Previously Defined Object with
 * useful methods.
 */

?>

@Exception Handling

use Try|Catch when using order Create method, since it can cause HTTP exceptions.

When setting data into the order object there is Client Exceptions that may force to set the attributes in the
correct format.

获取订单

使用“获取订单”方法从Pagantis服务器再次检索订单。检索到的订单状态已更新。存储Pagantis订单ID和商户订单ID之间的关系,以便能够在订单创建后识别订单。

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

//By storing the Pagantis order ID, fetch back the updated order:
$order = $orderApiClient->getOrder($orderId);

?>

@Exception Handling

use Try|Catch when using get Order method, since it can cause HTTP exceptions.

<?php

/** Tip: Navigate inside the fetched order properties:

/** $status array() **/
$status = $order->getStatus();

/** $amount int **/
$amount = $order->getShoppingCart()->getTotalAmount();

/** $createdAt \DateTime **/
$createdAt = $order->getCreateAt();

/** $refunds Refund[] **/
$refunds = $order->getRefunds();

列出订单

查找订单,获取昨天的所有订单,查看在线或店内订单,列出已确认订单。使用此服务在系统中查找订单。查看所有queryString参数在此

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

//Define the queryString parameters to filter:
$queryString = [
    'channel'       => 'online',
    'pageSize'      => 2,
    'page'          => 1,
    'status'        => Order::STATUS_CONFIRMED,
    'createdFrom'   => '2018-06-28T14:08:01',
    'createdTo'     => '2018-06-28T14:08:03',
];

$orders = $orderApiClient->listOrders($queryString);

?>

@Exception Handling

use Try|Catch when using get Order method, since it can cause HTTP exceptions.



<?php

/** Tip: Iterate inside the fetched list of Orders:

// Calculate the total amount of sales withing the orders: List the orders from yesterday with status CONFIRMED and...

$amount = 0;

foreach ($orders as $order) {
    $amount += $order->getShoppingCart()->getTotalAmount();
}

// In cents, the total amount of sales from yesterday.
echo $amount;

确认订单

当订单处于授权状态时,确认是商户通知支付方式他验证并确认用户已支付订单的操作。已确认的订单将被处理,并创建贷款。一旦贷款被确认,它就可以进行退款。

可以为订单添加多个回调以通知订单授权或拒绝。还可以列出所有待确认的订单。

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

$order = $orderApiClient->confirmOrder($orderId);

/** @See https://github.com/pagantis/orders-api-client **/

?>

@Exception Handling

use Try|Catch when using get Order method, since it can cause HTTP exceptions.

<?php

/** Tip: List the AUTHORIZED orders and confirm them to prevent loosing any transaction:

$authorizedOrders = $orderApiClient->listOrders([
    'status' => Order::STATUS_AUTHORIZED,
]);

foreach ($orders as $order) {
    // validate the payment in the merchant system and then inform Pagantis API that
    // the order is processed and valid in the merchant
    $orderConfirmed = $orderApiClient->confirmOrder($order->getId());
}

?>

Remember that if a AUTHORIZED order is not confirmed, the payment will
be released and the loan will not be created. It is mandatory to
confirm all AUTHORIZED orders.

退款订单

退款是订单总金额的扣除。退款只能在对已确认的订单提出请求。订单的退款将自动减少分期付款的金额。

订单可以有多次退款,只要它们不超过订单总金额。一旦总金额被全额退款,订单状态将保持为“已确认”。

<?php

//Use the API Client to operate with the API
$orderApiClient = new Pagantis\OrdersApiClient\Client(
    $publicKey,
    $privateKey
);

//Create a Refund object and set the amounts:
$refund = new Pagantis\OrdersApiClient\Model\Order\Refund();
$refund
    ->setPromotedAmount(0)
    ->setTotalAmount(10)
;

//Then use the API client to generate a the refund:
$refundCreated = $apiClient->refundOrder($orderId, $refund);

?>

@Exception Handling

use Try|Catch when using get Order method, since it can cause HTTP exceptions.

帮助我们改进

我们很高兴接受建议或pull requests。如果您愿意帮助我们开发更好的软件,请在此处创建pull request,遵循PSR-2代码风格,我们将使用reviewable来检查代码,如果所有测试通过,且没有通过SensioLab Insights检测到问题,您就可以准备合并了。