BitpayX.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Library;
  3. class BitpayX
  4. {
  5. private $bitpayxAppSecret;
  6. private $bitpayxGatewayUri;
  7. /**
  8. * 签名初始化
  9. * @param merKey 签名密钥
  10. */
  11. public function __construct($bitpayxAppSecret)
  12. {
  13. $this->bitpayxAppSecret = $bitpayxAppSecret;
  14. $this->bitpayxGatewayUri = 'https://api.mugglepay.com/v1/';
  15. }
  16. public function prepareSignId($tradeno)
  17. {
  18. $data_sign = array();
  19. $data_sign['merchant_order_id'] = $tradeno;
  20. $data_sign['secret'] = $this->bitpayxAppSecret;
  21. $data_sign['type'] = 'FIAT';
  22. ksort($data_sign);
  23. return http_build_query($data_sign);
  24. }
  25. public function sign($data)
  26. {
  27. return strtolower(md5(md5($data) . $this->bitpayxAppSecret));
  28. }
  29. public function verify($data, $signature)
  30. {
  31. $mySign = $this->sign($data);
  32. return $mySign === $signature;
  33. }
  34. public function mprequest($data)
  35. {
  36. $headers = array('content-type: application/json', 'token: ' . $this->bitpayxAppSecret);
  37. $curl = curl_init();
  38. $url = $this->bitpayxGatewayUri . 'orders';
  39. curl_setopt($curl, CURLOPT_URL, $url);
  40. curl_setopt($curl, CURLOPT_POST, 1);
  41. $data_string = json_encode($data);
  42. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  43. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  44. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  45. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  46. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  47. $data = curl_exec($curl);
  48. curl_close($curl);
  49. return json_decode($data, true);
  50. }
  51. public function mpcheckout($orderId, $data)
  52. {
  53. $headers = array('content-type: application/json', 'token: ' . $this->bitpayxAppSecret);
  54. $curl = curl_init();
  55. $url = $this->bitpayxGatewayUri . 'orders/' . $orderId . '/checkout';
  56. curl_setopt($curl, CURLOPT_URL, $url);
  57. curl_setopt($curl, CURLOPT_POST, 1);
  58. $data_string = json_encode($data);
  59. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  60. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  61. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  62. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  63. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  64. $data = curl_exec($curl);
  65. curl_close($curl);
  66. return json_decode($data, true);
  67. }
  68. public function refund($merchantTradeNo)
  69. {
  70. // TODO
  71. return true;
  72. }
  73. public function buildHtml($params, $method = 'post', $target = '_self')
  74. {
  75. // var_dump($params);exit;
  76. $html = "<form id='submit' name='submit' action='" . $this->gatewayUri . "' method='$method' target='$target'>";
  77. foreach ($params as $key => $value) {
  78. $html .= "<input type='hidden' name='$key' value='$value'/>";
  79. }
  80. $html .= "</form><script>document.forms['submit'].submit();</script>";
  81. return $html;
  82. }
  83. }