class UnionScanQrCode{
protected $appId;
protected $appKey;
protected $mid;
protected $tid;
protected $fromCode;
protected $authorization = 'OPEN-FORM-PARAM';
protected $instMid = 'QRPAYDEFAULT';//扫码业务
protected $nonce;
protected $timestamp;
protected $qrCodeId;
protected $requestTimestamp;
protected $local_url = 'http://58.247.0.18:29015';//测试环境
protected $produce_url = 'https://api-mop.chinaums.com';//生产环境
protected $pay_url = '/v1/netpay/bills/get-qrcode';
protected $close_url = '/v1/netpay/bills/close-qrcode';
protected $query_url = '/v1/netpay/bills/query';
protected $query_info_url = '/v1/netpay/bills/query-qrcode-info';
protected $url;
//,$appId,$appKey,$mid,$tid,$fromCode
public function __construct($local,$appId,$appKey,$mid,$fromCode,$tid)
{
$this->nonce = (string)rand();
$this->timestamp = date('YmdHis');
$this->requestTimestamp = date('Y-m-d H:i:s');
//$this->qrCodeId = $this->generateCode();
if ($local){
$this->url = $this->local_url;
$this->appId = '10037e6f6a4e6da4016a6284b4de000b';
$this->appKey = '1eb243a54d1349098af435ae8fac290d';
$this->mid = '898340149000035';
$this->tid = '38557688';
$this->fromCode = '1017';
}else{
$this->url = $this->produce_url;
$this->appId = $appId;
$this->appKey = $appKey;
$this->mid = $mid;
$this->fromCode = $fromCode;
$this->tid = $tid;
}
}
public function getUnionUrl($money,$qrCode,$notify_url,$order_sn)
{
$content = $this->generateData($money,$qrCode,$notify_url,$order_sn);
$header = [
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($content),
'Authorization:'.$this->auth($content),
];
$url=$this->url.$this->pay_url;
return ['url'=>$url,'content'=>$content,'header'=>$header];
}
public function getCloseUrl($qrCode)
{
$content = $this->generateCloseData($qrCode);
$header = [
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($content),
'Authorization:'.$this->auth($content),
];
$url=$this->url.$this->close_url;
return ['url'=>$url,'content'=>$content,'header'=>$header];
}
public function getQueryUrl($order_sn,$pay_time)
{
$content = $this->generateQueryData($order_sn,$pay_time);
$header = [
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($content),
'Authorization:'.$this->auth($content),
];
$url=$this->url.$this->query_url;
return ['url'=>$url,'content'=>$content,'header'=>$header];
}
public function getQueryInfoUrl($order_sn)
{
$content = $this->generateQueryInfoData($order_sn);
$header = [
'Content-Type: application/json; charset=utf-8',
'Content-Length:' . strlen($content),
'Authorization:'.$this->auth($content),
];
$url=$this->url.$this->query_info_url;
return ['url'=>$url,'content'=>$content,'header'=>$header];
}
protected function signature($content){
$str = bin2hex(hash('sha256', $content, true));
return base64_encode(hash_hmac('sha256',"$this->appId$this->timestamp$this->nonce$str", $this->appKey, true));
}
protected function auth($content)
{
$signature = $this->signature($content);
return 'OPEN-BODY-SIG AppId="'.$this->appId.'", Timestamp="'.$this->timestamp.'", Nonce="'.$this->nonce.'", Signature="'.$signature.'"';
}
public function generateData($money,$qrCode,$notify_url,$order_sn)
{
$data = [
"requestTimestamp" => $this->requestTimestamp,
"qrCodeId" => $order_sn,
'billNo' => $qrCode,
'billData' => date("Y-m-d",time()),
"mid" => $this->mid,
"tid" => $this->tid,
"instMid" => $this->instMid,
"totalAmount" => $money,
'notifyUrl' => $notify_url,
];
return json_encode($data,true);
}
public function generateCloseData($qrCode)
{
$data = [
'qrCodeId' => $qrCode,
'requestTimestamp' => $this->requestTimestamp,
'mid' => $this->mid,
'tid' => $this->tid,
'instMid' => $this->instMid,
];
return json_encode($data,true);
}
private function generateQueryData($order_sn,$pay_time)
{
$data = [
'requestTimestamp' => $this->requestTimestamp,
'mid' => $this->mid,
'tid' => $this->tid,
'instMid' => $this->instMid,
'billNo' => $order_sn,
'billDate' => date("Y-m-d",$pay_time)
];
return json_encode($data,true);
}
private function generateQueryInfoData($order_sn)
{
$data = [
'requestTimestamp' => $this->requestTimestamp,
'qrCodeId' => $order_sn,
];
return json_encode($data,true);
}
private function generateCode()
{
return $this->sn($this->fromCode);
}
}