Skip to content

Commit

Permalink
Merge pull request #311 from ac1982/master
Browse files Browse the repository at this point in the history
Add merchant pay support.
  • Loading branch information
overtrue committed Mar 1, 2016
2 parents 862cc09 + 58117d3 commit f7b3bfb
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Core/AbstractAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class AbstractAPI
protected $http;

/**
* The reqeust token.
* The request token.
*
* @var \EasyWeChat\Core\AccessToken
*/
Expand Down Expand Up @@ -219,7 +219,7 @@ protected function retryMiddleware()
}

/**
* Check the array data erros, and Throw expcetion when the contents cotnains error.
* Check the array data errors, and Throw exception when the contents contains error.
*
* @param array $contents
*
Expand Down
57 changes: 30 additions & 27 deletions src/Foundation/ServiceProviders/PaymentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author overtrue <[email protected]>
* @author AC <[email protected]>
* @copyright 2015
*
* @link https://github.com/overtrue/wechat
Expand All @@ -27,40 +27,43 @@

use EasyWeChat\Payment\LuckyMoney\LuckyMoney;
use EasyWeChat\Payment\Merchant;
use EasyWeChat\Payment\MerchantPay\MerchantPay;
use EasyWeChat\Payment\Payment;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class PaymentServiceProvider.
*/
class PaymentServiceProvider implements ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple)
{
$pimple['merchant'] = function ($pimple) {
$config = array_merge(
['app_id' => $pimple['config']['app_id']],
$pimple['config']->get('payment', [])
);
class PaymentServiceProvider implements ServiceProviderInterface {
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple) {
$pimple['merchant'] = function ($pimple) {
$config = array_merge(
['app_id' => $pimple['config']['app_id']],
$pimple['config']->get('payment', [])
);

return new Merchant($config);
};
return new Merchant($config);
};

$pimple['payment'] = function ($pimple) {
return new Payment($pimple['merchant']);
};
$pimple['payment'] = function ($pimple) {
return new Payment($pimple['merchant']);
};

$pimple['lucky_money'] = function ($pimple) {
return new LuckyMoney($pimple['merchant']);
};
}
$pimple['lucky_money'] = function ($pimple) {
return new LuckyMoney($pimple['merchant']);
};

$pimple['merchant_pay'] = function ($pimple) {
return new MerchantPay($pimple['merchant']);
};
}
}
150 changes: 150 additions & 0 deletions src/Payment/MerchantPay/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?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.
*/

/**
* API.php.
*
* @author AC <[email protected]>
* @copyright 2015 overtrue <[email protected]>
*
* @link https://github.com/overtrue
* @link http://overtrue.me
*/
namespace EasyWeChat\Payment\MerchantPay;

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/promotion/transfers';
const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo ';

/**
* API constructor.
*
* @param \EasyWeChat\Payment\Merchant $merchant
*/
public function __construct(Merchant $merchant)
{
$this->merchant = $merchant;
}

/**
* Query MerchantPay.
*
* @param string $mchBillNo
*
* @return \EasyWeChat\Support\Collection
*
* @notice mch_id when query, but mchid when send
*/
public function query($mchBillNo)
{
$params = [
'appid' => $this->merchant->app_id,
'mch_id' => $this->merchant->merchant_id,
'partner_trade_no' => $mchBillNo,
];

return $this->request(self::API_QUERY, $params);
}

/**
* Send MerchantPay.
*
* @param array $params
* @param string $type
*
* @return \EasyWeChat\Support\Collection
*/
public function send(array $params)
{
$params['mchid'] = $this->merchant->merchant_id;
$params['mch_appid'] = $this->merchant->app_id;
return $this->request(self::API_SEND, $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['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));
}
}
108 changes: 108 additions & 0 deletions src/Payment/MerchantPay/MerchantPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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.
*/

/**
* MerchantPay.php.
*
* @author AC <[email protected]>
* @copyright 2015 overtrue <[email protected]>
*
* @link https://github.com/overtrue
* @link http://overtrue.me
*/
namespace EasyWeChat\Payment\MerchantPay;

use EasyWeChat\Payment\Merchant;

/**
* Class MerchantPay.
*/
class MerchantPay
{
/**
* @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);
}
}
}

0 comments on commit f7b3bfb

Please sign in to comment.