<?php
namespace WeChat\Contracts;
use WeChat\Exceptions\InvalidArgumentException;
use WeChat\Exceptions\InvalidResponseException;
class BasicPay
{
protected $config;
protected $params;
public function __construct(array $options)
{
if (empty($options['appid'])) {
throw new InvalidArgumentException("Missing Config -- [appid]");
}
if (empty($options['mch_id'])) {
throw new InvalidArgumentException("Missing Config -- [mch_id]");
}
if (empty($options['mch_key'])) {
throw new InvalidArgumentException("Missing Config -- [mch_key]");
}
if (!empty($options['cache_path'])) {
Tools::$cache_path = $options['cache_path'];
}
$this->config = new DataArray($options);
$this->params = new DataArray([
'appid' => $this->config->get('appid'),
'mch_id' => $this->config->get('mch_id'),
'nonce_str' => Tools::createNoncestr(),
]);
if ($this->config->get('sub_appid')) {
$this->params->set('sub_appid', $this->config->get('sub_appid'));
}
if ($this->config->get('sub_mch_id')) {
$this->params->set('sub_mch_id', $this->config->get('sub_mch_id'));
}
}
public function getNotify()
{
$data = Tools::xml2arr(file_get_contents('php://input'));
if (!empty($data['sign'])) {
if ($this->getPaySign($data) === $data['sign']) {
return $data;
}
}
throw new InvalidResponseException('Invalid Notify.', '0');
}
public function getPaySign(array $data, $signType = 'MD5', $buff = '')
{
unset($data['sign']);
ksort($data);
foreach ($data as $k => $v) {
$buff .= "{$k}={$v}&";
}
$buff .= ("key=" . $this->config->get('mch_key'));
if (strtoupper($signType) === 'MD5') {
return strtoupper(md5($buff));
}
return strtoupper(hash_hmac('SHA256', $buff, $this->config->get('mch_key')));
}
public function shortUrl($longUrl)
{
$url = 'https://api.mch.weixin.qq.com/tools/shorturl';
return $this->callPostApi($url, ['long_url' => $longUrl]);
}
protected function callPostApi($url, array $data, $isCert = false, $signType = 'HMAC-SHA256', $needSignType = true)
{
$option = [];
if ($isCert) {
$option['ssl_cer'] = $this->config->get('ssl_cer');
$option['ssl_key'] = $this->config->get('ssl_key');
if (empty($option['ssl_cer']) || !file_exists($option['ssl_cer']))
throw new InvalidArgumentException("Missing Config -- ssl_cer", '0');
if (empty($option['ssl_key']) || !file_exists($option['ssl_key']))
throw new InvalidArgumentException("Missing Config -- ssl_key", '0');
}
$params = $this->params->merge($data);
$needSignType && ($params['sign_type'] = strtoupper($signType));
$params['sign'] = $this->getPaySign($params, $signType);
$result = Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option));
if ($result['return_code'] !== 'SUCCESS') {
throw new InvalidResponseException($result['return_msg'], '0');
}
return $result;
}
}