完成 WeChat 公众号支付开发

This commit is contained in:
yansongda 2017-08-15 15:32:07 +08:00
parent dba611d5e5
commit cc8a2fa1f1
9 changed files with 165 additions and 18 deletions

View File

@ -35,7 +35,9 @@ $config = [
'wechat' => [
'app_id' => '',
'mch_id' => '',
'appid' => '',
'notify_url' => '',
'return_url' => '',
'key' => '',
],
];

View File

@ -9,7 +9,8 @@ interface GatewayInterface
/**
* 支付
* @author yansongda <me@yansongda.cn>
* @version 2017-08-11
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function pay(array $config_biz);
@ -17,7 +18,8 @@ interface GatewayInterface
/**
* 退款
* @author yansongda <me@yansongda.cn>
* @version 2017-08-11
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function refund(array $config_biz);
@ -25,7 +27,8 @@ interface GatewayInterface
/**
* 关闭
* @author yansongda <me@yansongda.cn>
* @version 2017-08-11
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function close(array $config_biz);
@ -33,8 +36,11 @@ interface GatewayInterface
/**
* 验证消息是否官方发出
* @author yansongda <me@yansongda.cn>
* @version 2017-08-11
* @version 2017-08-15
* @param mixed $data 待签名数组
* @param string $sign 签名字符串-支付宝服务器发送过来的原始串
* @param bool $sync 是否同步验证
* @return [type] [description]
*/
public function verify(array $data, $sign);
public function verify($data, $sign = null, $sync = false);
}

View File

@ -122,12 +122,14 @@ abstract class Alipay implements GatewayInterface
* @param bool $sync 是否同步验证
* @return [type] [description]
*/
public function verify(array $data, $sign, $sync = false)
public function verify($data, $sign = null, $sync = false)
{
if (is_null($this->user_config->get('ali_public_key'))) {
throw new InvalidArgumentException("Missing Config -- [ali_public_key]");
}
$sign = $sign ?? $data['sign']
$res = "-----BEGIN PUBLIC KEY-----\n" .
wordwrap($this->user_config->get('ali_public_key'), 64, "\n", true) .
"\n-----END PUBLIC KEY-----";
@ -187,11 +189,11 @@ abstract class Alipay implements GatewayInterface
{
$data = json_decode($this->post($this->gateway, $this->config), true);
if ($data[$method]['code'] !== '10000') {
if (! isset($data[$method]['code']) || $data[$method]['code'] !== '10000') {
throw new GatewayException(
$data[$method]['msg'] . ' - ' . $data[$method]['sub_msg'],
'get result error:' . $data[$method]['msg'] . ' - ' . $data[$method]['sub_msg'],
$data[$method]['code'],
$data[$method]);
$data);
}
return $this->verify($data[$method], $data['sign'], true);

View File

@ -0,0 +1,45 @@
<?php
namespace Yansongda\Pay\Gateways\Wechat;
/**
* 微信 - 公众号支付
*/
class AppGateway extends Wechat
{
/**
* 交易类型
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @return [type] [description]
*/
protected function getTradeType()
{
return 'APP';
}
/**
* 对外支付
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function pay(array $config_biz = [])
{
$this->config = array_merge($this->config, $config_biz);
$this->config['appid'] = $this->user_config->get('appid');
$payRequest = [
"appid" => $this->user_config->get('appid'),
'partnerid' => $this->user_config->get('partnerid'),
'prepayid' => $this->preOrder()['prepay_id'],
"timestamp" => time(),
"noncestr" => $this->createNonceStr(),
"package" => "Sign=WXPay",
];
$payRequest['sign'] = $this->getSign($payRequest);
return $payRequest;
}
}

View File

@ -7,8 +7,38 @@ namespace Yansongda\Pay\Gateways\Wechat;
*/
class ScanGateway extends Wechat
{
/**
* [getTradeType description]
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @return [type] [description]
*/
public function getTradeType()
{
return 'JSAPI';
}
/**
* 对外支付
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function pay(array $config_biz = [])
{
$this->config = array_merge($this->config, $config_biz);
$payRequest = [
"appId" => $this->user_config->get('app_id'),
"timeStamp" => time(),
"nonceStr" => $this->createNonceStr(),
"package" => "prepay_id=" . $this->preOrder()['prepay_id'],
"signType" => "MD5",
//"paySign" "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
];
$payRequest['paySign'] = $this->getSign($payRequest);
return $payRequest;
}
}

View File

@ -7,8 +7,30 @@ namespace Yansongda\Pay\Gateways\Wechat;
*/
class ScanGateway extends Wechat
{
/**
* [getTradeType description]
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @return [type] [description]
*/
public function getTradeType()
{
return 'NATIVE';
}
/**
* 对外支付,采用 「模式二」 进行支付
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @param array $config_biz [description]
* @return string 微信支付扫码 URL
*/
public function pay(array $config_biz = [])
{
$this->config = array_merge($this->config, $config_biz);
$data = $this->preOrder(),
return $data['code_url'];
}
}

View File

@ -7,8 +7,31 @@ namespace Yansongda\Pay\Gateways\Wechat;
*/
class WapGateway extends Wechat
{
/**
* [getTradeType description]
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @return [type] [description]
*/
public function getTradeType()
{
return 'MWEB';
}
/**
* 支付
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @param array $config_biz [description]
* @return [type] [description]
*/
public function pay(array $config_biz = [])
{
$this->config = array_merge($this->config, $config_biz);
$data = $this->preOrder();
return is_null($this->user_config->get('return_url')) ? $data['MWEB_URL'] : $data['MWEB_URL'] .
'&redirect_url=' . urlencode($this->config['return_url']);
}
}

View File

@ -60,26 +60,43 @@ abstract class Wechat implements GatewayInterface
* @param array $cofnig_biz [description]
* @return [type] [description]
*/
abstract public function pay(array $config_biz);
abstract public function pay(array $config_biz = []);
/**
* 验证签名
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @param string $data 待验证 xml 数据
* @param string $sign 服务器返回的签名
* @return bool 是否相符
*/
public function verify($data, $sign = null, $sync = false)
{
$data = $this->fromXml($data);
$sign = $sign ?? $data['sign'];
return $this->getSign($data) === $sign;
}
/**
* 预下单
* @author yansongda <me@yansongda.cn>
* @version 2017-08-15
* @return [type] [description]
* @return array 服务器返回结果数组
*/
protected function preOrder()
{
$data = $this->fromXml($this->get($this->preOrder_gateway, $this->config));
if ($data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS' || $data['result_code'] !== 'SUCCESS') {
throw new GatewayException(
'preOrder error:' . $data['return_msg'] . ' - ' . $data['err_code_des'],
20000,
$data);
}
if ($this->getSign($res) !== $data['sign']) {
if ($this->getSign($data) !== $data['sign']) {
throw new GatewayException(
'preOrder error: return data sign error',
20000,