merge to dev: 优化代码 + 增加支付宝扫码支付&刷卡支付

This commit is contained in:
yansongda 2017-08-23 16:19:22 +08:00
commit 4de3e18b3b
10 changed files with 222 additions and 28 deletions

View File

@ -47,6 +47,8 @@ laravel 扩展包请 [传送至这里](https://github.com/yansongda/laravel-pay)
- 电脑支付
- 手机网站支付
- APP 支付
- 刷卡支付
- 扫码支付
SDK 中对应的 driver 和 gateway 如下表所示:
@ -55,6 +57,8 @@ SDK 中对应的 driver 和 gateway 如下表所示:
| alipay | web | 电脑支付 |
| alipay | wap | 手机网站支付 |
| alipay | app | APP 支付 |
| alipay | pos | 刷卡支付 |
| alipay | scan | 扫码支付 |
### 2、微信
@ -182,10 +186,10 @@ class PayController extends Controller
{
protected $config = [
'wechat' => [
'app_id' => 'wxb3f6d068e382c63d',
'mch_id' => '1457768302',
'app_id' => 'wxb3f6xxxxxxxxxx',
'mch_id' => '1457xxxxx2',
'notify_url' => 'http://yansongda.cn/wechat_notify.php',
'key' => 'mF2suE9sU6Mk1C7GXnT5H12315645645',
'key' => 'mF2suE9sU6Mk1Cxxxxxxxxxx45',
'cert_client' => './apiclient_cert.pem',
'cert_key' => './apiclient_key.pem',
],
@ -197,7 +201,7 @@ class PayController extends Controller
'out_trade_no' => 'e2',
'total_fee' => '0.01',
'body' => 'test body',
'spbill_create_ip' => '14.213.156.207',
'spbill_create_ip' => '8.8.8.8',
'openid' => 'onkVf1FjWS5SBIihS-123456_abc',
];
@ -206,10 +210,10 @@ class PayController extends Controller
return $pay->driver('wechat')->gateway('mp')->pay($config_biz);
}
public function notify()
public function notify(Request $request)
{
$pay = new Pay($this->config);
$verify = $p->driver('wechat')->gateway('mp')->verify(file_get_contents('php://input'));
$verify = $p->driver('wechat')->gateway('mp')->verify($request->getContent());
if ($verify) {
file_put_contents('notify.txt', "收到来自微信的异步通知\r\n", FILE_APPEND);

View File

@ -77,15 +77,13 @@ abstract class Alipay implements GatewayInterface
*
* @return string [description]
*/
public function pay(array $config_biz = [])
abstract public function pay($config_biz)
{
$config_biz['product_code'] = $this->getPayProductCode();
$config_biz['product_code'] = $this->getProductCode();
$this->config['method'] = $this->getPayMethod();
$this->config['method'] = $this->getMethod();
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['sign'] = $this->getSign();
return $this->buildPayHtml();
}
/**
@ -161,7 +159,7 @@ abstract class Alipay implements GatewayInterface
*
* @param array $data 待签名数组
* @param string $sign 签名字符串-支付宝服务器发送过来的原始串
* @param bool $sync 是否同步验证
* @param bool $sync 是否同步返回验证
*
* @return array|boolean [description]
*/
@ -191,7 +189,7 @@ abstract class Alipay implements GatewayInterface
*
* @return string [description]
*/
abstract protected function getPayMethod();
abstract protected function getMethod();
/**
* [getProductCode description].
@ -202,7 +200,7 @@ abstract class Alipay implements GatewayInterface
*
* @return string [description]
*/
abstract protected function getPayProductCode();
abstract protected function getProductCode();
/**
* [buildHtmlPay description]
@ -290,7 +288,7 @@ abstract class Alipay implements GatewayInterface
* @version 2017-08-11
*
* @param array $toBeSigned [description]
* @param boolean $verify 是否异步同时验证签名
* @param boolean $verify 是否验证签名
*
* @return string [description]
*/

View File

@ -16,7 +16,7 @@ class AppGateway extends Alipay
*
* @return string [description]
*/
protected function getPayMethod()
protected function getMethod()
{
return 'alipay.trade.app.pay';
}
@ -30,7 +30,7 @@ class AppGateway extends Alipay
*
* @return string [description]
*/
protected function getPayProductCode()
protected function getProductCode()
{
return 'QUICK_MSECURITY_PAY';
}
@ -48,11 +48,7 @@ class AppGateway extends Alipay
*/
public function pay(array $config_biz = [])
{
$config_biz['product_code'] = $this->getPayProductCode();
$this->config['method'] = $this->getPayMethod();
$this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE);
$this->config['sign'] = $this->getSign();
parent::pay($config_biz);
return http_build_query($this->config);
}

View File

@ -0,0 +1,53 @@
<?php
namespace Yansongda\Pay\Gateways\Alipay;
class PosGateway extends Alipay
{
/**
* [getMethod description].
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-10
*
* @return string [description]
*/
protected function getMethod()
{
return 'alipay.trade.pay';
}
/**
* [getProductCode description].
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-10
*
* @return string [description]
*/
protected function getProductCode()
{
return 'FACE_TO_FACE_PAYMENT';
}
/**
* 刷卡支付.
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-23
*
* @param array $config_biz 订单信息
* @param string $scene 支付场景,默认二维码
*
* @return array|boolean [description]
*/
public function pay(array $config_biz = [], $scene = 'bar_code')
{
$config_biz['scene'] = $scene;
return $this->getResult($config_biz, $this->getMethod());
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Yansongda\Pay\Gateways\Alipay;
class ScanGateway extends Alipay
{
/**
* [getMethod description].
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-10
*
* @return string [description]
*/
protected function getMethod()
{
return 'alipay.trade.precreate';
}
/**
* [getProductCode description].
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-10
*
* @return string [description]
*/
protected function getProductCode()
{
return '';
}
/**
* 扫码支付.
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-23
*
* @param array $config_biz 订单信息
*
* @return array|boolean [description]
*/
public function pay(array $config_biz = [])
{
return $this->getResult($config_biz, $this->getMethod());
}
}

View File

@ -16,7 +16,7 @@ class WapGateway extends Alipay
*
* @return string [description]
*/
protected function getPayMethod()
protected function getMethod()
{
return 'alipay.trade.wap.pay';
}
@ -30,8 +30,26 @@ class WapGateway extends Alipay
*
* @return string [description]
*/
protected function getPayProductCode()
protected function getProductCode()
{
return 'QUICK_WAP_WAY';
}
/**
* 对外支付.
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-23
*
* @param [type] $config_biz [description]
*
* @return [type] [description]
*/
public function pay(array $config_biz = [])
{
parent::pay($config_biz);
return $this->buildPayHtml();
}
}

View File

@ -16,7 +16,7 @@ class WebGateway extends Alipay
*
* @return string [description]
*/
protected function getPayMethod()
protected function getMethod()
{
return 'alipay.trade.page.pay';
}
@ -30,8 +30,26 @@ class WebGateway extends Alipay
*
* @return string [description]
*/
protected function getPayProductCode()
protected function getProductCode()
{
return 'FAST_INSTANT_TRADE_PAY';
}
/**
* 对外支付.
*
* @author yansongda <me@yansongda.cn>
*
* @version 2017-08-23
*
* @param [type] $config_biz [description]
*
* @return [type] [description]
*/
public function pay(array $config_biz = [])
{
parent::pay($config_biz);
return $this->buildPayHtml();
}
}

View File

@ -72,7 +72,7 @@ class Config implements ArrayAccess
*/
public function set(string $key, $value)
{
if (is_null($key) || $key == '') {
if ($key == '') {
throw new InvalidArgumentException('Invalid config key.');
}
@ -86,7 +86,7 @@ class Config implements ArrayAccess
$this->config[$keys[0]][$keys[1]] = $value;
break;
case '3':
$this->config[$keys[0]][$keys[1]][$keys[3]] = $value;
$this->config[$keys[0]][$keys[1]][$keys[2]] = $value;
break;
default:

View File

@ -1,3 +1,52 @@
<?php
namespace Yansongda\Pay\Tests\Support;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Pay\Support\Config;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
class ConfigTest extends TestCase
{
public function testGetConfig()
{
$array = [
'foo' => 'bar',
'bar' => [
'id' => '18',
'key' => [
'public' => 'qwer',
'private' => 'asdf',
],
],
];
$config = new Config($array);
$this->assertTrue(isset($config['foo']));
$this->assertSame('bar', $config['foo']);
$this->assertSame('bar', $config->get('foo'));
$this->assertSame($array, $config->get());
$this->assertSame('qwer', $config->get('bar.key.public'));
$this->assertNull($config->get('bar.key.public.foo'));
$this->assertNull($config->get('bar.foo.foo.foo'));
}
public function testSetConfig()
{
$config = new Config([]);
$this->assertArrayHasKey('foo', $config->set('foo', 'bar'));
$this->assertSame('bar', $config->get('foo'));
$this->assertArrayHasKey('bar', $config->set('bar.key.public', 'qwer'));
$this->assertSame('qwer', $config->get('bar.key.public'));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid config key.');
$config->set('', '');
$this->assertArrayHasKey('error', $config->set('error.foo.foo.foo.foo', 'foo'));
}
}

View File

@ -2,3 +2,11 @@
namespace Yansongda\Pay\Tests\Traits;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
class HasHttpRequestTest extends TestCase
{
}