From ece967af9efcc3cc445150559175e5f832d6cac8 Mon Sep 17 00:00:00 2001 From: brjun Date: Wed, 15 Feb 2023 17:25:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E5=BA=94=E7=94=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=94=AF=E4=BB=98=E5=AE=9D=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/demo_rgapi/install.php | 1 + addons/demo_rgapi/manifest.xml | 3 +- addons/demo_rgapi/site.php | 74 +++++++++--- addons/demo_rgapi/template/alipay.html | 109 ++++++++++++++++++ .../template/{pay.html => wechatpay.html} | 9 +- payment/alipay/notify.php | 101 +++++++--------- 6 files changed, 218 insertions(+), 79 deletions(-) create mode 100755 addons/demo_rgapi/template/alipay.html rename addons/demo_rgapi/template/{pay.html => wechatpay.html} (89%) mode change 100755 => 100644 payment/alipay/notify.php diff --git a/addons/demo_rgapi/install.php b/addons/demo_rgapi/install.php index c3a4ec5..d79a3ce 100644 --- a/addons/demo_rgapi/install.php +++ b/addons/demo_rgapi/install.php @@ -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 '用户标识', diff --git a/addons/demo_rgapi/manifest.xml b/addons/demo_rgapi/manifest.xml index 2b6876e..cad762e 100644 --- a/addons/demo_rgapi/manifest.xml +++ b/addons/demo_rgapi/manifest.xml @@ -35,7 +35,8 @@ - + + diff --git a/addons/demo_rgapi/site.php b/addons/demo_rgapi/site.php index 8c5a728..30471a1 100644 --- a/addons/demo_rgapi/site.php +++ b/addons/demo_rgapi/site.php @@ -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'], @@ -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'], @@ -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()); diff --git a/addons/demo_rgapi/template/alipay.html b/addons/demo_rgapi/template/alipay.html new file mode 100755 index 0000000..356a4dd --- /dev/null +++ b/addons/demo_rgapi/template/alipay.html @@ -0,0 +1,109 @@ +{template 'common/header'} +
+

支付功能需先配置支付参数:软擎授权系统->进入对应关联的号码->配置->支付参数

+
+
+ 创建支付宝支付 + 刷新 + + + + + + + + + + + + + + + + + +
订单号创建时间状态操作
{{item.no}}{{item.createtime}} + 已支付 + 已退款 + 未支付 + + +
+
+ + + +{template 'common/footer'} diff --git a/addons/demo_rgapi/template/pay.html b/addons/demo_rgapi/template/wechatpay.html similarity index 89% rename from addons/demo_rgapi/template/pay.html rename to addons/demo_rgapi/template/wechatpay.html index 8f67bfe..37d83c7 100755 --- a/addons/demo_rgapi/template/pay.html +++ b/addons/demo_rgapi/template/wechatpay.html @@ -1,6 +1,9 @@ {template 'common/header'} +
+

支付功能需先配置支付参数:软擎授权系统->进入对应关联的号码->配置->支付参数

+
- 创建支付 + 创建微信支付 刷新 @@ -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'))}", } }); diff --git a/payment/alipay/notify.php b/payment/alipay/notify.php old mode 100755 new mode 100644 index 8bc394c..dac94c8 --- a/payment/alipay/notify.php +++ b/payment/alipay/notify.php @@ -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'); } } }