2019-12-27 01:29:08 +08:00
|
|
|
<?php
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
namespace Library;
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
class BitpayX
|
|
|
|
{
|
2019-12-27 01:29:08 +08:00
|
|
|
private $bitpayxAppSecret;
|
|
|
|
private $bitpayxGatewayUri;
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
/**
|
2020-01-11 13:36:52 +08:00
|
|
|
* 签名初始化
|
|
|
|
* @param merKey 签名密钥
|
|
|
|
*/
|
|
|
|
public function __construct($bitpayxAppSecret)
|
|
|
|
{
|
|
|
|
$this->bitpayxAppSecret = $bitpayxAppSecret;
|
|
|
|
$this->bitpayxGatewayUri = 'https://api.mugglepay.com/v1/';
|
|
|
|
}
|
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
public function prepareSignId($tradeno)
|
|
|
|
{
|
|
|
|
$data_sign = array();
|
|
|
|
$data_sign['merchant_order_id'] = $tradeno;
|
|
|
|
$data_sign['secret'] = $this->bitpayxAppSecret;
|
|
|
|
$data_sign['type'] = 'FIAT';
|
|
|
|
ksort($data_sign);
|
|
|
|
return http_build_query($data_sign);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
public function sign($data)
|
|
|
|
{
|
|
|
|
return strtolower(md5(md5($data) . $this->bitpayxAppSecret));
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
public function verify($data, $signature)
|
|
|
|
{
|
|
|
|
$mySign = $this->sign($data);
|
|
|
|
return $mySign === $signature;
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
public function mprequest($data)
|
|
|
|
{
|
|
|
|
$headers = array('content-type: application/json', 'token: ' . $this->bitpayxAppSecret);
|
|
|
|
$curl = curl_init();
|
|
|
|
$url = $this->bitpayxGatewayUri . 'orders';
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
|
$data_string = json_encode($data);
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
$data = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return json_decode($data, true);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-27 01:29:08 +08:00
|
|
|
public function mpcheckout($orderId, $data)
|
|
|
|
{
|
|
|
|
$headers = array('content-type: application/json', 'token: ' . $this->bitpayxAppSecret);
|
|
|
|
$curl = curl_init();
|
|
|
|
$url = $this->bitpayxGatewayUri . 'orders/' . $orderId . '/checkout';
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, 1);
|
|
|
|
$data_string = json_encode($data);
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
$data = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return json_decode($data, true);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
public function refund($merchantTradeNo)
|
|
|
|
{
|
2019-12-27 01:29:08 +08:00
|
|
|
// TODO
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
public function buildHtml($params, $method = 'post', $target = '_self')
|
|
|
|
{
|
2019-12-27 01:29:08 +08:00
|
|
|
// var_dump($params);exit;
|
2020-01-11 13:36:52 +08:00
|
|
|
$html = "<form id='submit' name='submit' action='" . $this->gatewayUri . "' method='$method' target='$target'>";
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$html .= "<input type='hidden' name='$key' value='$value'/>";
|
|
|
|
}
|
|
|
|
$html .= "</form><script>document.forms['submit'].submit();</script>";
|
|
|
|
return $html;
|
2019-12-27 01:29:08 +08:00
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
}
|