-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* V2版本 微信发放普通红包 * V2微信发送普通红包测试单例 --------- Co-authored-by: ohh <mrmercury@foxmail.com> Co-authored-by: yansongda <me@yansongda.cn>
- Loading branch information
1 parent
fe81a43
commit 669c018
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
## v3.7.2 | ||
|
||
### added | ||
|
||
- feat: 微信V2版本支持普通红包(#973) | ||
|
||
## v3.7.1 | ||
|
||
### fixed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yansongda\Pay\Plugin\Wechat\V2\Pay\Redpack; | ||
|
||
use Closure; | ||
use Throwable; | ||
use Yansongda\Artful\Contract\PluginInterface; | ||
use Yansongda\Artful\Exception\ContainerException; | ||
use Yansongda\Artful\Exception\ServiceNotFoundException; | ||
use Yansongda\Artful\Logger; | ||
use Yansongda\Artful\Packer\XmlPacker; | ||
use Yansongda\Artful\Rocket; | ||
use Yansongda\Pay\Pay; | ||
use Yansongda\Supports\Collection; | ||
use Yansongda\Supports\Str; | ||
|
||
use function Yansongda\Pay\get_wechat_config; | ||
use function Yansongda\Pay\get_wechat_type_key; | ||
|
||
/** | ||
* @see https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon_sl.php?chapter=13_4&index=3 | ||
*/ | ||
class SendPlugin implements PluginInterface | ||
{ | ||
/** | ||
* @throws ContainerException | ||
* @throws ServiceNotFoundException | ||
* @throws Throwable 随机字符串生成失败 | ||
*/ | ||
public function assembly(Rocket $rocket, Closure $next): Rocket | ||
{ | ||
Logger::debug('[Wechat][V2][Pay][Redpack][SendPlugin] 插件开始装载', ['rocket' => $rocket]); | ||
$payload = $rocket->getPayload(); | ||
$params = $rocket->getParams(); | ||
$config = get_wechat_config($params); | ||
|
||
if (Pay::MODE_SERVICE === ($config['mode'] ?? Pay::MODE_NORMAL)) { | ||
$data = $this->service($payload, $config, $params); | ||
} | ||
|
||
$rocket->setPacker(XmlPacker::class) | ||
->mergePayload(array_merge( | ||
[ | ||
'_url' => 'mmpaymkttransfers/sendredpack', | ||
'_content_type' => 'application/xml', | ||
'nonce_str' => Str::random(32), | ||
'_http' => [ | ||
'ssl_key' => $config['mch_secret_cert'], | ||
'cert' => $config['mch_public_cert_path'], | ||
], | ||
], | ||
$data ?? $this->normal($config, $params) | ||
)); | ||
|
||
Logger::info('[Wechat][V2][Pay][Pos][PayPlugin] 插件装载完毕', ['rocket' => $rocket]); | ||
|
||
return $next($rocket); | ||
} | ||
|
||
protected function normal(array $config, array $params): array | ||
{ | ||
return [ | ||
'wxappid' => $config[get_wechat_type_key($params)] ?? '', | ||
'mch_id' => $config['mch_id'] ?? '', | ||
]; | ||
} | ||
|
||
protected function service(Collection $payload, array $config, array $params): array | ||
{ | ||
$wechatTypeKey = get_wechat_type_key($params); | ||
|
||
return [ | ||
'wxappid' => $config[$wechatTypeKey] ?? '', | ||
'mch_id' => $config['mch_id'] ?? '', | ||
'sub_mch_id' => $payload->get('sub_mch_id', $config['sub_mch_id'] ?? ''), | ||
'msgappid' => $config['sub_'.$wechatTypeKey], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yansongda\Pay\Shortcut\Wechat; | ||
|
||
use Yansongda\Artful\Contract\ShortcutInterface; | ||
use Yansongda\Artful\Plugin\AddPayloadBodyPlugin; | ||
use Yansongda\Artful\Plugin\ParserPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\AddRadarPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\ResponsePlugin; | ||
use Yansongda\Pay\Plugin\Wechat\StartPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\AddPayloadSignaturePlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\Pay\Redpack\SendPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\VerifySignaturePlugin; | ||
|
||
class RedpackShortcut implements ShortcutInterface | ||
{ | ||
public function getPlugins(array $params): array | ||
{ | ||
return [ | ||
StartPlugin::class, | ||
SendPlugin::class, | ||
AddPayloadSignaturePlugin::class, | ||
AddPayloadBodyPlugin::class, | ||
AddRadarPlugin::class, | ||
VerifySignaturePlugin::class, | ||
ResponsePlugin::class, | ||
ParserPlugin::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Yansongda\Pay\Tests\Plugin\Wechat\V2\Pay\Redpack; | ||
|
||
use Yansongda\Artful\Packer\XmlPacker; | ||
use Yansongda\Artful\Rocket; | ||
use Yansongda\Pay\Plugin\Wechat\V2\Pay\Redpack\SendPlugin; | ||
use Yansongda\Pay\Tests\TestCase; | ||
use Yansongda\Supports\Collection; | ||
|
||
class SendPluginTest extends TestCase | ||
{ | ||
protected SendPlugin $plugin; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->plugin = new SendPlugin(); | ||
} | ||
|
||
public function testNormal() | ||
{ | ||
$rocket = new Rocket(); | ||
$rocket->setPayload(new Collection( [ | ||
"mch_billno" => "111", | ||
])); | ||
|
||
$result = $this->plugin->assembly($rocket, function ($rocket) { return $rocket; }); | ||
$payload = $result->getPayload(); | ||
|
||
self::assertEquals(XmlPacker::class, $result->getPacker()); | ||
self::assertEquals('mmpaymkttransfers/sendredpack', $payload->get('_url')); | ||
self::assertEquals('application/xml', $payload->get('_content_type')); | ||
self::assertEquals('111', $payload->get('mch_billno')); | ||
self::assertEquals('wx55955316af4ef13', $payload->get('wxappid')); | ||
self::assertEquals('1600314069', $payload->get('mch_id')); | ||
self::assertNotEmpty($payload->get('nonce_str')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yansongda\Pay\Tests\Shortcut\Wechat; | ||
|
||
use Yansongda\Artful\Plugin\AddPayloadBodyPlugin; | ||
use Yansongda\Artful\Plugin\ParserPlugin; | ||
use Yansongda\Pay\Shortcut\Wechat\RedpackShortcut; | ||
use Yansongda\Pay\Tests\TestCase; | ||
use Yansongda\Pay\Plugin\Wechat\AddRadarPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\ResponsePlugin; | ||
use Yansongda\Pay\Plugin\Wechat\StartPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\AddPayloadSignaturePlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\Pay\Redpack\SendPlugin; | ||
use Yansongda\Pay\Plugin\Wechat\V2\VerifySignaturePlugin; | ||
|
||
class RedpackShortcutTest extends TestCase | ||
{ | ||
protected RedpackShortcut $plugin; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->plugin = new RedpackShortcut(); | ||
} | ||
|
||
public function testDefault() | ||
{ | ||
self::assertEquals([ | ||
StartPlugin::class, | ||
SendPlugin::class, | ||
AddPayloadSignaturePlugin::class, | ||
AddPayloadBodyPlugin::class, | ||
AddRadarPlugin::class, | ||
VerifySignaturePlugin::class, | ||
ResponsePlugin::class, | ||
ParserPlugin::class, | ||
], $this->plugin->getPlugins([])); | ||
} | ||
} |