Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 9, 2018
2 parents 0c81f74 + 1ba6d90 commit 34d785c
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 18 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# PaySDK

PaySDK 是 PHP 集成支付 SDK ,集成了支付宝、微信支付的支付接口和其它相关接口的操作。可以轻松嵌入支持 PHP >= 5.4 的任何系统中。
[![Latest Version](https://img.shields.io/packagist/v/yurunsoft/yurun-http.svg)](https://packagist.org/packages/yurunsoft/pay-sdk)
[![IMI Doc](https://img.shields.io/badge/docs-passing-green.svg)](http://doc.yurunsoft.com/PaySDK)
[![IMI License](https://img.shields.io/github/license/Yurunsoft/YurunHttp.svg)](https://github.com/Yurunsoft/PaySDK/blob/master/LICENSE)

## 介绍

PaySDK 是 PHP 集成支付 SDK ,集成了支付宝、微信支付的支付接口和其它相关接口的操作。可以轻松嵌入支持 PHP >= 5.4 的任何系统中,2.0 版现已支持 Swoole 协程环境。

我们有完善的在线技术文档:[http://doc.yurunsoft.com/PaySDK](http://doc.yurunsoft.com/PaySDK)

API 文档:[https://apidoc.gitee.com/yurunsoft/PaySDK](https://apidoc.gitee.com/yurunsoft/PaySDK)

同时欢迎各位加入技术支持群:74401592 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png "点击加群")](https://shang.qq.com/wpa/qunwpa?idkey=e2e6b49e9a648aae5285b3aba155d59107bb66fde02e229e078bd7359cac8ac3),如有问题可以及时解答和修复。

大家在开发中肯定会对接各种各样的支付平台,我个人精力有限,欢迎各位来提交 PR ([码云](https://gitee.com/yurunsoft/PaySDK)/[Github](https://github.com/Yurunsoft/PaySDK)),一起完善 PaySDK ,让它能够支持更多的支付平台,更加好用。
Expand Down Expand Up @@ -116,4 +124,10 @@ else
var_dump($pay->getErrorCode() . ':' . $pay->getError());
}
exit;
```
```

### Swoole 协程环境支持

```php
\Yurun\Util\YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole');
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"require": {
"php": "^5.4|^7.0",
"yurunsoft/yurun-http" : "1.3.*"
"yurunsoft/yurun-http" : "~3.0"
}
}
14 changes: 13 additions & 1 deletion src/AlipayCrossBorder/Online/Notify/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public function reply($success, $message = '')
{
if($success)
{
echo 'success';
$result = 'success';
if(null === $this->swooleResponse)
{
echo $result;
}
else
{
$this->swooleResponse->end($result);
}
}
}

Expand All @@ -36,6 +44,10 @@ public function reply($success, $message = '')
*/
public function getNotifyData()
{
if(null !== $this->swooleRequest)
{
return $this->swooleRequest->post;
}
return $_POST;
}

Expand Down
4 changes: 4 additions & 0 deletions src/AlipayCrossBorder/Online/Notify/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ abstract class Sync extends Base
*/
public function getNotifyData()
{
if(null !== $this->swooleRequest)
{
return $this->swooleRequest->get;
}
return $_GET;
}
}
41 changes: 31 additions & 10 deletions src/Base.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Yurun\PaySDK;

use \Yurun\Until\HttpRequest;
use \Yurun\Util\HttpRequest;
use Yurun\PaySDK\Lib\XML;

/**
Expand All @@ -11,13 +11,13 @@ abstract class Base
{
/**
* HttpRequest
* @var \Yurun\Until\HttpRequest
* @var \Yurun\Util\HttpRequest
*/
public $http;

/**
* 接口请求的返回结果
* @var \Yurun\Until\HttpResponse
* @var \Yurun\Util\YurunHttp\Http\Response
*/
public $response;

Expand Down Expand Up @@ -45,6 +45,20 @@ abstract class Base
*/
public $result;

/**
* swoole 请求类
*
* @var \swoole_http_request
*/
public $swooleRequest;

/**
* swoole 响应类
*
* @var \swoole_http_response
*/
public $swooleResponse;

public function __construct($publicParams)
{
$this->publicParams = $publicParams;
Expand All @@ -69,13 +83,13 @@ public function execute($params, $format = 'JSON')
switch($format)
{
case 'JSON':
$this->result = \json_decode($this->response->body, true);
$this->result = $this->response->json(true);
break;
case 'XML':
$this->result = XML::fromString($this->response->body);
$this->result = XML::fromString($this->response->body());
break;
default:
$this->result = $this->response->body;
$this->result = $this->response->body();
}
if($params->_isSyncVerify && !$this->verifySync($params, $this->result))
{
Expand Down Expand Up @@ -201,10 +215,17 @@ public function redirectExecute($params)
}
$this->requestData = $data;
$url .= \http_build_query($data, '', '&');
header('HTTP/1.1 302 Temporarily Moved');
header('Status: 302 Temporarily Moved');
header('Location: ' . $url);
exit;
if(null === $this->swooleResponse)
{
header('HTTP/1.1 302 Temporarily Moved');
header('Status: 302 Temporarily Moved');
header('Location: ' . $url);
exit;
}
else
{
$this->swooleResponse->redirect($url, 302);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/NotifyBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*/
abstract class NotifyBase
{
/**
* 通知数据
* @var array
*/
public $data;

/**
Expand All @@ -22,6 +26,20 @@ abstract class NotifyBase
*/
public $replyData;

/**
* swoole 请求类
*
* @var \swoole_http_request
*/
public $swooleRequest;

/**
* swoole 响应类
*
* @var \swoole_http_response
*/
public $swooleResponse;

public function __construct()
{

Expand Down
13 changes: 12 additions & 1 deletion src/Weixin/Notify/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public function reply($success, $message = '')
{
$this->replyData->return_code = $success ? 'SUCCESS' : 'FAIL';
$this->replyData->return_msg = $message;
echo $this->replyData;
if(null === $this->swooleResponse)
{
echo $this->replyData;
}
else
{
$this->swooleResponse->end($this->replyData->toString());
}
}

/**
Expand All @@ -36,6 +43,10 @@ public function reply($success, $message = '')
*/
public function getNotifyData()
{
if(null !== $this->swooleRequest)
{
return XML::fromString($this->swooleRequest->rawContent());
}
return XML::fromString(\file_get_contents('php://input'));
}

Expand Down
4 changes: 2 additions & 2 deletions test/AlipayApp/download_bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
var_dump($data);

// 下载对账单
$download = new \Yurun\Until\Download($data['alipay_data_dataservice_bill_downloadurl_query_response']['bill_download_url']);
$download->download(__DIR__ . '/test.zip');
$http = new \Yurun\Util\HttpRequest;
$http->download(__DIR__ . '/test.zip', $data['alipay_data_dataservice_bill_downloadurl_query_response']['bill_download_url']);
2 changes: 1 addition & 1 deletion test/AlipayApp/ftf_qr.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
var_dump('error:', $pay->getError(), 'error_code:', $pay->getErrorCode());
}
catch(Exception $e){
var_dump($pay->response->body);
var_dump($pay->response->body());
}
// 下面二维码为演示方便随便找了个二维码接口,如有需要你可以自己生成二维码或者使用其它的二维码接口
?>
Expand Down
109 changes: 109 additions & 0 deletions test/Swoole/weixinNative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Swoole 协程 Demo
* 请先安装 Swoole 扩展
* 运行方式:php test/Swoole/weixinNative.php
*
* 请勿直接将本文件用于生产环境,仅作为演示用
* Swoole 暂时仅有微信扫码支付(模式二)+支付异步通知演示,但其实用法和传统方式基本一致
*/
require dirname(__DIR__) . '/common.php';

use Yurun\Util\YurunHttp;

$GLOBALS['PAY_CONFIG'] = array(
'appid' => '',
'mch_id' => '',
'key' => '',
'pay_notify_url' => 'http://yurun.test.com/test/Weixin/pay_notify.php',
'certPath' => __DIR__ . '/cert/apiclient_cert.pem',
'keyPath' => __DIR__ . '/cert/apiclient_key.pem',
);

// 设置 Http 请求处理器为 Swoole
YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole');

class PayNotify extends \Yurun\PaySDK\Weixin\Notify\Pay
{
/**
* 后续执行操作
* @return void
*/
protected function __exec()
{
// 支付成功处理,一般做订单处理,$this->data 是从微信发送来的数据
file_put_contents(__DIR__ . '/notify_result.txt', date('Y-m-d H:i:s') . ':' . var_export($this->data, true));
// 告诉微信我处理过了,不要再通过了
$this->reply(true, 'OK');
}
}

$server = new swoole_http_server('0.0.0.0', 80);
$server->on('request', function ($request, $response) {
switch($request->server['request_uri'])
{
case '/unifiedorder':
unifiedorder($request, $response);
break;
case '/pay_notify':
payNotify($request, $response);
break;
default:
$response->end('404');
break;
}
});
$server->start();

function unifiedorder($request, $response)
{
// 公共配置
$params = new \Yurun\PaySDK\Weixin\Params\PublicParams;
$params->appID = $GLOBALS['PAY_CONFIG']['appid'];
$params->mch_id = $GLOBALS['PAY_CONFIG']['mch_id'];
$params->key = $GLOBALS['PAY_CONFIG']['key'];

// SDK实例化,传入公共配置
$pay = new \Yurun\PaySDK\Weixin\SDK($params);
// 这2行很重要,和传统用法的差异处
$pay->swooleRequest = $request;
$pay->swooleResponse = $response;

// 支付接口
$request = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request;
$request->body = 'test'; // 商品描述
$request->out_trade_no = 'test' . mt_rand(10000000,99999999); // 订单号
$request->total_fee = 1; // 订单总金额,单位为:分
$request->spbill_create_ip = '127.0.0.1'; // 客户端ip
$request->notify_url = $GLOBALS['PAY_CONFIG']['pay_notify_url']; // 异步通知地址

// 调用接口
$result = $pay->execute($request);
$shortUrl = $result['code_url'];
$response->end(json_encode([
'message' => 'weixin qr scan url:',
'url' => $shortUrl,
]));
}

function payNotify($request, $response)
{
// 公共配置
$params = new \Yurun\PaySDK\Weixin\Params\PublicParams;
$params->appID = $GLOBALS['PAY_CONFIG']['appid'];
$params->mch_id = $GLOBALS['PAY_CONFIG']['mch_id'];
$params->key = $GLOBALS['PAY_CONFIG']['key'];

// SDK实例化,传入公共配置
$sdk = new \Yurun\PaySDK\Weixin\SDK($params);

$payNotify = new PayNotify;
// 这2行很重要,和传统用法的差异处
$payNotify->swooleRequest = $request;
$payNotify->swooleResponse = $response;
try{
$sdk->notify($payNotify);
}catch(Exception $e){
file_put_contents(__DIR__ . '/notify_result.txt', $e->getMessage() . ':' . var_export($payNotify->data, true));
}
}

0 comments on commit 34d785c

Please sign in to comment.