notify.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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'] : $wechat['signkey'];
  60. }
  61. $sign = strtoupper(md5($string1 . "key={$wechat['signkey']}"));
  62. if($sign == $get['sign']) {
  63. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `uniontid`=:uniontid';
  64. $params = array();
  65. $params[':uniontid'] = $get['out_trade_no'];
  66. $log = pdo_fetch($sql, $params);
  67. if (intval($wechat['switch']) == PAYMENT_WECHAT_TYPE_SERVICE) {
  68. $get['openid'] = $log['openid'];
  69. }
  70. if(!empty($log) && $log['status'] == '0' && (($get['total_fee'] / 100) == $log['card_fee'])) {
  71. $log['tag'] = iunserializer($log['tag']);
  72. $log['tag']['transaction_id'] = $get['transaction_id'];
  73. $log['uid'] = $log['tag']['uid'];
  74. $record = array();
  75. $record['status'] = '1';
  76. $record['tag'] = iserializer($log['tag']);
  77. pdo_update('core_paylog', $record, array('plid' => $log['plid']));
  78. $mix_pay_credit_log = pdo_get('core_paylog', array('module' => $log['module'], 'tid' => $log['tid'], 'uniacid' => $log['uniacid'], 'type' => 'credit'));
  79. if (!empty($mix_pay_credit_log)) {
  80. pdo_update('core_paylog', array('status' => 1), array('plid' => $mix_pay_credit_log['plid']));
  81. $log['fee'] = $mix_pay_credit_log['fee'] + $log['fee'];
  82. $log['card_fee'] = $mix_pay_credit_log['fee'] + $log['card_fee'];
  83. $setting = uni_setting($_W['uniacid'], array('creditbehaviors'));
  84. $credtis = mc_credit_fetch($log['uid']);
  85. mc_credit_update($log['uid'], $setting['creditbehaviors']['currency'], -$mix_pay_credit_log['fee'], array($log['uid'], '消费' . $setting['creditbehaviors']['currency'] . ':' . $fee));
  86. }
  87. if ($log['is_usecard'] == 1 && !empty($log['encrypt_code'])) {
  88. $coupon_info = pdo_get('coupon', array('id' => $log['card_id']), array('id'));
  89. $coupon_record = pdo_get('coupon_record', array('code' => $log['encrypt_code'], 'status' => '1'));
  90. load()->model('activity');
  91. $status = activity_coupon_use($coupon_info['id'], $coupon_record['id'], $log['module']);
  92. }
  93. if ($log['type'] == 'wxapp') {
  94. $site = WeUtility::createModuleWxapp($log['module']);
  95. } else {
  96. $site = WeUtility::createModuleSite($log['module']);
  97. }
  98. if(!is_error($site)) {
  99. $method = 'payResult';
  100. if (method_exists($site, $method)) {
  101. $ret = array();
  102. $ret['weid'] = $log['weid'];
  103. $ret['uniacid'] = $log['uniacid'];
  104. $ret['acid'] = $log['acid'];
  105. $ret['result'] = 'success';
  106. $ret['type'] = $log['type'];
  107. $ret['from'] = 'notify';
  108. $ret['tid'] = $log['tid'];
  109. $ret['uniontid'] = $log['uniontid'];
  110. $ret['transaction_id'] = $log['transaction_id'];
  111. $ret['trade_type'] = $get['trade_type'];
  112. $ret['follow'] = $get['is_subscribe'] == 'Y' ? 1 : 0;
  113. $ret['user'] = empty($get['openid']) ? $log['openid'] : $get['openid'];
  114. $ret['fee'] = $log['fee'];
  115. $ret['tag'] = $log['tag'];
  116. $ret['is_usecard'] = $log['is_usecard'];
  117. $ret['card_type'] = $log['card_type'];
  118. $ret['card_fee'] = $log['card_fee'];
  119. $ret['card_id'] = $log['card_id'];
  120. if(!empty($get['time_end'])) {
  121. $ret['paytime'] = strtotime($get['time_end']);
  122. }
  123. $site->$method($ret);
  124. if($isxml) {
  125. $result = array(
  126. 'return_code' => 'SUCCESS',
  127. 'return_msg' => 'OK'
  128. );
  129. echo array2xml($result);
  130. exit;
  131. } else {
  132. exit('success');
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. if($isxml) {
  141. $result = array(
  142. 'return_code' => 'FAIL',
  143. 'return_msg' => ''
  144. );
  145. echo array2xml($result);
  146. exit;
  147. } else {
  148. exit('fail');
  149. }