BitpayX.php 2.9 KB

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