Created
December 23, 2017 15:05
-
-
Save xhat/74fa550f182927f4b864df94e5a3dce7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Payjs | |
{ | |
private $url = 'https://payjs.cn/api/native'; | |
private $key = ''; // 填写通信密钥 | |
private $mchid = ''; // 特写商户号 | |
public function __construct($data=null) { | |
$this->data = $data; | |
} | |
public function pay(){ | |
$data = $this->data; | |
$data['mchid'] = $this->mchid; | |
$data['sign'] = $this->sign($data); | |
return $this->post($data, $this->url); | |
} | |
public function post($data, $url) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$rst = curl_exec($ch); | |
curl_close($ch); | |
return $rst; | |
} | |
public function sign(array $attributes) { | |
ksort($attributes); | |
$sign = strtoupper(md5(urldecode(http_build_query($attributes)) . '&key=' . $this->key)); | |
return $sign; | |
} | |
} | |
$arr = [ | |
'body' => 'test', // 订单标题 | |
'out_trade_no' => time(), // 订单号 | |
'total_fee' => 120, // 金额,单位:分 | |
]; | |
$payjs = new Payjs($arr); | |
$rst = $payjs->pay(); | |
print_r($rst); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment