pagantis/orders-api-client

PHP版本的Pagantis订单API客户端。使用简单的方法和面向对象的方式使用API。

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内部使用的对象已经定义为具有所需属性的类。每个对象都有一个设置器和获取器的配置,以便于验证和面向对象。

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客户端向Pagantis发送API调用。结果是与所有字段已完成的相同订单对象。状态是已创建

存储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.

帮助我们改进

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