Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merchant client. #983

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Payment/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Application extends ServiceContainer
Bill\ServiceProvider::class,
Coupon\ServiceProvider::class,
Jssdk\ServiceProvider::class,
Merchant\ServiceProvider::class,
Order\ServiceProvider::class,
Redpack\ServiceProvider::class,
Refund\ServiceProvider::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Payment/Bill/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class Client extends BaseClient
*
* @return \EasyWeChat\Kernel\Http\StreamResponse|\Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function download(string $date, string $type = 'ALL')
public function download(string $date, string $type = 'ALL', array $optional = [])
{
$params = [
'bill_date' => $date,
'bill_type' => $type,
];
] + $optional;

$response = $this->requestRaw('pay/downloadbill', $params);

Expand Down
7 changes: 4 additions & 3 deletions src/Payment/Kernel/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ protected function requestRaw($endpoint, array $params = [], $method = 'post', a
* @param string $endpoint
* @param array $params
* @param string $method
* @param array $options
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
protected function safeRequest($endpoint, array $params, $method = 'post')
protected function safeRequest($endpoint, array $params, $method = 'post', array $options = [])
{
$options = [
$options = array_merge([
'cert' => $this->app['config']->get('cert_path'),
'ssl_key' => $this->app['config']->get('key_path'),
];
], $options);

return $this->request($endpoint, $params, $method, $options);
}
Expand Down
77 changes: 77 additions & 0 deletions src/Payment/Merchant/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Merchant;

use EasyWeChat\Payment\Kernel\BaseClient;

/**
* Class Client.
*
* @author mingyoung <[email protected]>
*/
class Client extends BaseClient
{
/**
* Add sub-merchant.
*
* @param array $params
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function addSubMerchant(array $params)
{
return $this->manage($params, ['action' => 'add']);
}

/**
* Query sub-merchant by merchant id.
*
* @param string $id
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function querySubMerchantByMerchantId(string $id)
{
$params = [
'micro_mch_id' => $id,
];

return $this->manage($params, ['action' => 'query']);
}

/**
* Query sub-merchant by wechat id.
*
* @param string $id
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function querySubMerchantByWeChatId(string $id)
{
$params = [
'recipient_wechatid' => $id,
];

return $this->manage($params, ['action' => 'query']);
}

/**
* @param array $params
* @param array $query
*
*@return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
protected function manage(array $params, array $query)
{
return $this->safeRequest('secapi/mch/submchmanage', $params, 'post', compact('query'));
}
}
33 changes: 33 additions & 0 deletions src/Payment/Merchant/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Merchant;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class ServiceProvider.
*
* @author mingyoung <[email protected]>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['merchant'] = function ($app) {
return new Client($app);
};
}
}
58 changes: 58 additions & 0 deletions tests/Payment/Merchant/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Tests\Payment\Merchant;

use EasyWeChat\Payment\Application;
use EasyWeChat\Payment\Merchant\Client;
use EasyWeChat\Tests\TestCase;

class ClientTest extends TestCase
{
protected function app()
{
return new Application([
'app_id' => 'wx123456',
'mch_id' => 'foo-merchant-id',
'notify_url' => 'http://easywechat.org/notify',
]);
}

public function testAddSubMerchant()
{
$client = $this->mockApiClient(Client::class, ['safeRequest'], $this->app());
$client->expects()->safeRequest('secapi/mch/submchmanage', [
'foo' => 'bar',
], 'post', ['query' => ['action' => 'add']])->andReturn('mock-result');

$this->assertSame('mock-result', $client->addSubMerchant(['foo' => 'bar']));
}

public function testQuerySubMerchantByMerchantId()
{
$client = $this->mockApiClient(Client::class, ['safeRequest'], $this->app());
$client->expects()->safeRequest('secapi/mch/submchmanage', [
'micro_mch_id' => 'foo-id',
], 'post', ['query' => ['action' => 'query']])->andReturn('mock-result');

$this->assertSame('mock-result', $client->querySubMerchantByMerchantId('foo-id'));
}

public function testQuerySubMerchantByWeChatId()
{
$client = $this->mockApiClient(Client::class, ['safeRequest'], $this->app());
$client->expects()->safeRequest('secapi/mch/submchmanage', [
'recipient_wechatid' => 'foo-id',
], 'post', ['query' => ['action' => 'query']])->andReturn('mock-result');

$this->assertSame('mock-result', $client->querySubMerchantByWeChatId('foo-id'));
}
}