mirror of
https://gitee.com/yansongda/pay.git
synced 2024-12-02 12:17:38 +08:00
完成 WeChat 公众号支付开发
This commit is contained in:
parent
dba611d5e5
commit
cc8a2fa1f1
@ -35,7 +35,9 @@ $config = [
|
|||||||
'wechat' => [
|
'wechat' => [
|
||||||
'app_id' => '',
|
'app_id' => '',
|
||||||
'mch_id' => '',
|
'mch_id' => '',
|
||||||
|
'appid' => '',
|
||||||
'notify_url' => '',
|
'notify_url' => '',
|
||||||
|
'return_url' => '',
|
||||||
'key' => '',
|
'key' => '',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -9,32 +9,38 @@ interface GatewayInterface
|
|||||||
/**
|
/**
|
||||||
* 支付
|
* 支付
|
||||||
* @author yansongda <me@yansongda.cn>
|
* @author yansongda <me@yansongda.cn>
|
||||||
* @version 2017-08-11
|
* @version 2017-08-15
|
||||||
* @return [type] [description]
|
* @param array $config_biz [description]
|
||||||
|
* @return [type] [description]
|
||||||
*/
|
*/
|
||||||
public function pay(array $config_biz);
|
public function pay(array $config_biz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退款
|
* 退款
|
||||||
* @author yansongda <me@yansongda.cn>
|
* @author yansongda <me@yansongda.cn>
|
||||||
* @version 2017-08-11
|
* @version 2017-08-15
|
||||||
* @return [type] [description]
|
* @param array $config_biz [description]
|
||||||
|
* @return [type] [description]
|
||||||
*/
|
*/
|
||||||
public function refund(array $config_biz);
|
public function refund(array $config_biz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 关闭
|
* 关闭
|
||||||
* @author yansongda <me@yansongda.cn>
|
* @author yansongda <me@yansongda.cn>
|
||||||
* @version 2017-08-11
|
* @version 2017-08-15
|
||||||
* @return [type] [description]
|
* @param array $config_biz [description]
|
||||||
|
* @return [type] [description]
|
||||||
*/
|
*/
|
||||||
public function close(array $config_biz);
|
public function close(array $config_biz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证消息是否官方发出
|
* 验证消息是否官方发出
|
||||||
* @author yansongda <me@yansongda.cn>
|
* @author yansongda <me@yansongda.cn>
|
||||||
* @version 2017-08-11
|
* @version 2017-08-15
|
||||||
* @return [type] [description]
|
* @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);
|
||||||
}
|
}
|
||||||
|
@ -122,12 +122,14 @@ abstract class Alipay implements GatewayInterface
|
|||||||
* @param bool $sync 是否同步验证
|
* @param bool $sync 是否同步验证
|
||||||
* @return [type] [description]
|
* @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'))) {
|
if (is_null($this->user_config->get('ali_public_key'))) {
|
||||||
throw new InvalidArgumentException("Missing Config -- [ali_public_key]");
|
throw new InvalidArgumentException("Missing Config -- [ali_public_key]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sign = $sign ?? $data['sign']
|
||||||
|
|
||||||
$res = "-----BEGIN PUBLIC KEY-----\n" .
|
$res = "-----BEGIN PUBLIC KEY-----\n" .
|
||||||
wordwrap($this->user_config->get('ali_public_key'), 64, "\n", true) .
|
wordwrap($this->user_config->get('ali_public_key'), 64, "\n", true) .
|
||||||
"\n-----END PUBLIC KEY-----";
|
"\n-----END PUBLIC KEY-----";
|
||||||
@ -187,11 +189,11 @@ abstract class Alipay implements GatewayInterface
|
|||||||
{
|
{
|
||||||
$data = json_decode($this->post($this->gateway, $this->config), true);
|
$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(
|
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]['code'],
|
||||||
$data[$method]);
|
$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->verify($data[$method], $data['sign'], true);
|
return $this->verify($data[$method], $data['sign'], true);
|
||||||
|
45
src/Gateways/Wechat/AppGateway.php
Normal file
45
src/Gateways/Wechat/AppGateway.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,38 @@ namespace Yansongda\Pay\Gateways\Wechat;
|
|||||||
*/
|
*/
|
||||||
class ScanGateway extends Wechat
|
class ScanGateway extends Wechat
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* [getTradeType description]
|
||||||
|
* @author yansongda <me@yansongda.cn>
|
||||||
|
* @version 2017-08-15
|
||||||
|
* @return [type] [description]
|
||||||
|
*/
|
||||||
public function getTradeType()
|
public function getTradeType()
|
||||||
{
|
{
|
||||||
return 'JSAPI';
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class MpGateway extends Wechat
|
|||||||
"appId" => $this->user_config->get('app_id'),
|
"appId" => $this->user_config->get('app_id'),
|
||||||
"timeStamp" => time(),
|
"timeStamp" => time(),
|
||||||
"nonceStr" => $this->createNonceStr(),
|
"nonceStr" => $this->createNonceStr(),
|
||||||
"package" => "prepay_id=" . $this->preOrder()['prepay_id'],
|
"package" => "prepay_id=" . $this->preOrder()['prepay_id'],
|
||||||
"signType" => "MD5",
|
"signType" => "MD5",
|
||||||
//"paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
|
//"paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
|
||||||
];
|
];
|
||||||
|
@ -7,8 +7,30 @@ namespace Yansongda\Pay\Gateways\Wechat;
|
|||||||
*/
|
*/
|
||||||
class ScanGateway extends Wechat
|
class ScanGateway extends Wechat
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* [getTradeType description]
|
||||||
|
* @author yansongda <me@yansongda.cn>
|
||||||
|
* @version 2017-08-15
|
||||||
|
* @return [type] [description]
|
||||||
|
*/
|
||||||
public function getTradeType()
|
public function getTradeType()
|
||||||
{
|
{
|
||||||
return 'NATIVE
';
|
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'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,31 @@ namespace Yansongda\Pay\Gateways\Wechat;
|
|||||||
*/
|
*/
|
||||||
class WapGateway extends Wechat
|
class WapGateway extends Wechat
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* [getTradeType description]
|
||||||
|
* @author yansongda <me@yansongda.cn>
|
||||||
|
* @version 2017-08-15
|
||||||
|
* @return [type] [description]
|
||||||
|
*/
|
||||||
public function getTradeType()
|
public function getTradeType()
|
||||||
{
|
{
|
||||||
return 'MWEB';
|
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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,26 +60,43 @@ abstract class Wechat implements GatewayInterface
|
|||||||
* @param array $cofnig_biz [description]
|
* @param array $cofnig_biz [description]
|
||||||
* @return [type] [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>
|
* @author yansongda <me@yansongda.cn>
|
||||||
* @version 2017-08-15
|
* @version 2017-08-15
|
||||||
* @return [type] [description]
|
* @return array 服务器返回结果数组
|
||||||
*/
|
*/
|
||||||
protected function preOrder()
|
protected function preOrder()
|
||||||
{
|
{
|
||||||
$data = $this->fromXml($this->get($this->preOrder_gateway, $this->config));
|
$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(
|
throw new GatewayException(
|
||||||
'preOrder error:' . $data['return_msg'] . ' - ' . $data['err_code_des'],
|
'preOrder error:' . $data['return_msg'] . ' - ' . $data['err_code_des'],
|
||||||
20000,
|
20000,
|
||||||
$data);
|
$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->getSign($res) !== $data['sign']) {
|
if ($this->getSign($data) !== $data['sign']) {
|
||||||
throw new GatewayException(
|
throw new GatewayException(
|
||||||
'preOrder error: return data sign error',
|
'preOrder error: return data sign error',
|
||||||
20000,
|
20000,
|
||||||
|
Loading…
Reference in New Issue
Block a user