Skip to content

Commit

Permalink
support cash coupon (#642)
Browse files Browse the repository at this point in the history
* support cash coupon

* fixed styleci
  • Loading branch information
Hanson authored and overtrue committed Apr 8, 2017
1 parent 8914430 commit e38f5cd
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* @property \EasyWeChat\Payment\Payment $payment
* @property \EasyWeChat\Payment\LuckyMoney\LuckyMoney $lucky_money
* @property \EasyWeChat\Payment\MerchantPay\MerchantPay $merchant_pay
* @property \EasyWeChat\Payment\CashCoupon\CashCoupon $cash_coupon
* @property \EasyWeChat\Reply\Reply $reply
* @property \EasyWeChat\Broadcast\Broadcast $broadcast
* @property \EasyWeChat\Card\Card $card
Expand Down
5 changes: 5 additions & 0 deletions src/Foundation/ServiceProviders/PaymentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace EasyWeChat\Foundation\ServiceProviders;

use EasyWeChat\Payment\CashCoupon\CashCoupon;
use EasyWeChat\Payment\LuckyMoney\LuckyMoney;
use EasyWeChat\Payment\Merchant;
use EasyWeChat\Payment\MerchantPay\MerchantPay;
Expand Down Expand Up @@ -68,5 +69,9 @@ public function register(Container $pimple)
$pimple['merchant_pay'] = function ($pimple) {
return new MerchantPay($pimple['merchant']);
};

$pimple['cash_coupon'] = function ($pimple) {
return new CashCoupon($pimple['merchant']);
};
}
}
158 changes: 158 additions & 0 deletions src/Payment/CashCoupon/API.php
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));
}
}
109 changes: 109 additions & 0 deletions src/Payment/CashCoupon/CashCoupon.php
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);
}
}
}
92 changes: 92 additions & 0 deletions tests/Payment/PaymentCashCouponAPITest.php
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']);
}
}

0 comments on commit e38f5cd

Please sign in to comment.