You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

307 lines
9.9 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权�?�? 运游�? * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改�?
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布�?
  10. * ============================================================================
  11. * Author By: 倪宗�?
  12. * PhpStorm AliPayService.php
  13. * Create By 2016/12/12 14:32 $
  14. */
  15. namespace addons\nzf;
  16. use addons\unishop\model\Config;
  17. class AliPay
  18. {
  19. private static $gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
  20. /**
  21. * Des:扫描支付
  22. * Name: unifiedOrderByOrderIdForSao
  23. * @param $par
  24. * @return array
  25. * @author 倪宗锋
  26. */
  27. public function unifiedOrderByOrderIdForSao($par)
  28. {
  29. $aliPay = new AliPay();
  30. //交易参数
  31. $params['body'] = $par['name'];
  32. $params['subject'] = $par['name'];
  33. $params['order_id'] = $par['order_id'];
  34. $params['price'] = $par['total_fee'];
  35. $params['timeout_express'] = '1m';
  36. $params['goType'] = 3;
  37. $params['method'] = 'alipay.trade.precreate';
  38. $getUrl = $aliPay->getAliPayUrl($params);
  39. $url = '/fx/?r=weChat/we-chat/q-code&qCode=' . urlencode($getUrl['data']['codUrl']) . '&_math=' . rand(100, 999);
  40. return Util::returnArrSu('', array('codUrl' => $url, 'price' => $par['total_fee']));
  41. }
  42. /**
  43. * Function Description:支付宝web支付
  44. * Function Name: webPay
  45. * @param $param array
  46. * @return array
  47. *
  48. * @author nzf
  49. */
  50. public function webPay($param)
  51. {
  52. //交易参数
  53. $params['body'] = $param['name'];
  54. $params['subject'] = $param['name'];
  55. $params['order_id'] = $param['order_id'];
  56. $params['price'] = $param['total_fee'];
  57. $params['timeout_express'] = '1m';
  58. $params['goType'] = 2;
  59. $params['method'] = 'alipay.trade.wap.pay';
  60. $return = static::getAliPayUrl($params);
  61. return $return;
  62. }
  63. /********************通用方法**************************/
  64. /**
  65. * Function Description:获取阿里支付pay
  66. * Function Name: getAliPayUrl
  67. * @param $params array
  68. *
  69. * @return array
  70. *
  71. * @author 倪宗�?
  72. */
  73. private static function getAliPayUrl($params)
  74. {
  75. $sysParams = self::getSysParams();
  76. $sysParams["method"] = $params['method'];
  77. //交易参数
  78. $body['productCode'] = 'QUICK_WAP_PAY';
  79. $body['body'] = $params['body'];
  80. $body['subject'] = $params['subject'];
  81. $body['out_trade_no'] = $params['order_id'];
  82. $body['total_amount'] = $params['price'];
  83. $body['timeout_express'] = "1m";
  84. $sysParams['biz_content'] = json_encode($body);
  85. if ($params['goType'] == 1) {//如果是页面跳�?
  86. $sysParams['sign'] = urlencode(static::getSign($sysParams));
  87. $paramString = static::getSignContent($sysParams);
  88. $requestUrl = static::$gatewayUrl . "?" . $paramString;
  89. return Util::returnArrSu('', ['payData' => $requestUrl]);
  90. } elseif ($params['goType'] == 2) {//如果是参数返�?
  91. $sysParams['sign'] = static::getSign($sysParams);
  92. $result = self::buildRequestForm($sysParams);
  93. return Util::returnArrSu('', ['payData' => $result]);
  94. } else {
  95. $sysParams['sign'] = urlencode(static::getSign($sysParams));
  96. $paramString = static::getSignContent($sysParams);
  97. $requestUrl = static::$gatewayUrl . "?" . $paramString;
  98. $curl = new CurlInterface('', 5);
  99. $result = $curl->execute($requestUrl, 'GET');
  100. $result = json_decode($result, true);
  101. $return['codUrl'] = '';
  102. if ($result['alipay_trade_precreate_response']['code'] == '10000') {
  103. $return['codUrl'] = $result['alipay_trade_precreate_response']['qr_code'];
  104. }
  105. return Util::returnArrSu('', $return);
  106. }
  107. }
  108. /**
  109. * Function Description:取消订单
  110. * Function Name: cancelOrder
  111. * @param $params
  112. *
  113. * @return array
  114. *
  115. * @author 倪宗�?
  116. */
  117. public static function cancelOrder($params)
  118. {
  119. $sysParams = self::getSysParams();
  120. $sysParams["method"] = 'alipay.trade.refund';
  121. $body = array(
  122. 'out_trade_no' => $params['order_id'],//订单�?
  123. 'refund_amount' => $params['refund_fee'],//退款金额
  124. 'refund_reason' => $params['memo'],
  125. 'out_request_no' => $params['order_id'] . '-' . date('YmdHis') . rand(100, 999)//�?款原�?
  126. );
  127. unset($sysParams["notify_url"]);
  128. unset($sysParams["return_url"]);
  129. $sysParams['biz_content'] = json_encode($body);
  130. $sysParams['sign'] = static::getSign($sysParams);
  131. $sysParams['sign'] = urlencode(static::getSign($sysParams));
  132. $paramString = static::getSignContent($sysParams);
  133. $requestUrl = static::$gatewayUrl."?".$paramString;
  134. //调用款接
  135. $curlInterface = new CurlInterface($sysParams, 5);
  136. $result = $curlInterface->execute($requestUrl, 'GET');
  137. $result = json_decode(mb_convert_encoding($result, 'utf-8'), true);
  138. if (isset($result['alipay_trade_refund_response']) && $result['alipay_trade_refund_response']['code'] == '10000') {
  139. return Util::returnArrSu();
  140. }
  141. return Util::returnArrEr('退款失败!');
  142. }
  143. /**
  144. * Function Description:获取系统通用参数
  145. * Function Name: getSysParams
  146. * @param $app_id string
  147. * @param $order_id string
  148. * @return mixed
  149. *
  150. * @author 倪宗�?
  151. */
  152. private static function getSysParams()
  153. {
  154. //组装系统参数
  155. $sysParams["app_id"] = Config::getByName('ali_app_id')['value'];
  156. $sysParams["version"] = '1.0';
  157. $sysParams["format"] = 'json';
  158. $sysParams["sign_type"] = 'RSA2';
  159. $sysParams["timestamp"] = date("Y-m-d H:i:s");
  160. $sysParams["alipay_sdk"] = 'alipay-sdk-php-20160411';
  161. $sysParams["prod_code"] = '';
  162. $sysParams["notify_url"] = Config::getByName('ali_notify_url')['value'];
  163. $sysParams["return_url"] = Config::getByName('ali_return_url')['value'];
  164. $sysParams["charset"] = "utf-8";
  165. return $sysParams;
  166. }
  167. /**
  168. * Function Description:建立请求,以表单HTML形式构�?�(默认�?
  169. * Function Name: buildRequestForm
  170. * @param $para_temp
  171. *
  172. * @return string 提交表单HTML文本
  173. *
  174. * @author 倪宗�?
  175. */
  176. protected static function buildRequestForm($para_temp)
  177. {
  178. $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='" . static::$gatewayUrl . "?charset=utf-8' method='get'>";
  179. while (list ($key, $val) = each($para_temp)) {
  180. if (false === static::checkEmpty($val)) {
  181. $val = str_replace("'", "&apos;", $val);
  182. $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
  183. }
  184. }
  185. return $sHtml;
  186. }
  187. /**
  188. * Function Description:获取签名
  189. * Function Name: getSign
  190. * @param $params
  191. * @param $app_id
  192. * @return string
  193. *
  194. * @author 倪宗�?
  195. */
  196. public static function getSign($params)
  197. {
  198. if (isset($params['sign'])) {
  199. unset($params['sign']);
  200. }
  201. $content = self::getSignContent($params);
  202. $priKey = Config::getByName('ali_private_key')['value'];
  203. // $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  204. // wordwrap($priKey, 64, "\n", true) .
  205. // "\n-----END RSA PRIVATE KEY-----";
  206. // openssl_sign($content, $sign, $res);
  207. // $sign = base64_encode($sign);
  208. // return $sign;
  209. $res = "-----BEGIN RSA PRIVATE KEY-----\n".
  210. wordwrap($priKey, 64, "\n", true).
  211. "\n-----END RSA PRIVATE KEY-----";
  212. openssl_sign($content, $sign, $res, OPENSSL_ALGO_SHA256);
  213. return base64_encode($sign);
  214. }
  215. /**
  216. * Function Description:将数组转换为符合阿里支付的路由参�?
  217. * Function Name: getSignContent
  218. * @param $params
  219. *
  220. * @return string
  221. *
  222. * @author 倪宗�?
  223. */
  224. public static function getSignContent($params)
  225. {
  226. ksort($params);
  227. $stringToBeSigned = "";
  228. $i = 0;
  229. foreach ($params as $k => $v) {
  230. if (false === self::checkEmpty($v) && "@" != substr($v, 0, 1)) {
  231. // 转换成目标字符集
  232. $v = self::characet($v, 'utf-8');
  233. if ($i == 0) {
  234. $stringToBeSigned.="$k"."="."$v";
  235. } else {
  236. $stringToBeSigned.="&"."$k"."=".$v;
  237. }
  238. $i++;
  239. }
  240. }
  241. unset ($k, $v);
  242. return $stringToBeSigned;
  243. }
  244. /**
  245. * Function Description:校验$value是否非空
  246. * Function Name: checkEmpty
  247. * @param $value
  248. *
  249. * @return bool
  250. *
  251. * @author 倪宗�?
  252. */
  253. private static function checkEmpty($value)
  254. {
  255. if (!isset($value)) {
  256. return true;
  257. }
  258. if ($value === null) {
  259. return true;
  260. }
  261. if (trim($value) === "") {
  262. return true;
  263. }
  264. return false;
  265. }
  266. /**
  267. * Function Description:转换字符集编�?
  268. * Function Name: characet
  269. * @param $data
  270. * @param $targetCharset
  271. *
  272. * @return string
  273. *
  274. * @author 倪宗�?
  275. */
  276. private static function characet($data, $targetCharset)
  277. {
  278. if (!empty($data)) {
  279. $fileType = 'utf-8';
  280. if (strcasecmp($fileType, $targetCharset) != 0) {
  281. $data = mb_convert_encoding($data, $targetCharset);
  282. }
  283. }
  284. return $data;
  285. }
  286. }