定制 kassacom/php-sdk 二次开发

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

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

kassacom/php-sdk

Composer 安装命令:

composer require kassacom/php-sdk

包简介

Kassa.com PHP SDK

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version

Documentation: https://kassa.com/help/

Requirements

PHP 5.5 and later.

Dependencies

The bindings require the following extensions in order to work properly:

Optionally

Composer

You can install the lib via Composer. Run the following command:

composer require kassacom/php-sdk

To use the bindings, use Composer's autoload:

require_once('vendor/autoload.php');

If you use Composer, these dependencies should be handled automatically.

Getting Started

We recommend using the GuzzleHttp Client

Init client

$guzzleClient = new GuzzleHttp\Client();
$transport = new KassaCom\SDK\Transport\GuzzleApiTransport($guzzleClient);
$client = new KassaCom\SDK\Client($transport);
$client->setAuth('login', 'secret'); // or for basic authorization $client->setAuth('login', 'password', KassaCom\SDK\Transport\Authorization\BasicAuthorization::class);

All requests are processed in similar steps:

  1. Create request instance of KassaCom\SDK\Model\Request\AbstractRequest
  2. Request serialization
  3. Sending a request to the server
  4. You have a response object instance of KassaCom\SDK\Model\Response\AbstractResponse or throws exception if request fail

All requests are creating by suitable objects or can be created on the basis of arrays, integers and strings

Create payment

Creating request with object

// Create a request object
$createPaymentRequest = new KassaCom\SDK\Model\Request\Payment\CreatePaymentRequest();

// Set up $createPaymentRequest with required params

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CreatePaymentResponse $createPaymentResponse */
    $createPaymentResponse = $client->createPayment($createPaymentRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with array

$requestArray = [
    'order' => [/*...*/],
    'settings' => [/*...*/],
    'custom_parameters' => [/*...*/],
    'receipt' => [/*...*/],
];

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CreatePaymentResponse $createPaymentResponse */
    $createPaymentResponse = $client->createPayment($requestArray);
} catch (\Exception $e) {
    // ...
}

Process payment

Creating request with object

// Create a request object
$processPayment = new KassaCom\SDK\Model\Request\Payment\ProcessPaymentRequest();

// Set up $processPayment with required params

try {
    /** @var KassaCom\SDK\Model\Response\Payment\ProcessPaymentResponse $processPaymentResponse */
    $processPaymentResponse = $client->processPayment($processPayment);
} catch (\Exception $e) {
    // ...
}

or you can create request with array

$requestArray = [
    'token' => 'token',
    'ip' => '127.0.0.1',
    'payment_method_data' => [/*...*/],
];

try {
    /** @var KassaCom\SDK\Model\Response\Payment\ProcessPaymentResponse $processPaymentResponse */
    $processPaymentResponse = $client->processPayment($requestArray);
} catch (\Exception $e) {
    // ...
}

Capture payment

Creating request with object

// Create a request object
$capturePaymentRequest = new KassaCom\SDK\Model\Request\Payment\CapturePaymentRequest('token-string-from-create-payment-step');

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CapturePaymentResponse $capturePaymentResponse */
    $capturePaymentResponse = $client->capturePayment($capturePaymentRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with token

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CapturePaymentResponse $capturePaymentResponse */
    $processPaymentResponse = $client->processPayment('token-string-from-create-payment-step');
} catch (\Exception $e) {
    // ...
}

Get payment info

Creating request with object

// Create a request object
$getPaymentRequest = new KassaCom\SDK\Model\Request\Payment\GetPaymentRequest('token-string-from-create-payment-step');

