notify.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $input = file_get_contents('php://input');
  9. $isxml = true;
  10. if (!empty($input) && empty($_GET['out_trade_no'])) {
  11. $obj = isimplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
  12. $data = json_decode(json_encode($obj), true);
  13. if (empty($data)) {
  14. $result = array(
  15. 'return_code' => 'FAIL',
  16. 'return_msg' => ''
  17. );
  18. echo array2xml($result);
  19. exit;
  20. }
  21. if ($data['result_code'] != 'SUCCESS' || $data['return_code'] != 'SUCCESS') {
  22. $result = array(
  23. 'return_code' => 'FAIL',
  24. 'return_msg' => empty($data['return_msg']) ? $data['err_code_des'] : $data['return_msg']
  25. );
  26. echo array2xml($result);
  27. exit;
  28. }
  29. $get = $data;
  30. } else {
  31. $isxml = false;
  32. $get = $_GET;
  33. }
  34. load()->web('common');
  35. load()->classs('coupon');
  36. $_W['uniacid'] = $_W['weid'] = intval($get['attach']);
  37. $_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);
  38. $_W['acid'] = $_W['uniaccount']['acid'];
  39. $setting = uni_setting($_W['uniacid'], array('payment'));
  40. if ($get['trade_type'] == 'NATIVE') {
  41. $setting = setting_load('store_pay');
  42. $setting['payment']['wechat'] = $setting['store_pay']['wechat'];
  43. }
  44. if(is_array($setting['payment'])) {
  45. $wechat = $setting['payment']['wechat'];
  46. WeUtility::logging('pay', var_export($get, true));
  47. if(!empty($wechat)) {
  48. ksort($get);
  49. $string1 = '';
  50. foreach($get as $k => $v) {
  51. if($v != '' && $k != 'sign') {
  52. $string1 .= "{$k}={$v}&";
  53. }
  54. }
  55. if (intval($wechat['switch']) == 3) {
  56. $facilitator_setting = uni_setting($wechat['service'], array('payment'));
  57. $wechat['signkey'] = $facilitator_setting['payment']['wechat_facilitator']['signkey'];
  58. } else {
  59. $wechat['signkey'] = ($wechat['version'] == 1) ? $wechat['key'] : (!empty($wechat['apikey']) ? $wechat['apikey'] : $wechat['signkey']); }
  60. $sign = strtoupper(md5($string1 . "key={$wechat['signkey']}"));
  61. if($sign == $get['sign']) {
  62. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `uniontid`=:uniontid';
  63. $params = array();
  64. $params[':uniontid'] = $get['out_trade_no'];
  65. $log = pdo_fetch($sql, $params);
  66. if (intval($wechat['switch']) == PAYMENT_WECHAT_TYPE_SERVICE) {
  67. $get['openid'] = $log['openid'];
  68. }
  69. if(!empty($log) && $log['status'] == '0' && (($get['total_fee'] / 100) == $log['card_fee'])) {
  70. $log['tag'] = iunserializer($log['tag']);
  71. $log['tag']['transaction_id'] = $get['transaction_id'];
  72. $log['uid'] = $log['tag']['uid'];
  73. $record = array();
  74. $record['status'] = '1';
  75. $record['tag'] = iserializer($log['tag']);
  76. pdo_update('core_paylog', $record, array('plid' => $log['plid']));
  77. $mix_pay_credit_log = pdo_get('core_paylog', array('module' => $log['module'], 'tid' => $log['tid'], 'uniacid' => $log['uniacid'], 'type' => 'credit'));
  78. if (!empty($mix_pay_credit_log)) {
  79. pdo_update('core_paylog', array('status' => 1), array('plid' => $mix_pay_credit_log['plid']));
  80. $log['fee'] = $mix_pay_credit_log['fee'] + $log['fee'];
  81. $log['card_fee'] = $mix_pay_credit_log['fee'] + $log['card_fee'];
  82. $setting = uni_setting($_W['uniacid'], array('creditbehaviors'));
  83. $credtis = mc_credit_fetch($log['uid']);
  84. mc_credit_update($log['uid'], $setting['creditbehaviors']['currency'], -$mix_pay_credit_log['fee'], array($log['uid'], '消费' . $setting['creditbehaviors']['currency'] . ':' . $fee));
  85. }
  86. if ($log['is_usecard'] == 1 && !empty($log['encrypt_code'])) {
  87. $coupon_info = pdo_get('coupon', array('id' => $log['card_id']), array('id'));
  88. $coupon_record = pdo_get('coupon_record', array('code' => $log['encrypt_code'], 'status' => '1'));
  89. load()->model('activity');
  90. $status = activity_coupon_use($coupon_info['id'], $coupon_record['id'], $log['module']);
  91. }
  92. if ($log['type'] == 'wxapp') {
  93. $site = WeUtility::createModuleWxapp($log['module']);
  94. } else {
  95. $site = WeUtility::createModuleSite($log['module']);
  96. }
  97. if(!is_error($site)) {
  98. $method = 'payResult';
  99. if (method_exists($site, $method)) {
  100. $ret = array();
  101. $ret['weid'] = $log['weid'];
  102. $ret['uniacid'] = $log['uniacid'];
  103. $ret['acid'] = $log['acid'];
  104. $ret['result'] = 'success';
  105. $ret['type'] = $log['type'];
  106. $ret['from'] = 'notify';
  107. $ret['tid'] = $log['tid'];
  108. $ret['uniontid'] = $log['uniontid'];
  109. $ret['transaction_id'] = $log['transaction_id'];
  110. $ret['trade_type'] = $get['trade_type'];
  111. $ret['follow'] = $get['is_subscribe'] == 'Y' ? 1 : 0;
  112. $ret['user'] = empty($get['openid']) ? $log['openid'] : $get['openid'];
  113. $ret['fee'] = $log['fee'];
  114. $ret['tag'] = $log['tag'];
  115. $ret['is_usecard'] = $log['is_usecard'];
  116. $ret['card_type'] = $log['card_type'];
  117. $ret['card_fee'] = $log['card_fee'];
  118. $ret['card_id'] = $log['card_id'];
  119. if(!empty($get['time_end'])) {
  120. $ret['paytime'] = strtotime($get['time_end']);
  121. }
  122. $site->$method($ret);
  123. if($isxml) {
  124. $result = array(
  125. 'return_code' => 'SUCCESS',
  126. 'return_msg' => 'OK'
  127. );
  128. echo array2xml($result);
  129. exit;
  130. } else {
  131. exit('success');
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. if($isxml) {
  140. $result = array(
  141. 'return_code' => 'FAIL',
  142. 'return_msg' => ''
  143. );
  144. echo array2xml($result);
  145. exit;
  146. } else {
  147. exit('fail');
  148. }