Ali.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace Ycpay;
  3. class Ali implements PayInterface
  4. {
  5. private $orderParam;
  6. private $appid;
  7. private $version = "1.0";
  8. private $sign_type = "RSA2";
  9. private $charset = "UTF-8";
  10. private $format = "JSON";
  11. private $payMothod = "alipay.trade.create";
  12. private $openidMothod = "alipay.system.oauth.token";
  13. private $queryMothod = "alipay.trade.query";
  14. private $refundMothod = "alipay.trade.refund";
  15. private $templateMothod = "alipay.open.app.mini.templatemessage.send";
  16. private $notify_url;
  17. private $gateway = "https://openapi.alipay.com/gateway.do";
  18. private $secret; //AES
  19. private $privateKey; //应用私钥
  20. private $publicKey; //支付宝公钥
  21. private $notifyOrder;
  22. public static function init($config)
  23. {
  24. if (empty($config['appid'])) {
  25. throw new \Exception('not empty app_id');
  26. }
  27. if (empty($config['secret'])) {
  28. throw new \Exception('not empty secret');
  29. }
  30. $class = new self();
  31. $class->appid = $config['appid'];
  32. $class->secret = isset($config['secret']) ? $config['secret'] : "";
  33. $class->notify_url = isset($config['notify_url']) ? $config['notify_url'] : "";
  34. $class->privateKey = isset($config['privateKey']) ? $config['privateKey'] : "";
  35. $class->publicKey = isset($config['publicKey']) ? $config['publicKey'] : "";
  36. return $class;
  37. }
  38. /**
  39. * 获取下单信息
  40. */
  41. public function getParam()
  42. {
  43. return $this->orderParam;
  44. }
  45. /**
  46. * 获取异步订单信息
  47. */
  48. public function getNotifyOrder()
  49. {
  50. $this->notifyOrder = $_POST;
  51. return $this->notifyOrder;
  52. }
  53. /**
  54. * 设置订单号 金额 描述
  55. * @param string $order_no 平台订单号
  56. * @param int $money 订单金额
  57. * @param string $title 描述
  58. * @param string $desc 订单附加信息
  59. * @param string $openid 用户buyer_id
  60. */
  61. public function set($order_no, $money, $title, $desc, $openid)
  62. {
  63. $order = [
  64. 'app_id' => $this->appid,
  65. 'method' => $this->payMothod,
  66. 'format' => $this->format,
  67. 'charset' => $this->charset,
  68. 'sign_type' => $this->sign_type,
  69. 'timestamp' => date("Y-m-d H:i:s"),
  70. 'version' => $this->version,
  71. 'notify_url' => $this->notify_url,
  72. 'biz_content' => json_encode([
  73. 'out_trade_no' => $order_no,
  74. 'total_amount' => $money / 10000,
  75. 'subject' => $title,
  76. // 'body' => urlencode($desc),
  77. 'buyer_id' => $openid,
  78. ], JSON_UNESCAPED_UNICODE),
  79. ];
  80. $order['sign'] = $this->sign($order);
  81. $this->orderParam = json_decode($this->curl_post($this->gateway, $order), true);
  82. return $this;
  83. }
  84. //获取token
  85. public function getToken()
  86. {
  87. return 'Not written';
  88. }
  89. /**
  90. * 获取openid 也是user_id
  91. * @param string $code
  92. * @return array 成功返回数组 失败为空
  93. */
  94. public function getOpenid($code)
  95. {
  96. $order = [
  97. 'app_id' => $this->appid,
  98. 'method' => $this->openidMothod,
  99. 'format' => $this->format,
  100. 'charset' => $this->charset,
  101. 'sign_type' => $this->sign_type,
  102. 'timestamp' => date("Y-m-d H:i:s"),
  103. 'version' => $this->version,
  104. 'grant_type' => 'authorization_code',
  105. 'code' => $code,
  106. ];
  107. $order['sign'] = $this->sign($order);
  108. return json_decode($this->curl_post($this->gateway, $order), true);
  109. }
  110. /**
  111. * 解密手机号
  112. * @param string $encryptedData 前端传递的encryptedData
  113. */
  114. public function decryptPhone($encryptedData, $null = "", $nulls = "")
  115. {
  116. return json_decode(openssl_decrypt(base64_decode($encryptedData), 'AES-128-CBC', base64_decode($this->secret), OPENSSL_RAW_DATA), true);
  117. }
  118. /**
  119. * 订单查询
  120. * @param string $order_no [out_trade_no,trade_no]
  121. * @return array 订单信息
  122. */
  123. public function findOrder($order_no)
  124. {
  125. $order = [
  126. 'app_id' => $this->appid,
  127. 'method' => $this->queryMothod,
  128. 'format' => $this->format,
  129. 'charset' => $this->charset,
  130. 'sign_type' => $this->sign_type,
  131. 'timestamp' => date("Y-m-d H:i:s"),
  132. 'version' => $this->version,
  133. 'notify_url' => $this->notify_url,
  134. 'biz_content' => json_encode($order_no, JSON_UNESCAPED_UNICODE),
  135. ];
  136. $order['sign'] = $this->sign($order);
  137. return json_decode($this->curl_post($this->gateway, $order), true);
  138. }
  139. /**
  140. * 订单查询
  141. * @param string $order_no [out_trade_no,trade_no]
  142. * @return array 订单信息
  143. */
  144. public function applyOrderRefund($order_no)
  145. {
  146. $order = [
  147. 'app_id' => $this->appid,
  148. 'method' => $this->refundMothod,
  149. 'format' => $this->format,
  150. 'charset' => $this->charset,
  151. 'sign_type' => $this->sign_type,
  152. 'timestamp' => date("Y-m-d H:i:s"),
  153. 'version' => $this->version,
  154. 'notify_url' => $this->notify_url,
  155. 'biz_content' => json_encode($order_no, JSON_UNESCAPED_UNICODE),
  156. ];
  157. $order['sign'] = $this->sign($order);
  158. return json_decode($this->curl_post($this->gateway, $order), true);
  159. }
  160. /**
  161. * 发送模版消息
  162. * @param array $message [to_user_id,user_template_id,page,data]
  163. * @return void
  164. */
  165. public function sendMsg($message)
  166. {
  167. $order = [
  168. 'app_id' => $this->appid,
  169. 'method' => $this->templateMothod,
  170. 'format' => $this->format,
  171. 'charset' => $this->charset,
  172. 'sign_type' => $this->sign_type,
  173. 'timestamp' => date("Y-m-d H:i:s"),
  174. 'version' => $this->version,
  175. 'notify_url' => $this->notify_url,
  176. 'biz_content' => json_encode($message, JSON_UNESCAPED_UNICODE),
  177. ];
  178. $order['sign'] = $this->sign($order);
  179. return json_decode($this->curl_post($this->gateway, $order), true);
  180. }
  181. /**
  182. * @param array $map
  183. * @return string
  184. */
  185. public function sign(array $map)
  186. {
  187. $string = $this->formatBizQueryParaMap($map);
  188. $secret = "-----BEGIN RSA PRIVATE KEY-----\n" .
  189. wordwrap($this->privateKey, 64, "\n", true) .
  190. "\n-----END RSA PRIVATE KEY-----";
  191. ($secret) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  192. openssl_sign($string, $sign, $secret, OPENSSL_ALGO_SHA256);
  193. return base64_encode($sign);
  194. }
  195. /**
  196. * 异步回调
  197. * @return bool true 验签通过|false 验签不通过
  198. */
  199. public function notifyCheck()
  200. {
  201. $order = $this->getNotifyOrder();
  202. $sign = $order['sign'];
  203. unset($order['sign']);
  204. unset($order['sign_type']);
  205. $string = $this->formatBizQueryParaMap($order);
  206. $res = "-----BEGIN PUBLIC KEY-----\n" .
  207. wordwrap($this->publicKey, 64, "\n", true) .
  208. "\n-----END PUBLIC KEY-----";
  209. ($res) or die('支付宝RSA公钥错误。请检查公钥文件格式是否正确');
  210. $result = FALSE;
  211. $result = (openssl_verify($string, base64_decode($sign), $res, OPENSSL_ALGO_SHA256) === 1);
  212. if ($order['trade_status'] == "TRADE_SUCCESS" && $result) {
  213. return true;
  214. }
  215. return false;;
  216. }
  217. /**
  218. * 作用:格式化参数,签名过程需要使用
  219. */
  220. public function formatBizQueryParaMap($params)
  221. {
  222. ksort($params);
  223. $stringToBeSigned = "";
  224. $i = 0;
  225. foreach ($params as $k => $v) {
  226. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  227. // 转换成目标字符集
  228. $v = $this->characet($v, "UTF-8");
  229. if ($i == 0) {
  230. $stringToBeSigned .= "$k" . "=" . "$v";
  231. } else {
  232. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  233. }
  234. $i++;
  235. }
  236. }
  237. unset($k, $v);
  238. return $stringToBeSigned;
  239. }
  240. /**
  241. * 校验$value是否非空
  242. * if not set ,return true;
  243. * if is null , return true;
  244. **/
  245. protected function checkEmpty($value)
  246. {
  247. if (!isset($value))
  248. return true;
  249. if ($value === null)
  250. return true;
  251. if (trim($value) === "")
  252. return true;
  253. return false;
  254. }
  255. function characet($data, $targetCharset)
  256. {
  257. if (!empty($data)) {
  258. $fileType = "UTF-8";
  259. if (strcasecmp($fileType, $targetCharset) != 0) {
  260. $data = mb_convert_encoding($data, $targetCharset, $fileType);
  261. }
  262. }
  263. return $data;
  264. }
  265. public function curl_post($url, $data)
  266. {
  267. $ch = curl_init();
  268. curl_setopt($ch, CURLOPT_URL, $url);
  269. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  270. curl_setopt($ch, CURLOPT_POST, 1);
  271. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  272. $response = curl_exec($ch);
  273. $response = mb_convert_encoding($response, 'UTF-8', 'GBK');
  274. curl_close($ch);
  275. return $response;
  276. }
  277. }