72 行
2.3 KiB
PHP
72 行
2.3 KiB
PHP
<?php
|
|
if (!defined('IN_APP')) exit('Forbidden');
|
|
require_once __DIR__ . '/PaymentAlipay.php';
|
|
require_once __DIR__ . '/PaymentWechat.php';
|
|
interface PaymentGatewayInterface
|
|
{
|
|
public function createOrder(array $order);
|
|
public function verifyNotify(array $params);
|
|
public function getTradeNo(array $params);
|
|
public function isPaid(array $params);
|
|
public function respondSuccess();
|
|
public function getName();
|
|
}
|
|
class Payment
|
|
{
|
|
private static $gateways = [];
|
|
public static function register($code, PaymentGatewayInterface $gateway)
|
|
{
|
|
self::$gateways[$code] = $gateway;
|
|
}
|
|
public static function get($code)
|
|
{
|
|
return self::$gateways[$code] ?? null;
|
|
}
|
|
public static function availableMethods()
|
|
{
|
|
$list = [];
|
|
foreach (self::$gateways as $code => $gw) {
|
|
$list[] = [
|
|
'code' => $code,
|
|
'name' => $gw->getName(),
|
|
'enabled' => true,
|
|
];
|
|
}
|
|
return $list;
|
|
}
|
|
public static function initGateways()
|
|
{
|
|
$alipayEnabled = getSetting('pay_alipay_enabled', '0') === '1';
|
|
$wechatEnabled = getSetting('pay_wechat_enabled', '0') === '1';
|
|
if ($alipayEnabled) {
|
|
self::register('alipay', new AlipayGateway());
|
|
}
|
|
if ($wechatEnabled) {
|
|
self::register('wechat', new WechatGateway());
|
|
}
|
|
if (getSetting('pay_codepay_enabled', '0') === '1') {
|
|
require_once __DIR__ . '/PaymentCodePay.php';
|
|
self::register('codepay', new CodePayGateway());
|
|
}
|
|
}
|
|
public static function createPayLog($orderId, $method, $amount, $tradeNo = '')
|
|
{
|
|
db()->prepare(
|
|
'INSERT INTO ' . tn('pay_logs') . ' (order_id,pay_method,amount,trade_no,status,created_at) VALUES (?,?,?,?,?,NOW())'
|
|
)->execute([$orderId, $method, $amount, $tradeNo, 'pending']);
|
|
return (int)db()->lastInsertId();
|
|
}
|
|
public static function updatePayStatus($logId, $status, $tradeNo = '')
|
|
{
|
|
$sql = 'UPDATE ' . tn('pay_logs') . ' SET status=?, updated_at=NOW()';
|
|
$params = [$status];
|
|
if ($tradeNo) {
|
|
$sql .= ', trade_no=?';
|
|
$params[] = $tradeNo;
|
|
}
|
|
$sql .= ' WHERE id=?';
|
|
$params[] = $logId;
|
|
db()->prepare($sql)->execute($params);
|
|
}
|
|
}
|