Merge pull request #82 from krissss/master

对微信服务商的支持
This commit is contained in:
yansongda 2018-03-06 10:38:05 +08:00 committed by GitHub
commit ac303d9d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 4 deletions

View File

@ -27,6 +27,11 @@ use Yansongda\Supports\Str;
*/
class Wechat implements GatewayApplicationInterface
{
const MODE_NORMAL = 'normal'; // 普通模式
const MODE_DEV = 'dev'; // 沙箱模式
const MODE_HK = 'hk'; // 香港钱包
const MODE_SERVICE = 'service'; // 服务商
/**
* Config.
*
@ -34,6 +39,13 @@ class Wechat implements GatewayApplicationInterface
*/
protected $config;
/**
* Mode.
*
* @var string
*/
protected $mode;
/**
* Wechat payload.
*
@ -58,7 +70,8 @@ class Wechat implements GatewayApplicationInterface
public function __construct(Config $config)
{
$this->config = $config;
$this->gateway = Support::baseUri($this->config->get('mode', 'normal'));
$this->mode = $this->config->get('mode', self::MODE_NORMAL);
$this->gateway = Support::baseUri($this->mode);
$this->payload = [
'appid' => $this->config->get('app_id', ''),
'mch_id' => $this->config->get('mch_id', ''),
@ -68,6 +81,13 @@ class Wechat implements GatewayApplicationInterface
'trade_type' => '',
'spbill_create_ip' => Request::createFromGlobals()->getClientIp(),
];
if ($this->mode === static::MODE_SERVICE) {
$this->payload = array_merge($this->payload, [
'sub_mch_id' => $this->config->get('sub_mch_id'),
'sub_appid' => $this->config->get('sub_app_id', ''),
]);
}
}
/**

View File

@ -4,6 +4,7 @@ namespace Yansongda\Pay\Gateways\Wechat;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Str;
@ -23,10 +24,13 @@ class AppGateway extends Gateway
{
$payload['appid'] = $this->config->get('appid');
$payload['trade_type'] = $this->getTradeType();
if ($this->mode === Wechat::MODE_SERVICE) {
$payload['sub_appid'] = $this->config->get('sub_appid');
}
$payRequest = [
'appid' => $payload['appid'],
'partnerid' => $payload['mch_id'],
'partnerid' => $this->mode === Wechat::MODE_SERVICE ? $payload['sub_mch_id'] : $payload['mch_id'],
'prepayid' => $this->preOrder('pay/unifiedorder', $payload)->prepay_id,
'timestamp' => strval(time()),
'noncestr' => Str::random(),

View File

@ -3,6 +3,7 @@
namespace Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Config;
@ -16,6 +17,13 @@ abstract class Gateway implements GatewayInterface
*/
protected $config;
/**
* Mode.
*
* @var string
*/
protected $mode;
/**
* Bootstrap.
*
@ -26,6 +34,7 @@ abstract class Gateway implements GatewayInterface
public function __construct(Config $config)
{
$this->config = $config;
$this->mode = $this->config->get('mode', Wechat::MODE_NORMAL);
}
/**

View File

@ -2,6 +2,7 @@
namespace Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
@ -20,6 +21,9 @@ class GroupRedpackGateway extends Gateway
public function pay($endpoint, array $payload): Collection
{
$payload['wxappid'] = $payload['appid'];
if ($this->mode === Wechat::MODE_SERVICE) {
$payload['msgappid'] = $payload['appid'];
}
$payload['amt_type'] = 'ALL_RAND';
unset($payload['appid'], $payload['trade_type'], $payload['notify_url'], $payload['spbill_create_ip']);

View File

@ -2,6 +2,7 @@
namespace Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Supports\Collection;
class MiniappGateway extends MpGateway
@ -19,6 +20,9 @@ class MiniappGateway extends MpGateway
public function pay($endpoint, array $payload): Collection
{
$payload['appid'] = $this->config->get('miniapp_id');
if ($this->mode === Wechat::MODE_SERVICE) {
$payload['sub_appid'] = $this->config->get('sub_miniapp_id');
}
return parent::pay($endpoint, $payload);
}

View File

@ -3,6 +3,7 @@
namespace Yansongda\Pay\Gateways\Wechat;
use Symfony\Component\HttpFoundation\Request;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
@ -21,6 +22,9 @@ class RedpackGateway extends Gateway
public function pay($endpoint, array $payload): Collection
{
$payload['wxappid'] = $payload['appid'];
if ($this->mode === Wechat::MODE_SERVICE) {
$payload['msgappid'] = $payload['appid'];
}
$payload['client_ip'] = Request::createFromGlobals()->server->get('SERVER_ADDR');
unset($payload['appid'], $payload['trade_type'], $payload['notify_url'], $payload['spbill_create_ip']);

View File

@ -5,6 +5,7 @@ namespace Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Traits\HasHttpRequest;
@ -102,6 +103,10 @@ class Support
$type = isset($order['type']) ? $order['type'].($order['type'] == 'app' ? '' : '_').'id' : 'app_id';
$payload['appid'] = $config->get($type, '');
$mode = $config->get('mode', Wechat::MODE_NORMAL);
if ($mode === Wechat::MODE_SERVICE) {
$payload['sub_appid'] = $config->get('sub_'.$type, '');
}
unset($payload['notify_url'], $payload['trade_type'], $payload['type']);
@ -209,11 +214,11 @@ class Support
public static function baseUri($mode = null): string
{
switch ($mode) {
case 'dev':
case Wechat::MODE_DEV:
self::getInstance()->baseUri = 'https://api.mch.weixin.qq.com/sandboxnew/';
break;
case 'hk':
case Wechat::MODE_HK:
self::getInstance()->baseUri = 'https://apihk.mch.weixin.qq.com/';
break;

View File

@ -3,6 +3,7 @@
namespace Yansongda\Pay\Gateways\Wechat;
use Symfony\Component\HttpFoundation\Request;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Supports\Collection;
@ -20,6 +21,9 @@ class TransferGateway extends Gateway
*/
public function pay($endpoint, array $payload): Collection
{
if ($this->mode === Wechat::MODE_SERVICE) {
unset($payload['sub_mch_id'], $payload['sub_appid']);
}
$type = isset($payload['type']) ? ($payload['type'].($payload['type'] == 'app' ?: '_').'id') : 'app_id';
$payload['mch_appid'] = $this->config->get($type, '');