文件
mashangyoujiu/Application/Addons/Pay/WxPayV3.class.php

296 行
9.7 KiB
PHP
可执行文件

<?php
namespace Addons\Pay;
//[1.5.52]
class WxPayV3
{
protected $authorization = 'WECHATPAY2-SHA256-RSA2048'; //认证类型
protected $method = "POST";
protected $url; //链接
protected $mch_id; //商户号
protected $nonce_str; //随机字符串
protected $sign; //签名
protected $timestamp; //时间
protected $serial_no; //商户Api证书序列号
protected $apiclient_key; //私钥地址
protected $apiclient_cert; //公钥地址
protected $token; //Token
protected $data; //发送参数
protected $stock_id; //批次号
protected $response; //返回信息
protected $image_type; //图片类型
protected $boundary; //边界符
protected $suffix; //图片后缀
protected $openid; //发放的openid
protected $file; //图片信息
public function __construct($param)
{
$this->mch_id = isset($param['mch_id']) ? $param['mch_id'] : '';
$this->data = isset($param['data']) ? $param['data'] : '';
$this->stock_id = isset($param['stock_id']) ? $param['stock_id'] : '';
$this->serial_no = isset($param['serial_no']) ? $param['serial_no'] : '';
$this->apiclient_key = isset($param['apiclient_key']) ? $param['apiclient_key'] : '';
$this->image_type = isset($param['image_type']) ? $param['image_type'] : '';
$this->boundary = isset($param['boundary']) ? $param['boundary'] : '';
$this->openid = isset($param['openid']) ? $param['openid'] : '';
$this->file = isset($param['file']) ? $param['file'] : '';
}
/**
* 上传图片
* @return bool|string
*/
public function upload()
{
$this->url = 'https://api.mch.weixin.qq.com/v3/marketing/favor/media/image-upload';
$result = $this->uploadRequestAction();
return $result;
}
/**
* 图片上传请求
* @return bool|string
*/
public function uploadRequestAction()
{
if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
}
$headerParam = $this->uploadHeaderParam(); //获取头部信息
$boundarystr = "--{$this->boundary}\r\n";// $out是post的内容
$str = $boundarystr;
$str .= 'Content-Disposition: form-data; name="meta"' . "\r\n";
$str .= 'Content-Type: application/json' . "\r\n";
$str .= "\r\n";
$str .= json_encode($this->data['meta']) . "\r\n";
$str .= $boundarystr;
$str .= 'Content-Disposition: form-data; name="file"; filename="' . $this->data['meta']['filename'] . '"' . "\r\n";
$str .= 'Content-Type: ' . $this->image_type . ";\r\n";
$str .= "\r\n";
$str .= $this->data['file'] . "\r\n";
$str .= $boundarystr . "--\r\n";
// print_r($str);exit;
$this->response = $this->http_Request($this->url, $headerParam, $str);
return $this->response;
}
/**
* 图片上传头部参数
* @return array
*/
public function uploadHeaderParam()
{
$this->getUploadSign(); //生成签名
$this->getToken(); //生成Token
$header = [
"Content-Type: multipart/form-data;name='meta'",
"Content-Type: application/json",
"User-Agent:" . $_SERVER['HTTP_USER_AGENT'],
'Authorization: ' . $this->authorization . ' ' . $this->token,
"Content-Type: multipart/form-data;boundary=" . $this->boundary
];
return $header;
}
/**
* 图片生成签名
*/
protected function getUploadSign()
{
$url_parts = parse_url($this->url); //链接
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$this->timestamp = time();
$this->nonce_str = createKey(); //随机字符串
$message = $this->method . "\n" .
$canonical_url . "\n" .
$this->timestamp . "\n" .
$this->nonce_str . "\n" .
json_encode($this->data['meta']) . "\n";
openssl_sign($message, $raw_sign, openssl_get_privatekey(file_get_contents($this->apiclient_key)), 'sha256WithRSAEncryption');
$this->sign = base64_encode($raw_sign);
}
/**
* 核销商家券
* @return bool|string
*/
public function verificationCoupon()
{
$this->url = 'https://api.mch.weixin.qq.com/v3/marketing/busifavor/coupons/use'; //创建代金券请求链接
$result = $this->requestAction();
return $result;
}
/**
* 创建商家券
*/
public function createShopCoupon()
{
$this->url = 'https://api.mch.weixin.qq.com/v3/marketing/busifavor/stocks'; //创建代金券请求链接
$result = $this->requestAction();
return $result;
}
/* public function searchPayCoupon()
{
$this->url = 'https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/15067678'; //创建代金券请求链接
$this->method = "GET";
$result = $this->requestAction();
return $result;
}*/
/**
* 创建代金券
* @return bool|string
*/
public function createCoupon()
{
$this->url = 'https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks'; //创建代金券请求链接
$result = $this->requestAction();
return $result;
}
/**
* 激活代金券
* @return bool|string
*/
public function activateCoupon()
{
$this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/start";
$result = $this->requestAction();
return $result;
}
/**
* 发放代金券
*/
public function grant()
{
$this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/users/{$this->openid}/coupons";
$result = $this->requestAction();
return $result;
}
/**
* 暂停代金券
*/
public function suspend()
{
$this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/pause";
$result = $this->requestAction();
return $result;
}
/**
* 重启代金券
*/
public function restart()
{
$this->url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/{$this->stock_id}/restart";
$result = $this->requestAction();
return $result;
}
/**
* 发送请求
* @return bool|string
*/
protected function requestAction()
{
if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
}
$headerParam = $this->getHeaderParam(); //获取头部信息
$this->response = $this->http_Request($this->url, $headerParam, json_encode($this->data));
return $this->response;
}
/**
* 获取请求头部参数
* @return array
*/
protected function getHeaderParam()
{
$this->getSign(); //生成签名
$this->getToken(); //生成Token
$header = [
"Content-type: application/json;charset='utf-8'",
"Accept:application/json",
"User-Agent:*/*",
'Authorization: ' . $this->authorization . ' ' . $this->token,
];
return $header;
}
/**
* 生成签名
*/
protected function getSign()
{
$url_parts = parse_url($this->url); //链接
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$this->timestamp = time();
$this->nonce_str = createKey(); //随机字符串
$message = $this->method . "\n" .
$canonical_url . "\n" .
$this->timestamp . "\n" .
$this->nonce_str . "\n" .
json_encode($this->data) . "\n";
openssl_sign($message, $raw_sign, openssl_get_privatekey(file_get_contents($this->apiclient_key)), 'sha256WithRSAEncryption');
$this->sign = base64_encode($raw_sign);
}
/**
* 生成Token
* @return string
*/
protected function getToken()
{
$this->token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
$this->mch_id, $this->nonce_str, $this->timestamp, $this->serial_no, $this->sign);
}
/**
* 数据请求
* @param $url
* @param array $header 获取头部
* @param string $post_data POST数据,不填写默认以GET方式请求
* @return bool|string
*/
public function http_Request($url, $header = array(), $post_data = "")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 2);
if ($post_data != "") {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //设置post提交数据
}
//判断当前是不是有post数据的发
$response = curl_exec($ch);
if ($response === FALSE) {
$response = "curl 错误信息: " . curl_error($ch);
}
curl_close($ch);
return $response;
}
}