pay.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. define('IN_MOBILE', true);
  7. require '../../framework/bootstrap.inc.php';
  8. require '../../app/common/bootstrap.app.inc.php';
  9. load()->app('common');
  10. load()->app('template');
  11. load()->model('payment');
  12. $sl = $_GPC['ps'];
  13. $payopenid = $_GPC['payopenid'];
  14. $params = @json_decode(base64_decode($sl), true);
  15. if($_GPC['done'] == '1') {
  16. $log = pdo_get('core_paylog', array('plid' => $params['tid']));
  17. if(!empty($log) && !empty($log['status'])) {
  18. if (!empty($log['tag'])) {
  19. $tag = iunserializer($log['tag']);
  20. $log['uid'] = $tag['uid'];
  21. }
  22. $site = WeUtility::createModuleSite($log['module']);
  23. if(!is_error($site)) {
  24. $method = 'payResult';
  25. if (method_exists($site, $method)) {
  26. $ret = array();
  27. $ret['weid'] = $log['uniacid'];
  28. $ret['uniacid'] = $log['uniacid'];
  29. $ret['result'] = 'success';
  30. $ret['type'] = $log['type'];
  31. $ret['from'] = 'return';
  32. $ret['tid'] = $log['tid'];
  33. $ret['uniontid'] = $log['uniontid'];
  34. $ret['user'] = $log['openid'];
  35. $ret['fee'] = $log['fee'];
  36. $ret['tag'] = $tag;
  37. $ret['is_usecard'] = $log['is_usecard'];
  38. $ret['card_type'] = $log['card_type'];
  39. $ret['card_fee'] = $log['card_fee'];
  40. $ret['card_id'] = $log['card_id'];
  41. exit($site->$method($ret));
  42. }
  43. }
  44. }
  45. }
  46. $log = pdo_get('core_paylog', array('plid' => $params['tid']));
  47. if(!empty($log) && $log['status'] != '0') {
  48. exit('这个订单已经支付成功, 不需要重复支付.');
  49. }
  50. $auth = sha1($sl . $log['uniacid'] . $_W['config']['setting']['authkey']);
  51. if($auth != $_GPC['auth']) {
  52. exit('参数传输错误.');
  53. }
  54. $setting = uni_setting($_W['uniacid'], array('payment'));
  55. if (!empty($_GPC['code'])) {
  56. $proxy_pay_account = payment_proxy_pay_account();
  57. $oauth = $proxy_pay_account->getOauthInfo($_GPC['code']);
  58. if (!empty($oauth['openid'])) {
  59. $log['openid'] = $oauth['openid'];
  60. }
  61. }
  62. $_W['uniacid'] = $log['uniacid'];
  63. $_W['openid'] = $log['openid'];
  64. if(!is_array($setting['payment'])) {
  65. exit('没有设定支付参数.');
  66. }
  67. $wechat = $setting['payment']['wechat'];
  68. $sql = 'SELECT `key`,`secret` FROM ' . tablename('account_wechats') . ' WHERE `acid`=:acid';
  69. $row = pdo_fetch($sql, array(':acid' => $wechat['account']));
  70. $wechat['appid'] = $row['key'];
  71. $wechat['secret'] = $row['secret'];
  72. $wechat['openid'] = $payopenid;
  73. $params = array(
  74. 'tid' => $log['tid'],
  75. 'fee' => $log['card_fee'],
  76. 'user' => $log['openid'],
  77. 'title' => urldecode($params['title']),
  78. 'uniontid' => $log['uniontid'],
  79. );
  80. if (intval($wechat['switch']) == 3 || intval($wechat['switch']) == 2) {
  81. $wOpt = wechat_proxy_build($params, $wechat);
  82. } else {
  83. unset($wechat['sub_mch_id']);
  84. $wOpt = wechat_build($params, $wechat);
  85. }
  86. if (is_error($wOpt)) {
  87. if ($wOpt['message'] == 'invalid out_trade_no' || $wOpt['message'] == 'OUT_TRADE_NO_USED') {
  88. $id = date('YmdH');
  89. pdo_update('core_paylog', array('plid' => $id), array('plid' => $log['plid']));
  90. pdo_query("ALTER TABLE ".tablename('core_paylog')." auto_increment = ".($id+1).";");
  91. message("抱歉,发起支付失败,系统已经修复此问题,请重新尝试支付。");
  92. }
  93. message("抱歉,发起支付失败,具体原因为:“{$wOpt['errno']}:{$wOpt['message']}”。请及时联系站点管理员。");
  94. exit;
  95. }
  96. ?>
  97. <script type="text/javascript">
  98. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  99. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  100. 'appId' : '<?php echo $wOpt['appId'];?>',
  101. 'timeStamp': '<?php echo $wOpt['timeStamp'];?>',
  102. 'nonceStr' : '<?php echo $wOpt['nonceStr'];?>',
  103. 'package' : '<?php echo $wOpt['package'];?>',
  104. 'signType' : '<?php echo $wOpt['signType'];?>',
  105. 'paySign' : '<?php echo $wOpt['paySign'];?>'
  106. }, function(res) {
  107. if(res.err_msg == 'get_brand_wcpay_request:ok') {
  108. location.search += '&done=1';
  109. } else {
  110. // alert('启动微信支付失败, 请检查你的支付参数. 详细错误为: ' + res.err_msg);
  111. history.go(-1);
  112. }
  113. });
  114. }, false);
  115. </script>