2023-11-17 14:44:01 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
namespace Plugin\Btcpay;
|
2024-04-10 00:51:03 +08:00
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
use App\Services\Plugin\AbstractPlugin;
|
2025-01-21 14:57:54 +08:00
|
|
|
|
use App\Contracts\PaymentInterface;
|
2025-07-26 18:49:58 +08:00
|
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
class Plugin extends AbstractPlugin implements PaymentInterface
|
2024-04-10 00:51:03 +08:00
|
|
|
|
{
|
2025-07-26 18:49:58 +08:00
|
|
|
|
public function boot(): void
|
2024-04-10 00:51:03 +08:00
|
|
|
|
{
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$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' => [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'label' => 'API接口所在网址',
|
|
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => '包含最后的斜杠,例如:https://your-btcpay.com/'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'btcpay_storeId' => [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'label' => 'Store ID',
|
|
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => 'BTCPay商店标识符'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'btcpay_api_key' => [
|
|
|
|
|
|
'label' => 'API KEY',
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'type' => 'string',
|
|
|
|
|
|
'required' => true,
|
|
|
|
|
|
'description' => '个人设置中的API KEY(非商店设置中的)'
|
2023-11-17 14:44:01 +08:00
|
|
|
|
],
|
|
|
|
|
|
'btcpay_webhook_key' => [
|
|
|
|
|
|
'label' => 'WEBHOOK KEY',
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'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
|
2024-04-10 00:51:03 +08:00
|
|
|
|
{
|
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);
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$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);
|
|
|
|
|
|
|
2024-04-10 00:51:03 +08:00
|
|
|
|
if (empty($ret['checkoutLink'])) {
|
2023-12-07 04:01:32 +08:00
|
|
|
|
throw new ApiException("error!");
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
return [
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'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
|
2024-04-10 00:51:03 +08:00
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
$computedSignature = "sha256=" . \hash_hmac('sha256', $payload, $this->getConfig('btcpay_webhook_key'));
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
if (!$this->hashEqual($signraturHeader, $computedSignature)) {
|
2024-04-10 00:51:03 +08:00
|
|
|
|
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',
|
2025-07-26 18:49:58 +08:00
|
|
|
|
'header' => "Authorization:" . "token " . $this->getConfig('btcpay_api_key') . "\r\n"
|
2023-11-17 14:44:01 +08:00
|
|
|
|
)
|
|
|
|
|
|
));
|
|
|
|
|
|
|
2025-07-26 18:49:58 +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"];
|
2024-04-10 00:51:03 +08:00
|
|
|
|
$pay_trade_no = $json_param['invoiceId'];
|
2025-07-26 18:49:58 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
return [
|
|
|
|
|
|
'trade_no' => $out_trade_no,
|
|
|
|
|
|
'callback_no' => $pay_trade_no
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-26 18:49:58 +08:00
|
|
|
|
private function curlPost($url, $params = false)
|
2024-04-10 00:51:03 +08:00
|
|
|
|
{
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$ch = curl_init();
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
2025-05-07 19:48:19 +08:00
|
|
|
|
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(
|
2024-04-10 00:51:03 +08:00
|
|
|
|
$ch,
|
|
|
|
|
|
CURLOPT_HTTPHEADER,
|
2025-07-26 18:49:58 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-26 18:49:58 +08:00
|
|
|
|
}
|