BitpayX.php 2.9 KB

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