定制 compayer/compayer-lib-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

compayer/compayer-lib-php

Composer 安装命令:

composer require compayer/compayer-lib-php

包简介

Library for push stat message to Compayer from php-based projects

README 文档

README

Latest Stable Version Build Status Code Coverage Downloads GitHub license

Compayer is a stat data preprocessor and web analytics service that tracks customer events in payment forms for financial and marketing reports.

Compayer PHP SDK library is designed to push stat messages to the Compayer analytics from the php-based projects.

Features

  • Creates and sends the Events of start, success, failure or refund payments to the Compare analytics.
  • Helps to convert a response message from payment systems Yandex.Money, Xsolla and PaySuper to the Event message.

Table of Contents

Requirements

  • PHP >= 5.5
  • Required PHP extensions: json

Getting Started

Register your account in Compayer analytics to get:

  • CLIENT ID (the unique identifier for your client)
  • SECRET KEY (the secret API key for your client)

Installation

We recommend installing Compayer PHP SDK using Composer.

$ cd /path/to/your/project
$ composer require compayer/compayer-lib-php

After installing, you need to require the Composer's autoloader:

require '/path/to/vendor/autoload.php';

Usage

To use analytics you need to send 2 events:

  • The Event start when a user initiates a payment. The start event is optional, but we strongly recommend using it to track the entire payment chain.
  • One of the Events success, fail or refund after the payment system responds about the result of the operation.

The Event tries automatically determine the user IP address and address of the payment initiation page based on the data from the server request. If the user’s request is not available to the script, you can set the payment initiation page or user IP address by yourself (this is necessary for the geolocation filters to work correctly).

To send an Event start, use the following example:

use Compayer\SDK\Client;
use Compayer\SDK\Config;
use Compayer\SDK\Event;
use Compayer\SDK\Exceptions\SdkException;

const CLIENT_ID = 'client_id';
const SECRET_KEY = 'secret_key';

// Create and configure a configuration object (including debug mode).
$config = new Config(CLIENT_ID, SECRET_KEY);
$config->setDebugMode(true);

// Create an SDK client for sending events.
$client = new Client($config);

// Create an instance of the Event class and set the maximum possible properties about a user and payment.
// All fields are optional, but it's important to fill out one of the fields: "userEmails", "userPhones" or "userAccounts" 
// to identify the user made the payment.
$event = (new Event())
    ->setMerchantTransactionId('12345')
    ->setPaymentAmount(250.50)
    ->setPaymentCurrency('RUB')
    ->setUserLang('RUS')
    ->setUserEmails(['customer@compayer.com'])
    ->setUserAccounts(['54321'])
    ->setExtra(['my_property' => 'value']);

// You can also create an object with an event from an array, 
// where names of the keys of the array match names of the Event properties.
$event = Event::fromArray([
    'merchantTransactionId' => '12345',
    'paymentAmount' => 250.50,
    'paymentCurrency' => 'RUB',
    'userLang' => 'RUS',
    'userEmails' => ['customer@compayer.com'],
    'userAccounts' => ['54321'],
    'extra' => ['my_property' => 'value'],
]);

try {
    // Send the generated event and get the response message with a transaction identifier and log.
    $response = $client->pushStartEvent($event);
} catch (SdkException $e) {
    print_r($e->getMessage());
}

// Use it to send "success", "fail" or "refund" events and to chain events.
// The transaction identifier is UUID string like 3677eb06-1a9a-4b6c-9d6a-1799cae1b6bb.
$transactionId = $response->getTransactionId();

// Show logs with the debug mode configuration.
print_r($response->getLog());

After a payment system has received a response about a payment result (success, failure or refund), you need to send an Event with the data that you received after the payment. You can form the response event as described in the Event start. If you received a transaction ID at the start step, set it to link the entire payment chain.

For success, failure or refund events a payment system response is required in its original form. The response should be written as a string with the key "response" in the property extra.

For example, if the answer received in the JSON format then use the construct: setPaymentSystemResponse(json_encode($jsonPaymentSystemResponse)).

use Compayer\SDK\Client;
use Compayer\SDK\Config;
use Compayer\SDK\Event;
use Compayer\SDK\Exceptions\SdkException;

const CLIENT_ID = 'client_id';
const SECRET_KEY = 'secret_key';

// Create and configure a configuration object (e.g. with debug mode).
$config = new Config(CLIENT_ID, SECRET_KEY);
$config->setDebugMode(true);

// Create SDK client for sending events.
$client = new Client($config);

// Transaction ID received on start event
$transactionId = '3677eb06-1a9a-4b6c-9d6a-1799cae1b6bb';

// Create an instance of the Event class and set the maximum possible properties about the user and payment
// All fields are optional, but you must fill out one of the fields: "userEmails", "userPhones" or "userAccounts" 
// to identify the user who made the payment. If you have a transaction ID for the start event, specify it.
$event = (new Event())
    ->setTransactionId($transactionId)
    ->setMerchantTransactionId('12345')
    ->setPaymentAmount(250.50)
    ->setPaymentCurrency('RUB')
    ->setPayoutAmount(3.87)
    ->setPayoutCurrency('USD')
    ->setUserEmails(['customer@compayer.com'])
    ->setUserAccounts(['54321'])
    ->setExtra(['my_property' => 'value'])
    ->setPaymentSystemResponse('Payment system response as a string');

try {
    // Send the generated event
    // Or use $client->pushFailEvent($event) in case of payment failure
    // Or use $client->pushRefundEvent($event) in case of payment refund
    $client->pushSuccessEvent($event);
} catch (SdkException $e) {
    print_r($e->getMessage());
}

License

The project is available as open source under the terms of the MIT License.

compayer/compayer-lib-php 适用场景与选型建议

compayer/compayer-lib-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 186 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 compayer/compayer-lib-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 compayer/compayer-lib-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 compayer/compayer-lib-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 186
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 6
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2020-06-23