-
-
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.
Merge pull request #311 from ac1982/master
Add merchant pay support.
- Loading branch information
Showing
4 changed files
with
290 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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']); | ||
}; | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |