BTCPay.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Payments;
  3. class BTCPay {
  4. public function __construct($config) {
  5. $this->config = $config;
  6. }
  7. public function form()
  8. {
  9. return [
  10. 'btcpay_url' => [
  11. 'label' => 'API接口所在网址(包含最后的斜杠)',
  12. 'description' => '',
  13. 'type' => 'input',
  14. ],
  15. 'btcpay_storeId' => [
  16. 'label' => 'storeId',
  17. 'description' => '',
  18. 'type' => 'input',
  19. ],
  20. 'btcpay_api_key' => [
  21. 'label' => 'API KEY',
  22. 'description' => '个人设置中的API KEY(非商店设置中的)',
  23. 'type' => 'input',
  24. ],
  25. 'btcpay_webhook_key' => [
  26. 'label' => 'WEBHOOK KEY',
  27. 'description' => '',
  28. 'type' => 'input',
  29. ],
  30. ];
  31. }
  32. public function pay($order) {
  33. $params = [
  34. 'jsonResponse' => true,
  35. 'amount' => sprintf('%.2f', $order['total_amount'] / 100),
  36. 'currency' => 'CNY',
  37. 'metadata' => [
  38. 'orderId' => $order['trade_no']
  39. ]
  40. ];
  41. $params_string = @json_encode($params);
  42. $ret_raw = self::_curlPost($this->config['btcpay_url'] . 'api/v1/stores/' . $this->config['btcpay_storeId'] . '/invoices', $params_string);
  43. $ret = @json_decode($ret_raw, true);
  44. if(empty($ret['checkoutLink'])) {
  45. abort(500, "error!");
  46. }
  47. return [
  48. 'type' => 1, // Redirect to url
  49. 'data' => $ret['checkoutLink'],
  50. ];
  51. }
  52. public function notify($params) {
  53. $payload = trim(file_get_contents('php://input'));
  54. $headers = getallheaders();
  55. //IS Btcpay-Sig
  56. //NOT BTCPay-Sig
  57. //API doc is WRONG!
  58. $headerName = 'Btcpay-Sig';
  59. $signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : '';
  60. $json_param = json_decode($payload, true);
  61. $computedSignature = "sha256=" . \hash_hmac('sha256', $payload, $this->config['btcpay_webhook_key']);
  62. if (!self::hashEqual($signraturHeader, $computedSignature)) {
  63. abort(400, 'HMAC signature does not match');
  64. return false;
  65. }
  66. //get order id store in metadata
  67. $context = stream_context_create(array(
  68. 'http' => array(
  69. 'method' => 'GET',
  70. 'header' => "Authorization:" . "token " . $this->config['btcpay_api_key'] . "\r\n"
  71. )
  72. ));
  73. $invoiceDetail = file_get_contents($this->config['btcpay_url'] . 'api/v1/stores/' . $this->config['btcpay_storeId'] . '/invoices/' . $json_param['invoiceId'], false, $context);
  74. $invoiceDetail = json_decode($invoiceDetail, true);
  75. $out_trade_no = $invoiceDetail['metadata']["orderId"];
  76. $pay_trade_no=$json_param['invoiceId'];
  77. return [
  78. 'trade_no' => $out_trade_no,
  79. 'callback_no' => $pay_trade_no
  80. ];
  81. http_response_code(200);
  82. die('success');
  83. }
  84. private function _curlPost($url,$params=false){
  85. $ch = curl_init();
  86. curl_setopt($ch, CURLOPT_URL, $url);
  87. curl_setopt($ch, CURLOPT_HEADER, 0);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  90. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  91. curl_setopt(
  92. $ch, CURLOPT_HTTPHEADER, array('Authorization:' .'token '.$this->config['btcpay_api_key'], 'Content-Type: application/json')
  93. );
  94. $result = curl_exec($ch);
  95. curl_close($ch);
  96. return $result;
  97. }
  98. /**
  99. * @param string $str1
  100. * @param string $str2
  101. * @return bool
  102. */
  103. private function hashEqual($str1, $str2)
  104. {
  105. if (function_exists('hash_equals')) {
  106. return \hash_equals($str1, $str2);
  107. }
  108. if (strlen($str1) != strlen($str2)) {
  109. return false;
  110. } else {
  111. $res = $str1 ^ $str2;
  112. $ret = 0;
  113. for ($i = strlen($res) - 1; $i >= 0; $i--) {
  114. $ret |= ord($res[$i]);
  115. }
  116. return !$ret;
  117. }
  118. }
  119. }