Skip to content

Commit

Permalink
示例应用支持支付宝支付
Browse files Browse the repository at this point in the history
  • Loading branch information
brjunsuc committed Feb 15, 2023
1 parent 975e578 commit ece967a
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 79 deletions.
1 change: 1 addition & 0 deletions addons/demo_rgapi/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
`no` varchar(255) NOT NULL DEFAULT '' COMMENT '商户订单号',
`code` varchar(255) NOT NULL DEFAULT '' COMMENT '二维码链接',
`status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态',
`type` tinyint(1) NOT NULL DEFAULT 1 COMMENT '支付类型 1:微信;2:支付宝;',
`createtime` int(11) NOT NULL COMMENT '创建时间',
`updatetime` int(11) NOT NULL DEFAULT 0 COMMENT '更新时间',
`uid` varchar(255) NOT NULL DEFAULT '' COMMENT '用户标识',
Expand Down
3 changes: 2 additions & 1 deletion addons/demo_rgapi/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<entry title="code换token" do="code_to_token" />
<entry title="其他" do="other" />
<entry title="小程序-日记列表" do="list" state="" direct="false" />
<entry title="微信支付" do="pay" />
<entry title="微信支付" do="wechatpay" />
<entry title="支付宝支付" do="alipay" />
</menu>
</bindings>
<permissions>
Expand Down
74 changes: 57 additions & 17 deletions addons/demo_rgapi/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,52 @@ public function doWebOther() {
include $this->template('other');
}

public function doWebPay() {
$data = pdo_getall('demo_rgapi_paylog', array(), '', '', 'id DESC');
public function doWebWechatpay() {
global $_W;
$data = pdo_getall('demo_rgapi_paylog', array('type' => 1), '', '', 'id DESC');
foreach ($data as $key => &$value) {
$value['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
}
include $this->template('pay');
include $this->template('wechatpay');
}

public function doWebWechatPay() {
public function doWebAlipay() {
global $_W;
$data = pdo_getall('demo_rgapi_paylog', array('type' => 2), '', '', 'id DESC');
foreach ($data as $key => &$value) {
$value['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
}
include $this->template('alipay');
}

public function doWebPay() {
global $_W, $_GPC;
try {
$type = safe_gpc_string($_GPC['type']);
if (empty($type) || !in_array($type, array('wechat', 'ali'))) {
iajax(-1, '支付类型错误!');
}
load()->library('sdk-module');
$api = new \W7\Sdk\Module\Api($_W['setting']['server_setting']['app_id'], $_W['setting']['server_setting']['app_secret'], "1");
$pay = $api->wechatPay($_W['siteroot'] . 'payment/wechat/notify.php');
$out_trade_no = 'demo' . date('YmdHis', time()) . time() . rand(1111, 9999);
$data = $pay->payTransactionsNative("测试支付", $out_trade_no, 1, array('attach' => json_encode(array('uniacid' => $_W['uniacid']))))->toArray();
if (empty($data['code_url'])) {
iajax(-1, '支付失败!');
$out_trade_no = $type . date('YmdHis', time()) . time() . rand(11, 99);
if ('wechat' == $type) {
$pay = $api->wechatPay($_W['siteroot'] . 'payment/wechat/notify.php');
$data = $pay->payTransactionsNative("测试支付", $out_trade_no, 1, array('attach' => json_encode(array('uniacid' => $_W['uniacid']))))->toArray();
if (empty($data['code_url'])) {
iajax(-1, '支付失败!');
}
$code = $data['code_url'];
} else {
$pay = $api->aliPay($_W['siteroot'] . 'payment/alipay/notify.php');
$data = $pay->payForPc("测试支付", $out_trade_no, 0.01)->toArray();
if (empty($data['data'])) {
iajax(-1, '支付失败!');
}
$code = $data['data'];
}

$log = array(
'type' => 'wechat',
'type' => 'wechat' == $type ? 'wechat' : 'alipay',
'uniacid' => $_W['uniacid'],
'acid' => $_W['acid'],
'openid' => $_W['member']['uid'],
Expand All @@ -79,30 +104,40 @@ public function doWebWechatPay() {
pdo_insert('core_paylog', $log);
$insert = array(
'no' => $out_trade_no,
'code' => $data['code_url'],
'code' => $code,
'status' => 0,
'type' => 'wechat' == $type ? 1 : 2,
'createtime' => TIMESTAMP,
'updatetime' => TIMESTAMP,
'uid' => $_W['uid'],
'uniacid' => $_W['uniacid'],
);
pdo_insert('demo_rgapi_paylog', $insert);
iajax(0, array('code' => $data['code_url'], 'no' => $out_trade_no));
iajax(0, array('type' => $type, 'code' => $code, 'no' => $out_trade_no));
} catch (Exception $e) {
iajax(-1, '支付失败!错误详情: ' . $e->getMessage());
}
}

public function doWebWechatRefund() {
public function doWebRefund() {
global $_W, $_GPC;
try {
$type = safe_gpc_string($_GPC['type']);
if (empty($type) || !in_array($type, array('wechat', 'ali'))) {
iajax(-1, '退款类型错误!');
}
load()->library('sdk-module');
$api = new \W7\Sdk\Module\Api($_W['setting']['server_setting']['app_id'], $_W['setting']['server_setting']['app_secret'], "1");
$pay = $api->wechatPay($_W['siteroot'] . 'payment/wechat/refund.php');
$out_trade_no = safe_gpc_string($_GPC['__input']['no']);
$data = $pay->refund($out_trade_no, 1, 1, '', $out_trade_no)->toArray();
if (!empty($data['status']) && 'SUCCESS' == $data['status']) {
iajax(0, '已申请退款!');
if ('wechat' == $type) {
$pay = $api->wechatPay($_W['siteroot'] . 'payment/wechat/refund.php');
$data = $pay->refund($out_trade_no, 1, 1, '', $out_trade_no)->toArray();
if (!empty($data['status']) && 'SUCCESS' == $data['status']) {
iajax(0, '已申请退款!');
}
} else {
$pay = $api->aliPay($_W['siteroot'] . 'payment/alipay/refund.php');
$data = $pay->refund($out_trade_no, 0.01)->toArray();
}
$refund = array(
'uniacid' => $_W['uniacid'],
Expand All @@ -113,6 +148,11 @@ public function doWebWechatRefund() {
'reason' => '',
);
pdo_insert('core_refundlog', $refund);
if ('ali' == $type && !empty($data['alipay_trade_refund_response']) && 'Success' == $data['alipay_trade_refund_response']['msg']) {
pdo_update('core_refundlog', array('status' => 1), array('refund_uniontid' => $out_trade_no));
pdo_update('demo_rgapi_paylog', array('status' => 2), array('no' => $out_trade_no));
iajax(0, '退款成功!', referer());
}
iajax(0, '已发起退款申请,预计1分钟后退款成功!');
} catch (Exception $e) {
iajax(-1, '退款失败!错误详情: ' . $e->getMessage());
Expand Down
109 changes: 109 additions & 0 deletions addons/demo_rgapi/template/alipay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{template 'common/header'}
<div class="alert we7-page-alert">
<p>支付功能需先配置支付参数:软擎授权系统->进入对应关联的号码->配置->支付参数</p>
</div>
<div id="bind" ng-controller="ctrl">
<a href="javascript:;" class="btn btn-primary" style="margin-bottom: 8px;" ng-click="pay(2)">创建支付宝支付</a>
<a href="javascript:location.reload();" class="btn btn-default pull-right">刷新</a>
<table class="table we7-table table-hover vertical-middle">
<col width="250px"/>
<col width="250px" />
<col width="100px" />
<col width="50px" />
<tr>
<th>订单号</th>
<th>创建时间</th>
<th>状态</th>
<th class="text-center">操作</th>
</tr>
<tr ng-repeat="item in data">
<td>{{item.no}}</td>
<td>{{item.createtime}}</td>
<td>
<span ng-if="1 == item.status">已支付</span>
<span ng-if="2 == item.status">已退款</span>
<span ng-if="0 == item.status">未支付</span>
</td>
<td class="text-center">
<div class="link-group">
<a href="javascript:;" ng-if="1 == item.status" ng-click="refund(item.no)">退款</a>
<a href="javascript:;" ng-if="2 == item.status">-</a>
<a href="javascript:;" ng-if="0 == item.status" ng-click="pay(1, item.code, item.no)">支付</a>
</div>
</td>
</tr>
</table>
</div>

<script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<script>
angular.module('myApp', []);
angular.module('myApp').value('config', {
'data': {php echo !empty($data) ? json_encode($data) : '[]'},
'links': {
'payUrl': "{php echo url('site/entry/pay', array('module_name' => 'demo_rgapi', 'type' => 'ali'))}",
'refundUrl': "{php echo url('site/entry/refund', array('module_name' => 'demo_rgapi', 'type' => 'ali'))}",
'payStatusUrl': "{php echo url('site/entry/paystatus', array('module_name' => 'demo_rgapi'))}",
}
});
angular.module('myApp').controller('ctrl', ['$scope', '$http', 'config', '$interval', function($scope, $http, config, $interval) {
$scope.data = config.data;

$scope.pay = function(type, code = '', no = '') {
if (2 == type) {
$http.post(config.links.payUrl)
.success(function (data) {
if (0 != data.message.errno) {
util.message(data.message.message, '', 'error');
return '';
}
// $scope.payStatus(data.message.message.no);
util.confirm(function () {
location.reload();
}, function () {
return false;
}, '是否已完成支付?');
window.open(data.message.message.code);
return false;
})
} else {
// $scope.payStatus(no);
util.confirm(function () {
location.reload();
}, function () {
return false;
}, '是否已完成支付?');
window.open(code);
return false;
}
};

$scope.payStatus = function(no) {
$interval(function() {
$http.post(config.links.payStatusUrl, {no: no})
.success(function (data) {
if (0 == data.message.errno) {
util.message(data.message.message, data.redirect);
}
})
}, 3000);
};

$scope.refund = function(no) {
util.confirm(function () {
$http.post(config.links.refundUrl, {no: no})
.success(function (data) {
if (0 == data.message.errno) {
util.message(data.message.message, data.redirect);
return '';
}
util.message(data.message.message, '', 'error');
})
}, function () {
return false;
}, '确认要发起退款申请吗?');
};
}]);
angular.bootstrap($('#bind'), ['myApp']);
</script>
{template 'common/footer'}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{template 'common/header'}
<div class="alert we7-page-alert">
<p>支付功能需先配置支付参数:软擎授权系统->进入对应关联的号码->配置->支付参数</p>
</div>
<div id="bind" ng-controller="ctrl">
<a href="javascript:;" class="btn btn-primary" style="margin-bottom: 8px;" ng-click="pay(2)">创建支付</a>
<a href="javascript:;" class="btn btn-primary" style="margin-bottom: 8px;" ng-click="pay(2)">创建微信支付</a>
<a href="javascript:location.reload();" class="btn btn-default pull-right">刷新</a>
<table class="table we7-table table-hover vertical-middle">
<col width="250px"/>
Expand Down Expand Up @@ -57,8 +60,8 @@
angular.module('myApp').value('config', {
'data': {php echo !empty($data) ? json_encode($data) : '[]'},
'links': {
'payUrl': "{php echo url('site/entry/wechatpay', array('module_name' => 'demo_rgapi'))}",
'refundUrl': "{php echo url('site/entry/wechatrefund', array('module_name' => 'demo_rgapi'))}",
'payUrl': "{php echo url('site/entry/pay', array('module_name' => 'demo_rgapi', 'type' => 'wechat'))}",
'refundUrl': "{php echo url('site/entry/refund', array('module_name' => 'demo_rgapi', 'type' => 'wechat'))}",
'payStatusUrl': "{php echo url('site/entry/paystatus', array('module_name' => 'demo_rgapi'))}",
}
});
Expand Down
101 changes: 43 additions & 58 deletions payment/alipay/notify.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,52 @@
error_reporting(0);
define('IN_MOBILE', true);

if (!empty($_POST)) {
$out_trade_no = $_POST['out_trade_no'];
require '../../framework/bootstrap.inc.php';
require '../../framework/bootstrap.inc.php';
$input = file_get_contents('php://input');
if (!empty($input)) {
WeUtility::logging('pay-alipay', var_export($input, true));
load()->web('common');
load()->classs('coupon');
$_W['uniacid'] = $_W['weid'] = intval($_POST['body']);
$_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);
$_W['acid'] = $_W['uniaccount']['acid'];
$setting = uni_setting($_W['uniacid'], array('payment'));
if (is_array($setting['payment'])) {
$alipay = $setting['payment']['alipay'];
if (!empty($alipay)) {
$prepares = array();
foreach ($_POST as $key => $value) {
if ($key != 'sign' && $key != 'sign_type') {
$prepares[] = "{$key}={$value}";
}
}
sort($prepares);
$string = implode('&', $prepares);
$string .= $alipay['secret'];
$sign = md5($string);
if ($sign == $_POST['sign']) {
$_POST['query_type'] = 'notify';
WeUtility::logging('pay-alipay', var_export($_POST, true));
$log = table('core_paylog')
->where(array('uniontid' => $out_trade_no))
->get();
//此处判断微信请求消息金额必须与系统发起的金额一致
if (!empty($log) && $log['status'] == '0' && ($_POST['total_fee'] == $log['card_fee'])) {
$log['transaction_id'] = $_POST['trade_no'];
$record = array();
$record['status'] = '1';
table('core_paylog')
->where(array('plid' => $log['plid']))
->fill($record)
->save();
load()->library('sdk-module');
$appEncryptor = new \W7\Sdk\Module\Support\AppEncryptor($_W['setting']['server_setting']['app_id'], $_W['setting']['server_setting']['token'], $_W['setting']['server_setting']['encodingaeskey']);
$data = $appEncryptor->decrypt($input);
if (!empty($data['trade_status']) && 'TRADE_SUCCESS' == $data['trade_status']) {
$log = table('core_paylog')
->where(array('uniontid' => $data['out_trade_no']))
->get();
$_W['uniacid'] = $_W['weid'] = intval($log['uniacid']);
$_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);

if (!empty($log) && $log['status'] == '0' && ($data['total_amount'] == $log['card_fee'])) {
$log['transaction_id'] = $data['trade_no'];
$record = array();
$record['status'] = '1';
table('core_paylog')
->where(array('plid' => $log['plid']))
->fill($record)
->save();

$site = WeUtility::createModuleSite($log['module']);
if (!is_error($site)) {
$method = 'payResult';
if (method_exists($site, $method)) {
$ret = array();
$ret['weid'] = $log['weid'];
$ret['uniacid'] = $log['uniacid'];
$ret['result'] = 'success';
$ret['type'] = $log['type'];
$ret['from'] = 'notify';
$ret['tid'] = $log['tid'];
$ret['uniontid'] = $log['uniontid'];
$ret['transaction_id'] = $log['transaction_id'];
$ret['user'] = $log['openid'];
$ret['fee'] = $log['fee'];
$ret['is_usecard'] = $log['is_usecard'];
$ret['card_type'] = $log['card_type'];
$ret['card_fee'] = $log['card_fee'];
$ret['card_id'] = $log['card_id'];
$site->$method($ret);
exit('success');
}
}
$site = WeUtility::createModuleSite($log['module']);
if (!is_error($site)) {
$method = 'payResult';
if (method_exists($site, $method)) {
$ret = array();
$ret['weid'] = $log['weid'];
$ret['uniacid'] = $log['uniacid'];
$ret['result'] = 'success';
$ret['type'] = $log['type'];
$ret['from'] = 'notify';
$ret['tid'] = $log['tid'];
$ret['uniontid'] = $log['uniontid'];
$ret['transaction_id'] = $log['transaction_id'];
$ret['user'] = $log['openid'];
$ret['fee'] = $log['fee'];
$ret['is_usecard'] = $log['is_usecard'];
$ret['card_type'] = $log['card_type'];
$ret['card_fee'] = $log['card_fee'];
$ret['card_id'] = $log['card_id'];
$site->$method($ret);
exit('success');
}
}
}
Expand Down

0 comments on commit ece967a

Please sign in to comment.