khalyomede/systempay

为Systempay生成支付表单字段,并处理支付通知(IPN)。

v0.3.0 2020-08-08 10:36 UTC

This package is auto-updated.

Last update: 2024-09-08 19:46:47 UTC


README

为Systempay生成支付表单字段,并处理支付通知(IPN)。

Packagist Version Build Status Maintainability Packagist License Packagist PHP Version Support

摘要

关于

我创建了此包,以便能够在我的Laravel商店应用程序中使用它。

我看过其他人的包,但我要么找不到我需要的所有工具,要么工具未经测试。

我希望为人们提供一个经过测试的库,这样您就可以有信心使用它。

此包遵循语义版本控制

需求

  • Composer
  • PHP版本7.1+(对应Laravel 5.6+)

安装

在项目文件夹的根目录中,运行以下命令

composer require khalyomede/systempay:0.*

示例

1. 生成表单支付隐藏字段

在这个例子中,我们将在生成要注入到HTML页面中的隐藏HTML输入之前,仅填写必填字段。

<?php

use Khalyomede\Systempay\Payment;
use Khalyomede\Systempay\Currency;
use Khalyomede\Systempay\ContextMode;
use Khalyomede\Systempay\HashAlgorithm;
use Khalyomede\Systempay\PaymentConfiguration;

$payment = new Payment;
$payment->setKey("foo")
    ->setSiteId("12345678")
    ->setTotalAmount(199.99)
    ->setContextMode(ContextMode::TEST)
    ->setCurrency(Currency::EUR)
    ->setPaymentConfiguration(PaymentConfiguration::SINGLE) // One shot payment
    ->setTransactionDate(new DateTime("NOW"))
    ->setTransactionId("xrT15p")
    ->setHashAlgorithm(HashAlgorithm::SHA256);

$fields = $payment->getHtmlFormFields();
$url = $payment->getFormUrl();

?>

<form method="POST" action="<?= $url ?>">
  <?= $fields ?>
  <button type="submit">Payer</button>
</form>

2. 处理支付通知(IPN)

在这个例子中,我们将提供来自Systempay服务器的原始POST响应来处理通知。

use Khalyomede\Systempay\PaymentNotification;
use Khalyomede\Systempay\TransactionStatus;

$notification = new PaymentNotification($_POST);
$notification->setKey("the-private-key");

if ($notification->hasValidSignature() && $notification->getTransactionStatus() === TransactionStatus::AUTHORISED) {
	echo "all went good";
} else {
	echo "depending the transaction status, you should perform a custom action";
}

API

  • Khalyomede\Systempay\Payment::class
    • Payment::__construct
    • Payment::getHashAlgorithm
    • Payment::getTotalAmount
    • Payment::getFormTotalAmount
    • Payment::getSiteId
    • Payment::getContextMode
    • Payment::getCurrencyNumericCode
    • Payment::getPaymentConfiguration
    • Payment::getTransactionDate
    • Payment::getFormTransactionDate
    • Payment::getTransactionId
    • Payment::getVersion
    • Payment::getActionMode
    • Payment::getPageAction
    • Payment::getHtmlFormFields
    • Payment::getKey
    • Payment::getFormUrl
    • Payment::setHashAlgorithm
    • Payment::setTotalAmount
    • Payment::setSiteId
    • Payment::setContextMode
    • Payment::setCurrency
    • Payment::setPaymentConfiguration
    • Payment::setTransactionDate
    • Payment::setTransactionId
  • Khalyomede\Systempay\ContextMode::class
    • ContextMode::__construct
    • ContextMode::isAllowed
    • ContextMode::getAllowedToString
  • Khalyomede\Systempay\HashAlgorithm::class
    • HashAlgorithm::__construct
    • HashAlgorithm::isSupported
    • HashAlgorithm::isAllowed
    • HashAlgorithm::getAllowedToString
  • Khalyomede\Systempay\PaymentConfiguration::class
    • PaymentConfiguration::__construct
    • PaymentConfiguration::isAllowed
    • PaymentConfiguration::getAllowedToString
  • Khalyomede\Systempay\PaymentNotification::class
  • Khalyomede\Systempay\AuthorizationResult::class
    • __construct
    • requiresToContactCardIssuer
    • detectsSuccess
    • detectsInvalidAcceptor
    • detectsInvalidTransaction
    • detectsInvalidAmount
    • detectsInvalidCardHolderNumber
    • detectsShopperCanceled
    • detectsResponseError
    • detectsExpiredCard
    • detectsUnsufficientProvision
    • detectsWrongPin
    • detectsTransactionNotPermitted
    • detectsPinAttemptsExceeded
    • requiresToKeepTheCard
    • requiresToNotHonor
    • requiresToApproveAfterIdentification
    • requiresToRepeatTransactionLater
    • requiresToContactAcquirer
    • isFraudulentResult

Payment::__construct

构造函数将自动填充以下数据

  • 货币为"EUR"
  • 支付配置为"单个交易"
  • 事务日期为当前时间
  • 一个随机的安全事务ID
  • 上下文模式为"测试模式"

Payment::getHashAlgorithm

获取哈希算法。

public function getHashAlgorithm(): string;

Payment::getTotalAmount

获取总金额。

public function getTotalAmount(): float;

Payment::getFormTotalAmount

获取总金额,格式化为符合Systempay要求(例如,无小数分隔符)。例如,如果金额为199.99,该方法返回的值将是19999。

public function getFormTotalAmount(): int;

Payment::getSiteId

获取站点ID。查看Systempay文档了解如何找到您的站点ID。

public function getSiteId(): string;

