Skip to content

Commit

Permalink
Merge pull request #240 from tianyong90/develop
Browse files Browse the repository at this point in the history
Add Luckymoney.
  • Loading branch information
overtrue committed Jan 14, 2016
2 parents a4c1f15 + 185748f commit f2da49b
Show file tree
Hide file tree
Showing 3 changed files with 325 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Foundation/ServiceProviders/PaymentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

Expand Down Expand Up @@ -57,5 +58,9 @@ public function register(Container $pimple)
$pimple['payment'] = function ($pimple) {
return new Payment($pimple['merchant']);
};

$pimple['luckymoney'] = function ($pimple) {
return new LuckyMoney($pimple['merchant']);
};
}
}
212 changes: 212 additions & 0 deletions src/Payment/luckymoney/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?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 tianyong90 <[email protected]>
* @copyright 2015 overtrue <[email protected]>
*
* @link https://github.com/overtrue
* @link http://overtrue.me
*/
namespace EasyWeChat\Payment\LuckyMoney;

use EasyWeChat\Payment\Merchant;
use EasyWeChat\Core\AbstractAPI;
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/sendredpack';
const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder';

// LuckyMoney type
const TYPE_NORMAL = 'NORMAL';
const TYPE_GROUP = 'GROUP';

// Risk control type.
const RISK_NORMAL = 'NORMAL';
const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT';
const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT';
const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT';

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

/**
* Prepare luckymoney.
*
*
* @return Collection
*/
public function prepare(array $params)
{
$params['wxappid'] = $this->merchant->app_id;

//This parameter is fixed and can not be changed.
$params['auth_mchid'] = '1000052601';
//This parameter is fixed and can not be changed.
$params['auth_appid'] = 'wxbf42bd79c4391863';

$params['amt_type'] = 'ALL_RAND';

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

/**
* Query luckymoney.
*
* @param string $mchBillNo
*/
public function query($orderNo)
{
$params = [
'appid' => $this->merchant->app_id,
'mch_billno' => $orderNo,
'bill_type' => 'MCHT',
];

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

/**
* Send Luckymoney.
*
* @param array $params
* @param string $type
*/
public function send(array $params, $type = self::TYPE_NORMAL)
{
if ($type == self::TYPE_NORMAL) {
$api = self::API_SEND;
} else {
$api = self::API_SEND_GROUP;
}

$params['wxappid'] = $this->merchant->app_id;

return $this->request($api, $params);
}

/**
* Send normal lucnymoney.
*
* @param array $params
*
* @return Collection
*/
public function sendNormal($params)
{
$params['total_num'] = 1;
$params['client_ip'] = $params['client_ip'] ?: $_SERVER['HTTP_CLIENT_IP'];

return $this->send($params, self::TYPE_NORMAL);
}

/**
* Send group luckymoney.
*
* @param array $params
*
* @return Collection
*/
public function sendGroup($params)
{
$params['amt_type'] = 'ALL_RAND';
$params['client_ip'] = $params['client_ip'] ?: $_SERVER['HTTP_CLIENT_IP'];

return $this->send($params, self::TYPE_GROUP);
}

/**
* 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 Collection
*/
protected function request($api, array $params, $method = 'post')
{
$params['mch_id'] = $this->merchant->merchant_id;
$params['nonce_str'] = uniqid();
$params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5');

$options['body'] = XML::build($params);
$options['cert'] = $this->merchant->get('cert_path');
$options['ssl_key'] = $this->merchant->get('key_path');

return $this->parseResponse($this->getHttp()->request($api, $method, $options));
}

/**
* Parse Response XML to array.
*
* @param string $response
*
* @return 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/luckymoney/LuckyMoney.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.
*/

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

use EasyWeChat\Payment\Merchant;

/**
* Class LuckyMoney.
*/
class LuckyMoney
{
/**
* @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 f2da49b

Please sign in to comment.