purt09/interkassa

interkassa 的 sdk

0.1 2020-09-28 00:26 UTC

This package is auto-updated.

Last update: 2024-09-18 10:18:56 UTC


README

这是针对 psr-4 标准优化的 interkassa SDK;

基础未更改,取自 此处

Interkassa API for PHP

这个库简化了与 interkassa 支付系统 V 2.0 的工作。

安装

Composer

{
    "require": {
        "purt09/interkassa": "@dev"
    },
}

基本用法

这将注册库的自动加载,允许您使用其类而无需任何其他包含语句。

接下来,让我们创建一个简单的支付

<?php

// The following parameters are provided by interkassa
$shop_id    = '...';
$secret_key = '...';

// Create a shop
$shop = InterkassaShop::factory(array(
    'id'         => $shop_id,
    'secret_key' => $secret_key
));

// Create a payment
$payment_id     = '...'; // Your payment id
$payment_amount = '...'; // The amount to charge your shop's user
$payment_desc   = '...'; // Payment description

$payment = $shop->createPayment(array(
    'id'          => $payment_id,
    'amount'      => $payment_amount,
    'description' => $payment_desc
));

我们现在有了渲染支付表单所需的一切

<?php

// ... create and configure the payment object

?>
<form action="<?= htmlentities($payment->getFormAction()); ?>" method="post">
    <?php foreach ($payment->getFormValues() as $field => $value): ?>
    <input type="hidden" name="<?= htmlentities($field); ?>" value="<?= htmlentities($value); ?>" />
    <?php endforeach; ?>
    <button type="submit">Submit</button>
</form>

处理支付状态请求

Interkassa 可以将支付状态更新发送到您选择的 URL。您可以通过 interkassa 的账户配置此 URL 或与其他支付数据一起发送该 URL

<?php

$payment = $shop->createPayment(array(
    // ... usual payment data
    'status_url' => 'http://example.com/ik-status.php'
));

并在 ik-status.php 内部

<?php

// ... initialize library as usual

$shop = InterkassaShop::factory(array(
    'id'         => $shop_id,
    'secret_key' => $secret_key
));

try {
    $status = $shop->receiveStatus($_POST); // POST is used by default
} catch (InterkassaException $e) {
    // The signature was incorrect, send a 400 error to interkassa
    // They should resend payment status request until they receive a 200 status
    header('HTTP/1.0 400 Bad Request');
    exit;
}

$payment = $status->getPayment();

这将透明地检查支付的签名。

现在,$status 变量包含 InterkassaStatus 类的实例。变量 $payment 包含带有所有初始数据的 InterkassaPayment 类的实例。

注意,还支持成功和失败状态更新,但没有签名,并且通过用户的浏览器发送,因此不建议依赖它们。

要求

此库至少需要 PHP 5.1.0 才能正确工作。然而,始终推荐使用最新的 PHP 版本。

许可

此库在开源 MIT 许可下发布,这使您能够在任何情况下使用和修改它。

有关完整的版权和许可信息,请参阅与源代码一起分发的 LICENSE 文件。