Skip to content

Commit

Permalink
fix: 微信代金券 api 参数错误问题 (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Apr 14, 2023
1 parent 1e720dd commit d676442
Show file tree
Hide file tree
Showing 28 changed files with 921 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

- delete: 移除废弃的类(#752)

### fixed

- fix: 微信代金券 api 参数错误问题(#777)

### refactor

- refactor: 重构 ArrayParser 类(#754)
Expand Down
14 changes: 14 additions & 0 deletions src/Plugin/Wechat/Marketing/Coupon/CreatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_1.shtml
*/
class CreatePlugin extends GeneralPlugin
{
/**
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function doSomething(Rocket $rocket): void
{
$config = get_wechat_config($rocket->getParams());
$payload = $rocket->getPayload();

if (!$payload->has('belong_merchant')) {
$rocket->mergePayload(['belong_merchant' => $config['mch_id']]);
}
}

protected function getUri(Rocket $rocket): string
Expand Down
19 changes: 11 additions & 8 deletions src/Plugin/Wechat/Marketing/Coupon/PausePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_13.shtml
*/
class PausePlugin extends GeneralPlugin
{
/**
* @throws InvalidParamsException
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function doSomething(Rocket $rocket): void
{
$config = get_wechat_config($rocket->getParams());
$payload = $rocket->getPayload();

if (is_null($payload->get('stock_creator_mchid'))) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
if (!$payload->has('stock_creator_mchid')) {
$rocket->mergePayload(['stock_creator_mchid' => $config['mch_id']]);
}

$rocket->setPayload(new Collection([
'stock_creator_mchid' => $payload->get('stock_creator_mchid'),
]));
$rocket->getPayload()->forget('stock_id');
}

/**
Expand All @@ -38,7 +41,7 @@ protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();

if (is_null($payload->get('stock_id'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Plugin/Wechat/Marketing/Coupon/QueryCouponDetailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_9.shtml
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_6.shtml
*/
class QueryCouponDetailPlugin extends GeneralPlugin
{
Expand All @@ -36,16 +36,16 @@ protected function doSomething(Rocket $rocket): void
protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();
$appid = get_wechat_config($rocket->getParams())['mp_app_id'] ?? '';
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (is_null($payload->get('coupon_id'))
|| is_null($payload->get('openid'))) {
if (!$payload->has('coupon_id') || !$payload->has('openid')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

return 'v3/marketing/favor/users/'.
$payload->get('openid').
'/coupons/'.$payload->get('coupon_id').
'?appid='.$appid;
'?appid='.$payload->get('appid', $config[$this->getConfigKey($params)] ?? '');
}
}
12 changes: 10 additions & 2 deletions src/Plugin/Wechat/Marketing/Coupon/QueryStockDetailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_5.shtml
*/
Expand All @@ -26,17 +30,21 @@ protected function doSomething(Rocket $rocket): void

/**
* @throws InvalidParamsException
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (is_null($payload->get('stock_id')) || is_null($payload->get('stock_creator_mchid'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

return 'v3/marketing/favor/stocks/'.
$payload->get('stock_id').
'?stock_creator_mchid='.$payload->get('stock_creator_mchid');
'?stock_creator_mchid='.$payload->get('stock_creator_mchid', $config['mch_id'] ?? '');
}
}
17 changes: 15 additions & 2 deletions src/Plugin/Wechat/Marketing/Coupon/QueryStockItemsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_8.shtml
*/
Expand All @@ -26,16 +30,25 @@ protected function doSomething(Rocket $rocket): void

/**
* @throws InvalidParamsException
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (is_null($payload->get('stock_id'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

$query = $payload->all();
if (!$payload->has('stock_creator_mchid')) {
$rocket->mergePayload(['stock_creator_mchid' => $config['mch_id']]);
}

$query = $rocket->getPayload()->all();

unset($query['stock_id']);

return 'v3/marketing/favor/stocks/'.
Expand Down
17 changes: 15 additions & 2 deletions src/Plugin/Wechat/Marketing/Coupon/QueryStockMerchantsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_7.shtml
*/
Expand All @@ -26,16 +30,25 @@ protected function doSomething(Rocket $rocket): void

/**
* @throws InvalidParamsException
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (!$payload->has('stock_creator_mchid')) {
$rocket->mergePayload(['stock_creator_mchid' => $config['mch_id']]);
}

if (is_null($payload->get('stock_id'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

$query = $payload->all();
$query = $rocket->getPayload()->all();

unset($query['stock_id']);

return 'v3/marketing/favor/stocks/'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();

if (is_null($payload->get('stock_id'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

return 'vv3/marketing/favor/stocks/'.
return 'v3/marketing/favor/stocks/'.
$payload->get('stock_id').
'/refund-flow';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();

if (is_null($payload->get('stock_id'))) {
if (!$payload->has('stock_id')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

return 'vv3/marketing/favor/stocks/'.
return 'v3/marketing/favor/stocks/'.
$payload->get('stock_id').
'/use-flow';
}
Expand Down
15 changes: 15 additions & 0 deletions src/Plugin/Wechat/Marketing/Coupon/QueryStocksPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_4.shtml
*/
Expand All @@ -22,8 +26,19 @@ protected function doSomething(Rocket $rocket): void
$rocket->setPayload(null);
}

/**
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function getUri(Rocket $rocket): string
{
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (!$rocket->getPayload()->has('stock_creator_mchid')) {
$rocket->mergePayload(['stock_creator_mchid' => $config['mch_id']]);
}

return 'v3/marketing/favor/stocks?'.$rocket->getPayload()->query();
}
}
21 changes: 19 additions & 2 deletions src/Plugin/Wechat/Marketing/Coupon/QueryUserCouponsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Yansongda\Pay\Plugin\Wechat\Marketing\Coupon;

use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Plugin\Wechat\GeneralPlugin;
use Yansongda\Pay\Rocket;

use function Yansongda\Pay\get_wechat_config;

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_1_9.shtml
*/
Expand All @@ -26,16 +30,29 @@ protected function doSomething(Rocket $rocket): void

/**
* @throws InvalidParamsException
* @throws ContainerException
* @throws ServiceNotFoundException
*/
protected function getUri(Rocket $rocket): string
{
$payload = $rocket->getPayload();
$params = $rocket->getParams();
$config = get_wechat_config($params);

if (is_null($payload->get('openid'))) {
if (!$payload->has('openid')) {
throw new InvalidParamsException(Exception::MISSING_NECESSARY_PARAMS);
}

$query = $payload->all();
if (!$payload->has('appid')) {
$rocket->mergePayload(['appid' => $config[$this->getConfigKey($params)] ?? '']);
}

if (!$payload->has('creator_mchid')) {
$rocket->mergePayload(['creator_mchid' => $config['mch_id']]);
}

$query = $rocket->getPayload()->all();

unset($query['openid']);

return 'v3/marketing/favor/users/'.
Expand Down
Loading

0 comments on commit d676442

Please sign in to comment.