Skip to content

Instantly share code, notes, and snippets.

@payks
Created April 3, 2019 13:41
Show Gist options
  • Save payks/7c2389ee1d0fe9110ee192ff510b43aa to your computer and use it in GitHub Desktop.
Save payks/7c2389ee1d0fe9110ee192ff510b43aa to your computer and use it in GitHub Desktop.
payks微信支付php类
<?php
class Payks
{
private $mch_id = '';
private $mch_key = '';
public function __construct($mch_id, $mch_key) {
$this->mch_id = $mch_id;
$this->mch_key = $mch_key;
}
//扫码支付
public function native($data){
$data['mch_id'] = $this->mch_id;
$data['sign'] = $this->sign($data);
return $this->post('https://payks.com/api/native', $data);
}
//jsapi支付
public function jsapi($data){
$data['mch_id'] = $this->mch_id;
$data['sign'] = $this->sign($data);
return $this->post('https://payks.com/api/jsapi', $data);
}
//收银台支付
public function cashier($data){
$data['mch_id'] = $this->mch_id;
$data['sign'] = $this->sign($data);
return 'https://payks.com/api/cashier?' .http_build_query($data);
}
//退款
public function refund($order_id){
$data['mch_id'] = $this->mch_id;
$data['out_trade_no'] = $order_id;
$data['sign'] = $this->sign($data);
return $this->post('https://payks.com/api/refund', $data);
}
public function post($url, $data) {
$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);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$res = curl_exec($ch);
curl_close($ch);
try{
$json = json_decode($res, true);
return $json;
}catch(Exception $e){
return array('ret' => 1, 'msg' => $e->getMessage());
}
}
public function verify(){
$post = $_POST;
if(isset($post['sign']) && isset($post['mch_id'])){
$sign = $post['sign'];
unset($post['sign']);
if($sign === $this->sign($post)){
return $_POST;
}else{
return false;
}
}
return false;
}
public function sign($attributes) {
ksort($attributes);
$sign = md5(urldecode(http_build_query($attributes)) . '&key=' . $this->mch_key);
return $sign;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment