485 lines
8.8 KiB
PHP
485 lines
8.8 KiB
PHP
<?php
|
|
|
|
|
|
namespace wchat\qq;
|
|
|
|
use Kiri\Client;
|
|
use wchat\common\Help;
|
|
use wchat\common\Result;
|
|
|
|
class Redhat extends SmallProgram
|
|
{
|
|
|
|
private string $charset = 'UTF8';
|
|
private string $nonce_str = '';
|
|
private string $sign = '';
|
|
private string $mch_billno = '';
|
|
private string $mch_id = '';
|
|
private string $mch_name = '';
|
|
private string $qqappid = '';
|
|
private string $re_openid = '';
|
|
private string $total_amount = '';
|
|
private string $total_num = '';
|
|
private string $wishing = '';
|
|
private string $act_name = '';
|
|
private string $icon_id = '';
|
|
private string $banner_id = '';
|
|
private string $notify_url = '';
|
|
private string $not_send_msg = '';
|
|
private string $min_value = '';
|
|
private string $max_value = '';
|
|
private string $key = '';
|
|
private string $signType = '';
|
|
|
|
|
|
private string $sendUrl = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
|
|
private string $searchUrl = '/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getCharset(): string
|
|
{
|
|
return $this->charset;
|
|
}
|
|
|
|
/**
|
|
* @param string $charset
|
|
*/
|
|
public function setCharset(string $charset): void
|
|
{
|
|
$this->charset = $charset;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getNonceStr(): string
|
|
{
|
|
return $this->nonce_str;
|
|
}
|
|
|
|
/**
|
|
* @param string $nonce_str
|
|
*/
|
|
public function setNonceStr(string $nonce_str): void
|
|
{
|
|
$this->nonce_str = $nonce_str;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getSign(): string
|
|
{
|
|
return $this->sign;
|
|
}
|
|
|
|
/**
|
|
* @param string $sign
|
|
*/
|
|
public function setSign(string $sign): void
|
|
{
|
|
$this->sign = $sign;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getMchBillno(): string
|
|
{
|
|
return $this->mch_billno;
|
|
}
|
|
|
|
/**
|
|
* @param string $mch_billno
|
|
*/
|
|
public function setMchBillno(string $mch_billno): void
|
|
{
|
|
$this->mch_billno = $mch_billno;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getMchId(): string
|
|
{
|
|
return $this->mch_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $mch_id
|
|
*/
|
|
public function setMchId(string $mch_id): void
|
|
{
|
|
$this->mch_id = $mch_id;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getMchName(): string
|
|
{
|
|
return $this->mch_name;
|
|
}
|
|
|
|
/**
|
|
* @param string $mch_name
|
|
*/
|
|
public function setMchName(string $mch_name): void
|
|
{
|
|
$this->mch_name = $mch_name;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getQqappid(): string
|
|
{
|
|
return $this->qqappid;
|
|
}
|
|
|
|
/**
|
|
* @param string $qqappid
|
|
*/
|
|
public function setQqappid(string $qqappid): void
|
|
{
|
|
$this->qqappid = $qqappid;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getReOpenid(): string
|
|
{
|
|
return $this->re_openid;
|
|
}
|
|
|
|
/**
|
|
* @param string $re_openid
|
|
*/
|
|
public function setReOpenid(string $re_openid): void
|
|
{
|
|
$this->re_openid = $re_openid;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getTotalAmount(): string
|
|
{
|
|
return $this->total_amount;
|
|
}
|
|
|
|
/**
|
|
* @param string $total_amount
|
|
*/
|
|
public function setTotalAmount(string $total_amount): void
|
|
{
|
|
$this->total_amount = $total_amount;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getTotalNum(): string
|
|
{
|
|
return $this->total_num;
|
|
}
|
|
|
|
/**
|
|
* @param string $total_num
|
|
*/
|
|
public function setTotalNum(string $total_num): void
|
|
{
|
|
$this->total_num = $total_num;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getWishing(): string
|
|
{
|
|
return $this->wishing;
|
|
}
|
|
|
|
/**
|
|
* @param string $wishing
|
|
*/
|
|
public function setWishing(string $wishing): void
|
|
{
|
|
$this->wishing = $wishing;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getActName(): string
|
|
{
|
|
return $this->act_name;
|
|
}
|
|
|
|
/**
|
|
* @param string $act_name
|
|
*/
|
|
public function setActName(string $act_name): void
|
|
{
|
|
$this->act_name = $act_name;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getIconId(): string
|
|
{
|
|
return $this->icon_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $icon_id
|
|
*/
|
|
public function setIconId(string $icon_id): void
|
|
{
|
|
$this->icon_id = $icon_id;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getBannerId(): string
|
|
{
|
|
return $this->banner_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $banner_id
|
|
*/
|
|
public function setBannerId(string $banner_id): void
|
|
{
|
|
$this->banner_id = $banner_id;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getNotifyUrl(): string
|
|
{
|
|
return $this->notify_url;
|
|
}
|
|
|
|
/**
|
|
* @param string $notify_url
|
|
*/
|
|
public function setNotifyUrl(string $notify_url): void
|
|
{
|
|
$this->notify_url = $notify_url;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getNotSendMsg(): string
|
|
{
|
|
return $this->not_send_msg;
|
|
}
|
|
|
|
/**
|
|
* @param string $not_send_msg
|
|
*/
|
|
public function setNotSendMsg(string $not_send_msg): void
|
|
{
|
|
$this->not_send_msg = $not_send_msg;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getMinValue(): string
|
|
{
|
|
return $this->min_value;
|
|
}
|
|
|
|
/**
|
|
* @param string $min_value
|
|
*/
|
|
public function setMinValue(string $min_value): void
|
|
{
|
|
$this->min_value = $min_value;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getMaxValue(): string
|
|
{
|
|
return $this->max_value;
|
|
}
|
|
|
|
/**
|
|
* @param string $max_value
|
|
*/
|
|
public function setMaxValue(string $max_value): void
|
|
{
|
|
$this->max_value = $max_value;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getKey(): string
|
|
{
|
|
return $this->key;
|
|
}
|
|
|
|
/**
|
|
* @param string $key
|
|
*/
|
|
public function setKey(string $key): void
|
|
{
|
|
$this->key = $key;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getSignType(): string
|
|
{
|
|
return $this->signType;
|
|
}
|
|
|
|
/**
|
|
* @param string $signType
|
|
*/
|
|
public function setSignType(string $signType): void
|
|
{
|
|
$this->signType = $signType;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getSendUrl(): string
|
|
{
|
|
return $this->sendUrl;
|
|
}
|
|
|
|
/**
|
|
* @param string $sendUrl
|
|
*/
|
|
public function setSendUrl(string $sendUrl): void
|
|
{
|
|
$this->sendUrl = $sendUrl;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getSearchUrl(): string
|
|
{
|
|
return $this->searchUrl;
|
|
}
|
|
|
|
/**
|
|
* @param string $searchUrl
|
|
*/
|
|
public function setSearchUrl(string $searchUrl): void
|
|
{
|
|
$this->searchUrl = $searchUrl;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return mixed
|
|
* 构建请求参数
|
|
*/
|
|
private function generate(): array
|
|
{
|
|
$requestParam = [];
|
|
$requestParam['charset'] = $this->getCharset();
|
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
|
$requestParam['mch_billno'] = $this->getMchBillno();
|
|
$requestParam['mch_id'] = $this->getMchId();
|
|
$requestParam['mch_name'] = $this->getMchName();
|
|
$requestParam['qqappid'] = $this->getQqappid();
|
|
$requestParam['re_openid'] = $this->getReOpenid();
|
|
$requestParam['total_amount'] = $this->getTotalAmount() * 100;
|
|
$requestParam['min_value'] = $this->getMinValue() * 100;
|
|
$requestParam['max_value'] = $this->getMaxValue() * 100;
|
|
$requestParam['total_num'] = $this->getTotalNum();
|
|
$requestParam['wishing'] = $this->getWishing();
|
|
$requestParam['act_name'] = $this->getActName();
|
|
$requestParam['icon_id'] = $this->getIconId();
|
|
$requestParam['notify_url'] = $this->getNotifyUrl();
|
|
|
|
$requestParam['sign'] = $this->builderSign($requestParam);
|
|
|
|
return $requestParam;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $requestParam
|
|
* @return string
|
|
*/
|
|
private function builderSign($requestParam): string
|
|
{
|
|
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
|
|
}
|
|
|
|
/**
|
|
* @return Result
|
|
*/
|
|
public function redEnvelopes(): Result
|
|
{
|
|
$client = new Client('api.qpay.qq.com', 443, true);
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
$client->withSslKeyFile($this->config->getSslKey());
|
|
$client->withSslCertFile($this->config->getSslCert());
|
|
$client->post($this->sendUrl, $this->generate());
|
|
$client->close();
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
return new Result(code: 505, message: 'network error.');
|
|
}
|
|
$json = json_decode($client->getBody(), true);
|
|
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
|
|
return new Result(code: 500, message: $json['return_msg'] ?? $json['retmsg']);
|
|
}
|
|
if ($json['retcode'] != 0) {
|
|
return new Result(code: $json['retcode'], message: $json['retmsg']);
|
|
} else {
|
|
return new Result(code: 0, data: $json);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $listid
|
|
* @param $orderNo
|
|
* @return Result
|
|
*/
|
|
public function searchByOrderNo($listid, $orderNo = null): Result
|
|
{
|
|
$requestParam['nonce_str'] = $this->getNonceStr();
|
|
$requestParam['mch_id'] = $this->getMchId();
|
|
$requestParam['listid'] = $listid;
|
|
if (!empty($orderNo)) {
|
|
$requestParam['mch_billno'] = $orderNo;
|
|
}
|
|
$requestParam['sign'] = $this->builderSign($requestParam);
|
|
$client = new Client('qpay.qq.com', 443, true);
|
|
$client->withHeader(['Content-Type' => 'application/json']);
|
|
$client->post($this->searchUrl, $requestParam);
|
|
$client->close();
|
|
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
|
return new Result(code: 505, message: 'network error.');
|
|
}
|
|
$json = json_decode($client->getBody(), true);
|
|
if (isset($json['result']) && $json['result'] != 'SUCCESS') {
|
|
return new Result(code: 500, message: $response['res_info'] ?? '订单查询失败!');
|
|
} else {
|
|
return new Result(code: 0, data: $json);
|
|
}
|
|
}
|
|
|
|
}
|