ali.pay.class.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. defined('IN_IA') or exit('Access Denied');
  7. class AliPay{
  8. public $alipay;
  9. public $refundlog_id;
  10. public function __construct($module = '') {
  11. global $_W;
  12. if (!empty($module) && $module == 'store') {
  13. $setting = setting_load('store_pay');
  14. $this->setting = $setting['store_pay'];
  15. } else {
  16. $setting = uni_setting_load('payment', $_W['uniacid']);
  17. $this->setting = $setting['payment'];
  18. }
  19. }
  20. public function array2url($params) {
  21. $str = '';
  22. foreach($params as $key => $val) {
  23. if(empty($val)) {
  24. continue;
  25. }
  26. $str .= "{$key}={$val}&";
  27. }
  28. $str = trim($str, '&');
  29. return $str;
  30. }
  31. public function bulidSign($params) {
  32. unset($params['sign']);
  33. ksort($params);
  34. $string = $this->array2url($params);
  35. $prikey = authcode($this->setting['ali_refund']['private_key'], 'DECODE');
  36. $res = openssl_get_privatekey($prikey);
  37. openssl_sign($string, $sign, $res, OPENSSL_ALGO_SHA256);
  38. openssl_free_key($res);
  39. $sign = base64_encode($sign);
  40. return $sign;
  41. }
  42. public function handleReufndResult($result) {
  43. global $_W;
  44. if ($result['code'] == 10000) {
  45. WeUtility::logging ('ali_refund', var_export ($result, true));
  46. $pay_log = pdo_get ('core_paylog', array ('uniacid' => $_W['uniacid'], 'uniontid' => $result['out_trade_no']));
  47. $refund_log = pdo_get ('core_refundlog', array ('uniacid' => $_W['uniacid'], 'id' => $this->refundlog_id));
  48. if (!empty($refund_log) && $refund_log['status'] == '0' && (($result['refund_fee']) == $refund_log['fee'])) {
  49. pdo_update ('core_refundlog', array ('status' => 1), array ('id' => $refund_log['id']));
  50. $site = WeUtility::createModuleSite ($pay_log['module']);
  51. if (!is_error ($site)) {
  52. $method = 'refundResult';
  53. if (method_exists ($site, $method)) {
  54. $ret = array ();
  55. $ret['uniacid'] = $pay_log['uniacid'];
  56. $ret['result'] = 'success';
  57. $ret['type'] = $pay_log['type'];
  58. $ret['from'] = 'refund';
  59. $ret['tid'] = $pay_log['tid'];
  60. $ret['uniontid'] = $pay_log['uniontid'];
  61. $ret['refund_uniontid'] = $refund_log['refund_uniontid'];
  62. $ret['user'] = $pay_log['openid'];
  63. $ret['fee'] = $refund_log['fee'];
  64. $site->$method($ret);
  65. exit('success');
  66. }
  67. }
  68. }
  69. }
  70. }
  71. public function requestApi($url, $params) {
  72. load()->func('communication');
  73. $result = ihttp_post($url, $params);
  74. if(is_error($result)) {
  75. return $result;
  76. }
  77. $result['content'] = iconv("GBK", "UTF-8//IGNORE", $result['content']);
  78. $result = json_decode($result['content'], true);
  79. if(!is_array($result)) {
  80. return error(-1, '返回数据错误');
  81. }
  82. if($result['alipay_trade_refund_response']['code'] != 10000) {
  83. return error(-1, $result['alipay_trade_refund_response']['sub_msg']);
  84. }
  85. return $result['alipay_trade_refund_response'];
  86. }
  87. public function refund($params, $refundlog_id) {
  88. $this->refundlog_id = $refundlog_id;
  89. $params['sign'] = $this->bulidSign($params);
  90. return $this->requestApi('https://openapi.alipay.com/gateway.do', $params);
  91. }
  92. }