try {
    /** @var KassaCom\SDK\Model\Response\Payment\GetPaymentResponse $getPaymentResponse */
    $getPaymentResponse = $client->getPayment($getPaymentRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with token

try {
    /** @var KassaCom\SDK\Model\Response\Payment\GetPaymentResponse $getPaymentResponse */
    $getPaymentResponse = $client->getPayment('token-string-from-create-payment-step');
} catch (\Exception $e) {
    // ...
}

Cancel payment

Creating request with object

// Create a request object
$cancelPaymentRequest = new KassaCom\SDK\Model\Request\Payment\CancelPaymentRequest('token-string-from-create-payment-step');

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CancelPaymentResponse $cancelPaymentResponse */
    $cancelPaymentResponse = $client->cancelPayment($cancelPaymentRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with token

try {
    /** @var KassaCom\SDK\Model\Response\Payment\CancelPaymentResponse $cancelPaymentResponse */
    $cancelPaymentResponse = $client->cancelPayment('token-string-from-create-payment-step');
} catch (\Exception $e) {
    // ...
}

Create payout

Creating request with object

// Create a request object
$createPayoutRequest = new KassaCom\SDK\Model\Request\Payout\CreatePayoutRequest();

// Set up $createPayoutRequest with required params

try {
    /** @var KassaCom\SDK\Model\Response\Payout\CreatePayoutResponse $createPayoutResponse */
    $createPayoutResponse = $client->createPayout($createPayoutRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with array

$requestArray = [
    'transaction_id' => 'transaction_id',
    'wallet_id' => 123,
    'fee_type' => 'fee_type',
    'payout_method_data' => [/*...*/],
    'order' => [/*...*/],
];

try {
    /** @var KassaCom\SDK\Model\Response\Payout\CreatePayoutResponse $createPayoutResponse */
    $createPayoutResponse = $client->createPayout($requestArray);
} catch (\Exception $e) {
    // ...
}

Get payout info

Creating request with object

// Create a request object
$getPayoutRequest = new KassaCom\SDK\Model\Request\Payout\GetPayoutRequest();

// Set up $getPayoutRequest with required params

try {
    /** @var KassaCom\SDK\Model\Response\Payout\GetPayoutResponse $getPayoutResponse */
    $getPayoutResponse = $client->getPayout($getPayoutRequest);
} catch (\Exception $e) {
    // ...
}

or you can create request with array

$requestArray = [
    'transaction_id' => 'transaction_id',
    'wallet_id' => 123,
];

try {
    /** @var KassaCom\SDK\Model\Response\Payout\GetPayoutResponse $getPayoutResponse */
    $getPayoutResponse = $client->getPayout($requestArray);
} catch (\Exception $e) {
    // ...
}

or create by id

try {
    /** @var KassaCom\SDK\Model\Response\Payout\GetPayoutResponse $getPayoutResponse */
    $getPayoutResponse = $client->getPayout(123);
} catch (\Exception $e) {
    // ...
}

Payments report

Download report file

$paymentsReport = new KassaCom\SDK\Model\Request\Reports\PaymentsReportRequest();
$paymentsReport->setDatetimeFrom(new \DateTime('2 weeks ago'))
            ->setDatetimeTo(new \DateTime());

$response = $client->getPaymentsReport($paymentsReport);

http_response_code($response->getStatusCode());
$stream = $response->getBody();

foreach ($response->getHeaders() as $key => $header) {
    header($key . ': ' . join(',', $header));
}

echo $stream->getContents();

Payouts report

Download report file

$payoutsReportRequest = new KassaCom\SDK\Model\Request\Reports\PayoutsReportRequest();
$payoutsReportRequest->setDatetimeFrom(new \DateTime('2 weeks ago'))
            ->setDatetimeTo(new \DateTime());

$response = $client->getPayoutsReport($payoutsReportRequest);

http_response_code($response->getStatusCode());
$stream = $response->getBody();

foreach ($response->getHeaders() as $key => $header) {
    header($key . ': ' . join(',', $header));
}

echo $stream->getContents();

Exceptions

  • KassaCom\SDK\Exception\TransportException - throws in the case of an api transport error. For example, when authorization data is not provided.
  • KassaCom\SDK\Exception\JsonParseException - the server response doesn't contain a valid json.
  • KassaCom\SDK\Exception\ServerResponse\ResponseException - 4xx and 5xx server errors.
  • KassaCom\SDK\Exception\Response\ResponseParseException - create response errors.
  • KassaCom\SDK\Exception\Request\RequestParseException - create request errors.

Processing notification from server

This code is responsible for processing the payment result. You need to create handler, make it available on URL in your application and specify the URL in the project settings in kassa.com. This handler will be called after the user makes a payment using the form on kassa.com

$notification = new Notification();
$notification->setApiKey('api-key');
$notification->process();

You can use manual response to server:

$notification->process(false);

// if success
$notification->successResponse();
// if error
$notification->errorResponse('Error message');

If you use a proxy server, you can skip the IP check

$notification->setSkipIpCheck();

Custom Api Transport

You can create your own api transport by extending KassaCom\SDK\Transport\AbstractApiTransport

class MyApiTransport extends AbstractApiTransport {
    protected function sendRequest(Psr7\Request $request) {
        // Implementing the sendRequest() method
    }
}

kassacom/php-sdk 适用场景与选型建议

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

它主要适用于以下技术方向: 「payment」 「money」 「payments」 「pay」 「sdk」 「cards」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 40.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-06-07