refund.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. require '../../framework/bootstrap.inc.php';
  7. load()->web('common');
  8. $input = file_get_contents('php://input');
  9. if (!empty($input)) {
  10. $obj = isimplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
  11. $wechat_data = json_decode(json_encode($obj), true);
  12. if (empty($wechat_data)) {
  13. $result = array(
  14. 'return_code' => 'FAIL',
  15. 'return_msg' => ''
  16. );
  17. echo array2xml($result);
  18. exit;
  19. }
  20. if ($wechat_data['return_code'] != 'SUCCESS') {
  21. $result = array(
  22. 'return_code' => 'FAIL',
  23. 'return_msg' => $wechat_data['return_msg']
  24. );
  25. exit;
  26. }
  27. } else {
  28. $result = array(
  29. 'return_code' => 'FAIL',
  30. 'return_msg' => ''
  31. );
  32. echo array2xml($result);
  33. exit;
  34. }
  35. $account = pdo_get('account_wechats', array('key' => $wechat_data['appid']));
  36. if (empty($account)) {
  37. $account = pdo_get('account_wxapp', array('key' => $wechat_data['appid']));
  38. }
  39. $_W['uniacid'] = $account['uniacid'];
  40. if (!empty($wechat_data['sub_mch_id'])) {
  41. $account_list = pdo_getall('account', array(), array('uniacid'));
  42. if (is_array($account_list)) {
  43. foreach ($account_list as $sub_account) {
  44. $setting = uni_setting_load('payment', $sub_account['uniacid']);
  45. if ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_SERVICE && $wechat_data['sub_mch_id'] == $setting['payment']['wechat']['sub_mch_id']) {
  46. $_W['uniacid'] = $sub_account['uniacid'];
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. $setting = uni_setting_load('payment', $_W['uniacid']);
  53. $pay_setting = $setting['payment']['wechat'];
  54. $pay_setting['signkey'] = $pay_setting['version'] == 1 ? $pay_setting['key'] : $pay_setting['signkey'];
  55. if ($pay_setting['switch'] == PAYMENT_WECHAT_TYPE_SERVICE) {
  56. $proxy_setting = uni_setting_load('payment', $pay_setting['service']);
  57. $pay_setting['signkey'] = $proxy_setting['payment']['wechat_facilitator']['signkey'];
  58. }
  59. if(!empty($pay_setting['signkey'])) {
  60. WeUtility::logging('refund', var_export($wechat_data, true));
  61. $key = md5($pay_setting['signkey']);
  62. $wechat_data['req_info'] = aes_pkcs7_decode($wechat_data['req_info'], $key);
  63. $refund = json_decode(json_encode(isimplexml_load_string($wechat_data['req_info'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  64. if(!empty($refund)) {
  65. $pay_log = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refund['out_trade_no']));
  66. $refund_log = pdo_get('core_refundlog', array('uniacid' => $_W['uniacid'], 'refund_uniontid' => $refund['out_refund_no'], 'uniontid' => $refund['out_trade_no']));
  67. if(!empty($refund_log) && $refund_log['status'] == '0' && (($refund['total_fee'] / 100) == $pay_log['card_fee'])) {
  68. pdo_update('core_refundlog', array('status' => 1), array('id' => $refund_log['id']));
  69. $site = WeUtility::createModuleSite($pay_log['module']);
  70. if(!is_error($site)) {
  71. $method = 'refundResult';
  72. if (method_exists($site, $method)) {
  73. $ret = array();
  74. $ret['uniacid'] = $pay_log['uniacid'];
  75. $ret['result'] = 'success';
  76. $ret['type'] = $pay_log['type'];
  77. $ret['from'] = 'refund';
  78. $ret['tid'] = $pay_log['tid'];
  79. $ret['uniontid'] = $pay_log['uniontid'];
  80. $ret['refund_uniontid'] = $refund_log['refund_uniontid'];
  81. $ret['user'] = $pay_log['openid'];
  82. $ret['fee'] = $refund['fee'];
  83. if(!empty($refund['success_time'])) {
  84. $ret['refund_time'] = strtotime($refund['time_end']);
  85. }
  86. $site->$method($ret);
  87. exit('success');
  88. }
  89. }
  90. }
  91. }
  92. }