人人商城

ali.pay.class.php 2.9KB

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