-
-
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.
Wechat Payment
- Loading branch information
Showing
7 changed files
with
708 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Payment.php | ||
* | ||
* Part of Overtrue\Wechat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Frye <frye0423@gmail.com> | ||
* @copyright 2015 Frye <frye0423@gmail.com> | ||
* @link https://github.com/0i | ||
* @link http://blog.lost-magic.com | ||
* @link https://github.com/thenbsp/Wechat | ||
*/ | ||
|
||
namespace Overtrue\Wechat; | ||
|
||
use Overtrue\Wechat\Utils\JSON; | ||
use Overtrue\Wechat\Utils\SignGenerator; | ||
use Overtrue\Wechat\Payment\UnifiedOrder; | ||
|
||
/** | ||
* 微信支付 | ||
*/ | ||
class Payment | ||
{ | ||
/** | ||
* 统一下单 | ||
* | ||
* @var UnifiedOrder | ||
*/ | ||
protected $unifiedOrder; | ||
|
||
public function __construct(UnifiedOrder $unifiedOrder) | ||
{ | ||
$this->unifiedOrder = $unifiedOrder; | ||
} | ||
|
||
/** | ||
* 获取配置文件(用于 WeixinJSBridge invoke 方式) | ||
* | ||
* @param bool|true $asJson | ||
* | ||
* @return array|string | ||
*/ | ||
public function getConfig($asJson = true) | ||
{ | ||
$config = $this->generateConfig(); | ||
return $asJson ? JSON::encode($config) : $config; | ||
} | ||
|
||
/** | ||
* 获取配置文件(用于 Jssdk chooseWXPay 方式) | ||
* | ||
* @param bool|true $asJson | ||
* | ||
* @return array|string | ||
*/ | ||
public function getConfigJssdk($asJson = true) | ||
{ | ||
$config = $this->_generateConfig(); | ||
$params = array( | ||
'timestamp' => $config['timeStamp'], | ||
'nonceStr' => $config['nonceStr'], | ||
'package' => $config['package'], | ||
'signType' => $config['signType'], | ||
'paySign' => $config['paySign'] | ||
); | ||
return $asJson ? JSON::encode($params) : $params; | ||
} | ||
|
||
/** | ||
* 生成配置 | ||
* | ||
* @return array | ||
* @throws Payment\Exception | ||
*/ | ||
private function generateConfig() | ||
{ | ||
$response = $this->unifiedOrder->getResponse(); | ||
$business = $this->unifiedOrder->getBusiness(); | ||
$config = array( | ||
'appId' => $business->appid, | ||
'timeStamp' => (string) time(), | ||
'nonceStr' => $response['nonce_str'], | ||
'package' => 'prepay_id='.$response['prepay_id'], | ||
'signType' => 'MD5' | ||
); | ||
|
||
$signGenerator = new SignGenerator($config); | ||
$signGenerator->onSortAfter(function(SignGenerator $that) use ($business) { | ||
$that->key = $business->mch_key; | ||
}); | ||
$config['paySign'] = $signGenerator->getResult(); | ||
return $config; | ||
} | ||
} |
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,116 @@ | ||
<?php | ||
/** | ||
* Business.php | ||
* | ||
* Part of Overtrue\Wechat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Frye <frye0423@gmail.com> | ||
* @copyright 2015 Frye <frye0423@gmail.com> | ||
* @link https://github.com/0i | ||
* @link http://blog.lost-magic.com | ||
* @link https://github.com/thenbsp/Wechat | ||
*/ | ||
|
||
namespace Overtrue\Wechat\Payment; | ||
|
||
use Overtrue\Wechat\Utils\MagicAttributes; | ||
|
||
class Business extends MagicAttributes | ||
{ | ||
/** | ||
* 有效的参数 | ||
* | ||
* @var array | ||
*/ | ||
protected $valids = array('appid', 'appsecret', 'mch_id', 'mch_key'); | ||
|
||
/** | ||
* 商户证书 cert | ||
*/ | ||
protected $clientCert; | ||
|
||
/** | ||
* 商户证书 key | ||
*/ | ||
protected $clientKey; | ||
|
||
/** | ||
* @param string $appId | ||
* @param string $appSecret | ||
* @param string $mchId | ||
* @param string $mchKey | ||
*/ | ||
public function __construct($appId, $appSecret, $mchId, $mchKey) { | ||
$this->appid = $appId; | ||
$this->appsecret = $appSecret; | ||
$this->mch_id = $mchId; | ||
$this->mch_key = $mchKey; | ||
} | ||
|
||
/** | ||
* 设置商户证书 cert | ||
* | ||
* @param $filepath | ||
* | ||
* @return $this | ||
* @throws Exception | ||
*/ | ||
public function setClientCert($filepath) | ||
{ | ||
if( !file_exists($filepath) ) { | ||
throw new Exception(sprintf('client_cert "%s" is not found', $filepath)); | ||
} | ||
$this->clientCert = $filepath; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 获取商户证书 cert | ||
* @return string | ||
*/ | ||
public function getClientCert() | ||
{ | ||
return $this->clientCert; | ||
} | ||
|
||
/** | ||
* 设置商户证书 key | ||
* @param $filepath | ||
* | ||
* @return $this | ||
* @throws Exception | ||
*/ | ||
public function setClientKey($filepath) | ||
{ | ||
if (!file_exists($filepath)) { | ||
throw new Exception(sprintf('client_key "%s" is not found', $filepath)); | ||
} | ||
$this->clientKey = $filepath; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 获取商户证书 key | ||
* @return string | ||
*/ | ||
public function getClientKey() | ||
{ | ||
return $this->clientKey; | ||
} | ||
|
||
/** | ||
* 检测参数值是否有效 | ||
*/ | ||
public function checkParams() | ||
{ | ||
foreach($this->valids AS $paramName) { | ||
if (empty($this->attributes[$paramName])) { | ||
throw new Exception(sprintf('"%s" is required', $paramName)); | ||
} | ||
} | ||
} | ||
|
||
} |
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,19 @@ | ||
<?php | ||
/** | ||
* Exception.php | ||
* | ||
* Part of Overtrue\Wechat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Frye <frye0423@gmail.com> | ||
* @copyright 2015 Frye <frye0423@gmail.com> | ||
* @link https://github.com/0i | ||
* @link http://blog.lost-magic.com | ||
* @link https://github.com/thenbsp/Wechat | ||
*/ | ||
|
||
namespace Overtrue\Wechat\Payment; | ||
|
||
class Exception extends \Overtrue\Wechat\Exception { } |
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,94 @@ | ||
<?php | ||
/** | ||
* Notify.php | ||
* | ||
* Part of Overtrue\Wechat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Frye <frye0423@gmail.com> | ||
* @copyright 2015 Frye <frye0423@gmail.com> | ||
* @link https://github.com/0i | ||
* @link http://blog.lost-magic.com | ||
* @link https://github.com/0i/Wechat | ||
*/ | ||
|
||
namespace Overtrue\Wechat\Payment; | ||
|
||
use Overtrue\Wechat\Utils\XML; | ||
use Overtrue\Wechat\Utils\Bag; | ||
|
||
class Notify | ||
{ | ||
protected $appId; | ||
protected $appSecret; | ||
protected $mchId; | ||
protected $mchKey; | ||
|
||
/** | ||
* @var Bag | ||
*/ | ||
protected $transaction; | ||
|
||
public function __construct($appId, $appSecret, $mchId, $mchKey) { | ||
$this->appId = $appId; | ||
$this->appSecret = $appSecret; | ||
$this->mchId = $mchId; | ||
$this->mchKey = $mchKey; | ||
} | ||
|
||
/** | ||
* 验证订单消息是否合法, | ||
* 不合法返回false, 合法返回订单信息详情 | ||
* | ||
* @return bool|Bag | ||
*/ | ||
public function verify() { | ||
if (version_compare(PHP_VERSION, '5.6.0', '<')) { | ||
if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) { | ||
$xmlInput = $GLOBALS['HTTP_RAW_POST_DATA']; | ||
} else { | ||
$xmlInput = file_get_contents('php://input'); | ||
} | ||
} else { | ||
$xmlInput = file_get_contents('php://input'); | ||
} | ||
|
||
if (empty($xmlInput)) { | ||
return false; | ||
} | ||
|
||
$input = XML::parse($xmlInput); | ||
if (empty($input) || empty($input['sign'])) { | ||
return false; | ||
} | ||
|
||
$sign = $input['sign']; | ||
unset($input['sign']); ksort($input); | ||
$str = strtoupper(md5(http_build_query($input).'&key='.$this->mchKey)); | ||
if ($sign !== $str) { | ||
return false; | ||
} | ||
|
||
return $this->transaction = new Bag($input); | ||
} | ||
|
||
/** | ||
* 回复消息, 如果不回复, 微信会一直发送请求到notify_url | ||
* | ||
* @param string $code | ||
* @param string $msg | ||
* | ||
* @return string | ||
*/ | ||
public function reply($code = 'SUCCESS', $msg = 'OK') { | ||
$params = [ | ||
'return_code' => $code, | ||
'return_msg' => $msg, | ||
]; | ||
|
||
return XML::build($params); | ||
} | ||
|
||
} |
Oops, something went wrong.