-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support cash coupon * fixed styleci
- Loading branch information
Showing
5 changed files
with
365 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
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
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,158 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/wechat. | ||
* | ||
* (c) overtrue <i@overtrue.me> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* API.php. | ||
* | ||
* @author tianyong90 <412039588@qq.com> | ||
* @copyright 2015 overtrue <i@overtrue.me> | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\Payment\CashCoupon; | ||
|
||
use EasyWeChat\Core\AbstractAPI; | ||
use EasyWeChat\Payment\Merchant; | ||
use EasyWeChat\Support\Collection; | ||
use EasyWeChat\Support\XML; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class API. | ||
*/ | ||
class API extends AbstractAPI | ||
{ | ||
/** | ||
* Merchant instance. | ||
* | ||
* @var Merchant | ||
*/ | ||
protected $merchant; | ||
|
||
// api | ||
const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon'; | ||
const API_QUERY_STOCK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock'; | ||
const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo'; | ||
|
||
/** | ||
* API constructor. | ||
* | ||
* @param \EasyWeChat\Payment\Merchant $merchant | ||
*/ | ||
public function __construct(Merchant $merchant) | ||
{ | ||
$this->merchant = $merchant; | ||
} | ||
|
||
/** | ||
* send a cash coupon. | ||
* | ||
* @param array $params | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function send(array $params) | ||
{ | ||
$params['openid_count'] = 1; | ||
|
||
return $this->request(self::API_SEND, $params); | ||
} | ||
|
||
/** | ||
* query a coupon stock. | ||
* | ||
* @param array $params | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function queryStock(array $params) | ||
{ | ||
return $this->request(self::API_QUERY_STOCK, $params); | ||
} | ||
|
||
/** | ||
* query a info of coupon. | ||
* | ||
* @param array $params | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function query(array $params) | ||
{ | ||
return $this->request(self::API_QUERY, $params); | ||
} | ||
|
||
/** | ||
* Merchant setter. | ||
* | ||
* @param Merchant $merchant | ||
* | ||
* @return $this | ||
*/ | ||
public function setMerchant(Merchant $merchant) | ||
{ | ||
$this->merchant = $merchant; | ||
} | ||
|
||
/** | ||
* Merchant getter. | ||
* | ||
* @return Merchant | ||
*/ | ||
public function getMerchant() | ||
{ | ||
return $this->merchant; | ||
} | ||
|
||
/** | ||
* Make a API request. | ||
* | ||
* @param string $api | ||
* @param array $params | ||
* @param string $method | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
protected function request($api, array $params, $method = 'post') | ||
{ | ||
$params = array_filter($params); | ||
$params['mch_id'] = $this->merchant->merchant_id; | ||
$params['appid'] = $this->merchant->app_id; | ||
$params['nonce_str'] = uniqid(); | ||
$params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5'); | ||
|
||
$options = [ | ||
'body' => XML::build($params), | ||
'cert' => $this->merchant->get('cert_path'), | ||
'ssl_key' => $this->merchant->get('key_path'), | ||
]; | ||
|
||
return $this->parseResponse($this->getHttp()->request($api, $method, $options)); | ||
} | ||
|
||
/** | ||
* Parse Response XML to array. | ||
* | ||
* @param \Psr\Http\Message\ResponseInterface|string $response | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
protected function parseResponse($response) | ||
{ | ||
if ($response instanceof ResponseInterface) { | ||
$response = $response->getBody(); | ||
} | ||
|
||
return new Collection((array) XML::parse($response)); | ||
} | ||
} |
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,109 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/wechat. | ||
* | ||
* (c) overtrue <i@overtrue.me> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* LuckyMoney.php. | ||
* | ||
* @author tianyong90 <412039588@qq.com> | ||
* @copyright 2015 overtrue <i@overtrue.me> | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\Payment\CashCoupon; | ||
|
||
use EasyWeChat\Payment\Merchant; | ||
|
||
/** | ||
* Class LuckyMoney. | ||
*/ | ||
class CashCoupon | ||
{ | ||
/** | ||
* @var API | ||
*/ | ||
protected $api; | ||
|
||
/** | ||
* Merchant instance. | ||
* | ||
* @var \EasyWeChat\Payment\Merchant | ||
*/ | ||
protected $merchant; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Merchant $merchant | ||
*/ | ||
public function __construct(Merchant $merchant) | ||
{ | ||
$this->merchant = $merchant; | ||
} | ||
|
||
/** | ||
* Merchant setter. | ||
* | ||
* @param Merchant $merchant | ||
*/ | ||
public function setMerchant(Merchant $merchant) | ||
{ | ||
$this->merchant = $merchant; | ||
} | ||
|
||
/** | ||
* Merchant getter. | ||
* | ||
* @return Merchant | ||
*/ | ||
public function getMerchant() | ||
{ | ||
return $this->merchant; | ||
} | ||
|
||
/** | ||
* API setter. | ||
* | ||
* @param API $api | ||
*/ | ||
public function setAPI(API $api) | ||
{ | ||
$this->api = $api; | ||
} | ||
|
||
/** | ||
* Return API instance. | ||
* | ||
* @return API | ||
*/ | ||
public function getAPI() | ||
{ | ||
return $this->api ?: $this->api = new API($this->getMerchant()); | ||
} | ||
|
||
/** | ||
* Magic call. | ||
* | ||
* @param string $method | ||
* @param array $args | ||
* | ||
* @return mixed | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function __call($method, $args) | ||
{ | ||
if (is_callable([$this->getAPI(), $method])) { | ||
return call_user_func_array([$this->api, $method], $args); | ||
} | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/wechat. | ||
* | ||
* (c) overtrue <i@overtrue.me> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace EasyWeChat\Tests\Payment; | ||
|
||
use EasyWeChat\Core\Http; | ||
use EasyWeChat\Payment\CashCoupon\API; | ||
use EasyWeChat\Payment\Merchant; | ||
use EasyWeChat\Support\XML; | ||
use EasyWeChat\Tests\TestCase; | ||
|
||
class PaymentCashCouponAPITest extends TestCase | ||
{ | ||
/** | ||
* Build API instance. | ||
* | ||
* @return API | ||
*/ | ||
public function getAPI() | ||
{ | ||
$http = \Mockery::mock(Http::class); | ||
|
||
$http->shouldReceive('request')->andReturnUsing(function ($api, $method, $options) { | ||
$options['body'] = XML::parse($options['body']); | ||
|
||
return XML::build(compact('api', 'options')); | ||
}); | ||
|
||
$merchant = new Merchant([ | ||
'merchant_id' => 'testMerchantId', | ||
'app_id' => 'wxTestAppId', | ||
'key' => 'testKey', | ||
'cert_path' => 'testCertPath', | ||
'key_path' => 'testKeyPath', | ||
]); | ||
|
||
$api = \Mockery::mock('EasyWeChat\Payment\CashCoupon\API[getHttp]', [$merchant]); | ||
$api->shouldReceive('getHttp')->andReturn($http); | ||
|
||
return $api; | ||
} | ||
|
||
/** | ||
* Test send(). | ||
*/ | ||
public function testSend() | ||
{ | ||
$api = $this->getAPI(); | ||
|
||
$response = $api->send(['foo' => 'bar']); | ||
|
||
$this->assertEquals(API::API_SEND, $response['api']); | ||
$this->assertEquals('wxTestAppId', $response['options']['body']['appid']); | ||
$this->assertEquals('testMerchantId', $response['options']['body']['mch_id']); | ||
|
||
$this->assertEquals('bar', $response['options']['body']['foo']); | ||
} | ||
|
||
public function testQueryStock() | ||
{ | ||
$api = $this->getAPI(); | ||
|
||
$response = $api->queryStock(['coupon_stock_id' => '1234']); | ||
|
||
$this->assertEquals(API::API_QUERY_STOCK, $response['api']); | ||
$this->assertEquals('wxTestAppId', $response['options']['body']['appid']); | ||
$this->assertEquals('testMerchantId', $response['options']['body']['mch_id']); | ||
|
||
$this->assertEquals('1234', $response['options']['body']['coupon_stock_id']); | ||
} | ||
|
||
public function testQuery() | ||
{ | ||
$api = $this->getAPI(); | ||
|
||
$response = $api->query(['foo' => 'bar']); | ||
|
||
$this->assertEquals(API::API_QUERY, $response['api']); | ||
$this->assertEquals('wxTestAppId', $response['options']['body']['appid']); | ||
$this->assertEquals('testMerchantId', $response['options']['body']['mch_id']); | ||
|
||
$this->assertEquals('bar', $response['options']['body']['foo']); | ||
} | ||
} |