Files
Xboard/plugins/Btcpay/Plugin.php

150 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2023-11-17 14:44:01 +08:00
<?php
namespace Plugin\Btcpay;
use App\Services\Plugin\AbstractPlugin;
2025-01-21 14:57:54 +08:00
use App\Contracts\PaymentInterface;
use App\Exceptions\ApiException;
2023-11-17 14:44:01 +08:00
class Plugin extends AbstractPlugin implements PaymentInterface
{
public function boot(): void
{
$this->filter('available_payment_methods', function($methods) {
if ($this->getConfig('enabled', true)) {
$methods['BTCPay'] = [
'name' => $this->getConfig('display_name', 'BTCPay'),
'icon' => $this->getConfig('icon', '₿'),
'plugin_code' => $this->getPluginCode(),
'type' => 'plugin'
];
}
return $methods;
});
2023-11-17 14:44:01 +08:00
}
2025-01-21 14:57:54 +08:00
public function form(): array
2023-11-17 14:44:01 +08:00
{
return [
'btcpay_url' => [
'label' => 'API接口所在网址',
'type' => 'string',
'required' => true,
'description' => '包含最后的斜杠例如https://your-btcpay.com/'
2023-11-17 14:44:01 +08:00
],
'btcpay_storeId' => [
'label' => 'Store ID',
'type' => 'string',
'required' => true,
'description' => 'BTCPay商店标识符'
2023-11-17 14:44:01 +08:00
],
'btcpay_api_key' => [
'label' => 'API KEY',
'type' => 'string',
'required' => true,
'description' => '个人设置中的API KEY(非商店设置中的)'
2023-11-17 14:44:01 +08:00
],
'btcpay_webhook_key' => [
'label' => 'WEBHOOK KEY',
'type' => 'string',
'required' => true,
'description' => 'Webhook通知密钥'
2023-11-17 14:44:01 +08:00
],
];
}
2025-01-21 14:57:54 +08:00
public function pay($order): array
{
2023-11-17 14:44:01 +08:00
$params = [
'jsonResponse' => true,
'amount' => sprintf('%.2f', $order['total_amount'] / 100),
'currency' => 'CNY',
'metadata' => [
'orderId' => $order['trade_no']
]
];
$params_string = @json_encode($params);
$ret_raw = $this->curlPost($this->getConfig('btcpay_url') . 'api/v1/stores/' . $this->getConfig('btcpay_storeId') . '/invoices', $params_string);
2023-11-17 14:44:01 +08:00
$ret = @json_decode($ret_raw, true);
if (empty($ret['checkoutLink'])) {
throw new ApiException("error!");
2023-11-17 14:44:01 +08:00
}
2023-11-17 14:44:01 +08:00
return [
'type' => 1,
2023-11-17 14:44:01 +08:00
'data' => $ret['checkoutLink'],
];
}
2025-01-21 14:57:54 +08:00
public function notify($params): array|bool
{
2025-01-21 14:57:54 +08:00
$payload = trim(request()->getContent());
2023-11-17 14:44:01 +08:00
$headers = getallheaders();
$headerName = 'Btcpay-Sig';
$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : '';
$json_param = json_decode($payload, true);
$computedSignature = "sha256=" . \hash_hmac('sha256', $payload, $this->getConfig('btcpay_webhook_key'));
2023-11-17 14:44:01 +08:00
if (!$this->hashEqual($signraturHeader, $computedSignature)) {
throw new ApiException('HMAC signature does not match', 400);
2023-11-17 14:44:01 +08:00
}
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Authorization:" . "token " . $this->getConfig('btcpay_api_key') . "\r\n"
2023-11-17 14:44:01 +08:00
)
));
$invoiceDetail = file_get_contents($this->getConfig('btcpay_url') . 'api/v1/stores/' . $this->getConfig('btcpay_storeId') . '/invoices/' . $json_param['invoiceId'], false, $context);
2023-11-17 14:44:01 +08:00
$invoiceDetail = json_decode($invoiceDetail, true);
$out_trade_no = $invoiceDetail['metadata']["orderId"];
$pay_trade_no = $json_param['invoiceId'];
2023-11-17 14:44:01 +08:00
return [
'trade_no' => $out_trade_no,
'callback_no' => $pay_trade_no
];
}
private function curlPost($url, $params = false)
{
2023-11-17 14:44:01 +08:00
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2023-11-17 14:44:01 +08:00
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array('Authorization:' . 'token ' . $this->getConfig('btcpay_api_key'), 'Content-Type: application/json')
2023-11-17 14:44:01 +08:00
);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
private function hashEqual($str1, $str2)
{
if (function_exists('hash_equals')) {
return \hash_equals($str1, $str2);
}
if (strlen($str1) != strlen($str2)) {
return false;
} else {
$res = $str1 ^ $str2;
$ret = 0;
for ($i = strlen($res) - 1; $i >= 0; $i--) {
$ret |= ord($res[$i]);
}
return !$ret;
}
}
}