nevercom / iripg
用于与伊朗银行系统协同工作的IPG(互联网支付网关)管理器。
Requires
- php: >=5.3.0
- joshcam/mysqli-database-class: ^2.9
This package is not auto-updated.
Last update: 2024-09-23 05:58:34 UTC
README
为伊朗银行系统设计的IPG(互联网支付网关)管理器
使用Composer安装
composer require nevercom/iripg
使用方法
此库默认使用MySQL
数据库和预定义的DB Schema
。您需要创建所需的表才能使用此库。(顺便说一句,如果您不想使用此库中提供的类,您可以使用自己的模式实现自己的DB逻辑)
-- phpMyAdmin SQL Dump
-- http://www.phpmyadmin.net
--
-- Generation Time: Feb 02, 2018 at 01:24 PM
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
-- --------------------------------------------------------
--
-- Table structure for table `bank_logs`
--
DROP TABLE IF EXISTS `bank_logs`;
CREATE TABLE IF NOT EXISTS `bank_logs` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`pay_id` int(20) NOT NULL,
`method` varchar(2048) NOT NULL,
`status_code` int(11) NOT NULL,
`input` text NOT NULL,
`output` text NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `pay_id` (`pay_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `bank_transactions`
--
DROP TABLE IF EXISTS `bank_transactions`;
CREATE TABLE IF NOT EXISTS `bank_transactions` (
`pay_id` bigint(20) NOT NULL AUTO_INCREMENT,
`transaction_id` bigint(20) NOT NULL,
`bank_name` varchar(255) NOT NULL,
`bank_id` tinyint(4) NOT NULL,
`amount` bigint(20) NOT NULL,
`reference_id` varchar(256) NOT NULL,
`authority_id` varchar(255) DEFAULT NULL,
`status` tinyint(3) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`pay_id`),
KEY `transaction_id` (`transaction_id`),
KEY `reference_id` (`reference_id`(255)),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
首先,您需要扩展您想要使用的每个网关类,并为此网关提供认证信息
<?php
namespace Test;
use IPG\Gateways\Mellat\MellatIPG;
class MyMellat extends MellatIPG {
protected $terminalId = '123456789';
protected $userName = 'username';
protected $userPassword = 'P4$$w0rD';
}
现在,此类可以用作IPGManager
的支付网关。
然后,需要初始化IPGManager
$man = new IPGManager(new MyMellat(), new IPGDatabase('username', 'password', 'db_name'));
下一步是启动支付流程
$res = $man->startPayment(time(), 10000, 'http://your-site.ir/callback.php');
然后将所需数据通过POST
发送到目标URL
if (!$res->isIsSuccessful()) {
echo 'FAILED';
exit();
}
$location = 'poster.php?data=' . json_encode($res->getData()) . '&url=' . $res->getTargetUrl();
header("Location: {$location}");
如果您需要,这里有一个示例poster.php
<html>
<head>
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var jsondata = getParameterByName("data");
var obj = JSON.parse(jsondata);
var form = document.createElement("form");
form.setAttribute("method", "POST");
form.setAttribute("target", "_self");
form.setAttribute("action", getParameterByName("url"));
var len = count = Object.keys(obj).length;
for (var key in obj) {
var hiddenField1 = document.createElement("input");
hiddenField1.setAttribute("name", key);
hiddenField1.setAttribute("value", obj[key]);
form.appendChild(hiddenField1);
}
form.submit();
document.body.removeChild(form);
</script>
</head>
<body></body>
</html>
现在,用户将被重定向到IPG网站。支付完成(或取消)后,IPG将发送所需数据到您提供的回调地址。
在回调文件中,您需要检查支付是否成功,如果是,则交付产品。如果产品无法交付,您可以撤销支付。
<?php
require_once 'vendor/autoload.php';
use IPG\IPGDatabase;
use IPG\IPGManager;
$man = IPGManager::fromCallback($_REQUEST, new IPGDatabase('user', 'pass', 'db'));
$v = $man->validatePayment($_REQUEST);
if ($v->isValid()) {
// You should deliver the product now
// transactionId is the unique id that you provided in the startPayment method
// and is used to associate a payment transaction to a product order in your database
// you can use this id to find which order this payment is for, and deliver that product
$transactionId = $v->getTransactionId();
if (productDeliverIsSuccessful()) {
// Optionally you can settle the payment, if you dont it will settled automatically
$man->settle($v->getPayId(), $v->getReferenceId());
} else {
// if any error occured upon delivering the product
// you can reverse the payment, which will return the money to the customer
$man->reversal($v->getPayId(), $v->getReferenceId());
}
} else {
// you can get the last error after calling each method
$errorCode = $man->getErrorCode();
$errorMessage = $man->getErrorMessage();
}
// render the result page
添加另一个网关
如果您想使用此库中没有实现的另一个IPG,您应该创建一个新的类,并从IPG\Contract\AbstractIPG
扩展它,并为此特定的IPG提供逻辑。
不使用MySQL或您想使用自己的DB Schema和逻辑
您可以从IPG\Contract\AbstractIPGDatabaseManager
扩展,并提供数据库交互的逻辑。
操作日志
操作日志默认启用,将记录每个方法调用及其输入和输出。如果您希望关闭此功能,应调用setLoggingEnabled
方法。
$man->setLoggingEnabled(false);
许可证
MIT许可证
版权(c)2016 Mohammad Azam Rahmanpour
特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的人进行此类操作,但受以下条件约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他原因,源于、因之或与此软件或软件的使用或其他交易有关。