Payment::getContextMode

获取上下文模式。

public function getContextMode(): string;

Payment::getCurrencyNumericCode

获取货币的3位数字代码。

public function getCurrencyNumericCode(): int;

Payment::getPaymentConfiguration

获取支付配置。

public function getPaymentConfiguration(): string;

Payment::getTransactionDate

获取交易日期。

public function getTransactionDate(): DateTime;

Payment::getFormTransactionDate

获取格式化以供表单使用的交易日期。它按照Systempay交易日期格式要求,使用DateTime格式"YmdHis"进行格式化。

public function getFormTransactionDate(): string;

Payment::getTransactionId

获取交易ID。

public function getTransactionId(): string;

Payment::getVersion

获取支付协议版本。

public function getVersion(): string;

Payment::getActionMode

获取支付动作模式。

public function getActionMode(): string;

Payment::getPageAction

获取支付页面动作。

public function getPageAction(): string;

Payment::getHtmlFormFields

获取与您的支付对应的html表单字段。每个字段都是 <input type="hidden" />

public function getHtmlFormFields(): string;

Payment::getKey

获取您的站点密钥。查看Systempay文档了解如何找到您的站点密钥。

public function getKey(): string;

Payment::getFormUrl

获取表单URL。

public function getFormUrl(): string;

Payment::setHashAlgorithm

public function setHashAlgorithm(string $algorithm): Payment;

设置哈希算法,在sha1和(hmac) sha256之间。

throws

  • InvalidArgumentException:如果机器不支持运行的脚本中的哈希算法。
  • InvalidArgumentException:如果哈希算法不是"SHA1"或"SHA256"之一。

Payment::setTotalAmount

设置支付总金额。

public function setTotalAmount(float $amount): Payment;

Payment::setSiteId

public function setSiteId(string $siteId): Payment;

设置站点ID(检查Systempay文档了解如何找到您的站点ID)。

throws

  • InvalidArgumentException:如果提供的站点ID超过8个字符。
  • InvalidArgumentException:如果提供的站点ID不是有效的UTF-8字符串。

Payment::setContextMode

public function setContextMode(string $mode): Payment;

设置上下文模式("TEST"或"PRODUCTION")。您可以使用ContextMode类常量来避免硬编码模式。

throws

  • InvalidArgumentException:如果上下文模式不是"TEST"或"PRODUCTION"之一。

Payment::setCurrency

使用alpha-3货币代码(如"EUR")设置货币。您可以使用Currency类常量来避免硬编码货币。

public function setCurrency(string $currency): Payment;

throws

  • InvalidArgumentException:如果货币不是有效的ISO4217货币。

Payment::setPaymentConfiguration

设置支付配置("SINGLE"或"MULTI")。您可以使用PaymentConfiguration类常量来避免硬编码配置。

public function setPaymentConfiguration(string $configuration): Payment;

throws

  • InvalidArgumentException:如果支付配置不是"SINGLE"或"MULTI"之一。

Payment::setTransactionDate

设置交易日期。

public function setTransactionDate(DateTime $date): Payment;

Payment::setTransactionId

设置交易ID。

public function setTransactionId(string $transactionId): Payment;

throws

  • InvalidArgumentException:如果交易ID不是6个字符长。
  • InvalidArgumentException:如果交易ID不是有效的UTF-8字符串。

Payment::setKey

设置密钥,该密钥用于生成签名和验证请求的真实性。

public function setKey(string $key): Payment;

ContextMode::__construct

使用给定的模式构造上下文模式。

public function __construct(string $mode);

`ContextMode::isAllowed

如果上下文模式被允许,则返回true,否则返回false。

public function isAllowed(): bool;

`ContextMode::getAllowedToString

获取允许的上下文模式字符串,用逗号分隔。

public static function getAllowedToString(): string;

HashAlgorithm::__construct

使用给定的算法构造。

public function __construct(string $algorithm);

HashAlgorithm::isSupported

如果机器支持运行当前脚本的算法,则返回true,否则返回false。

public function isSupported(): bool;

HashAlgorithm::isAllowed

如果算法是SHA1或SHA256,则返回true,否则返回false。

public function isAllowed(): bool;

HashAlgorithm::getAllowedToString

获取允许的算法字符串,用逗号分隔。

public static function getAllowedToString(): string;

PaymentConfiguration::__construct

使用给定的支付配置构造。

public function __construct(string $configuration);

PaymentConfiguration::isAllowed

如果支付配置被允许,则返回true,否则返回false。

public function isAllowed(): bool;

PaymentConfiguration::getAllowedToString

返回允许的支付配置字符串,用逗号分隔。

public static function getAllowedToString(): string;

PaymentNotification::construct

使用给定的原始$_POST(或对于Laravel用户,“$request->all()”)构造。

public function __construct(array $paymentResultData)

PaymentNotification::setKey

设置(私有)密钥。您可以在您的后台办公室找到您的密钥。

public function setKey(string $key): PaymentNotification

PaymentNotification::setHashAlgorithm

设置哈希算法。如果您不希望使用硬编码字符串,可以使用HashAlgorithm类传递值。

public function setHashAlgorithm(string $algorithm): PaymentNotification

运行测试

在此项目的根目录中执行此命令

composer run test

兼容性表

?: 未测试

您可以通过以下步骤进行反向检查这些结果

  1. 切换到所需分支:git checkout v1.2.3
  2. 启动Docker容器:docker-compose up -d
  3. 在文件docker-compose.yml中,更改php服务的build键中PHP的版本,以适应您的需求
  4. 运行测试:docker-compose exec php composer run test