Pay.Class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * 支付宝支付
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/12/03
  7. * Time: 09:51 AM
  8. */
  9. namespace Util\AliPay;
  10. use Mall\Framework\Core\ErrorCode;
  11. Use Mall\Framework\Core\ResultWrapper;
  12. class Pay
  13. {
  14. // 支付宝应用id
  15. private $appid;
  16. // 开发者私钥
  17. private $rsaPrivateKey;
  18. // 支付宝公钥
  19. private $alipayrsaPublicKey;
  20. // 支付宝私钥文件路径
  21. private $rsaPrivateKeyFilePath;
  22. /**
  23. * 微信支付异步通知地址
  24. */
  25. private $notifyUrl = URL_PROJECT.'/common/AliPayNotify/notify';
  26. /**
  27. * 公共的接口请求地址
  28. * @var string
  29. */
  30. private $gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  31. public function __construct($appid, $rsaPrivateKey, $alipayrsaPublicKey){
  32. $this->appid = $appid;
  33. $this->rsaPrivateKey = $rsaPrivateKey;
  34. $this->alipayrsaPublicKey = $alipayrsaPublicKey;
  35. }
  36. /**
  37. * 外部商户APP唤起快捷SDK创建订单并支付
  38. * 官方文档地址: https://docs.open.alipay.com/api_1/alipay.trade.app.pay
  39. */
  40. public function appPay($orderData)
  41. {
  42. // 获取业务请求参数集合
  43. $return = self::getAppPayBizContent($orderData);
  44. if(!$return->isSuccess()){
  45. return ResultWrapper::fail($return->getData(), $return->getErrorCode());
  46. }
  47. $biz_content = $return->getData();
  48. // 公共请求参数
  49. $params = [
  50. 'app_id' => $this->appid,
  51. 'method' => 'alipay.trade.app.pay',
  52. 'format' => 'json',
  53. 'charset' => 'UTF-8',
  54. 'sign_type' => 'RSA2',
  55. 'timestamp' => date("Y-m-d H:i:s"),
  56. 'version' => '1.0',
  57. 'notify_url'=> $this->notifyUrl,
  58. ];
  59. $params['biz_content'] = $biz_content;
  60. ksort($params);
  61. // 生成签名
  62. $params['sign'] = self::sign($params);
  63. foreach ($params as &$value) {
  64. $value = $this->characet($value, $params['charset']);
  65. }
  66. $return = http_build_query($params);
  67. return ResultWrapper::success($return);
  68. }
  69. /**
  70. * 业务参数
  71. * 请求字符编码必须是UTF-8
  72. */
  73. private function getAppPayBizContent($orderData)
  74. {
  75. $bizcontent = [];
  76. $bizcontent['subject'] = $orderData['subject']; // 商品的标题/交易标题/订单标题/订单关键字
  77. $bizcontent['out_trade_no'] = $orderData['out_trade_no']; // 商户网站唯一订单号
  78. $bizcontent['timeout_express'] = isset($orderData['timeout_express'])?$orderData['timeout_express']:'30m'; // 最晚付款时间
  79. $bizcontent['total_amount'] = $orderData['total_amount']; // 订单总金额,单位为元,精确到小数点后两位
  80. $bizcontent['passback_params'] = urlencode($orderData['passback_params']); // 回调数据
  81. $bizcontent['product_code'] = 'QUICK_MSECURITY_PAY';
  82. foreach ($bizcontent as $key => $value){
  83. if(empty($value)){
  84. return ResultWrapper::fail('业务参数'.$key.'为空值', ErrorCode::$paramError);
  85. }
  86. }
  87. return ResultWrapper::success(json_encode($bizcontent, JSON_UNESCAPED_UNICODE));
  88. }
  89. /**
  90. * 单笔转账接口
  91. * 官网文档地址: https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
  92. */
  93. public function transfer($orderData)
  94. {
  95. // 获取业务请求参数集合
  96. $return = self::getTransferBizContent($orderData);
  97. if(!$return->isSuccess()){
  98. return ResultWrapper::fail($return->getData(), $return->getErrorCode());
  99. }
  100. $biz_content = $return->getData();
  101. // 公共请求参数
  102. $params = [
  103. 'app_id' => $this->appid,
  104. 'method' => 'alipay.fund.trans.uni.transfer',
  105. 'format' => 'json',
  106. 'charset' => 'UTF-8',
  107. 'sign_type' => 'RSA2',
  108. 'timestamp' => date("Y-m-d H:i:s"),
  109. 'version' => '1.0',
  110. 'notify_url'=> $this->notifyUrl,
  111. ];
  112. $params['biz_content'] = $biz_content;
  113. ksort($params);
  114. // 生成签名
  115. $params['sign'] = self::sign($params);
  116. foreach ($params as &$value) {
  117. $value = $this->characet($value, $params['charset']);
  118. }
  119. $return = http_build_query($params);
  120. return ResultWrapper::success($return);
  121. }
  122. /**
  123. * 业务参数
  124. * 请求字符编码必须是UTF-8
  125. */
  126. private function getTransferBizContent($orderData)
  127. {
  128. $bizcontent = [];
  129. $bizcontent['out_biz_no'] = $orderData['out_biz_no']; // 商户网站唯一订单号
  130. $bizcontent['trans_amount'] = $orderData['trans_amount']; // 订单总金额,单位为元,精确到小数点后两位
  131. $bizcontent['product_code'] = 'TRANS_ACCOUNT_NO_PWD';
  132. $bizcontent['payee_info'] = [
  133. 'identity' => $orderData['userCenterId'], // 参与方的唯一标识
  134. 'identity_type' => 'ALIPAY_LOGON_ID', // 支付宝登陆账号
  135. 'name' => $orderData['customerName'], // 支付宝对应的正式名称
  136. ];
  137. foreach ($bizcontent as $key => $value){
  138. if(empty($value)){
  139. return ResultWrapper::fail('业务参数'.$key.'为空值', ErrorCode::$paramError);
  140. }
  141. }
  142. return ResultWrapper::success(json_encode($bizcontent, JSON_UNESCAPED_UNICODE));
  143. }
  144. /**
  145. * 生成签名
  146. */
  147. private function sign($params)
  148. {
  149. if($this->checkEmpty($this->rsaPrivateKeyFilePath)){
  150. $priKey=$this->rsaPrivateKey;
  151. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  152. wordwrap($priKey, 64, "\n", true) .
  153. "\n-----END RSA PRIVATE KEY-----";
  154. }else {
  155. $priKey = file_get_contents($this->rsaPrivateKeyFilePath);
  156. $res = openssl_get_privatekey($priKey);
  157. }
  158. ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  159. $data = self::getSignContent($params);
  160. if ("RSA2" == $params['sign_type']) {
  161. openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
  162. } else {
  163. openssl_sign($data, $sign, $res);
  164. }
  165. if(!empty($this->rsaPrivateKeyFilePath)){
  166. openssl_free_key($res);
  167. }
  168. $sign = base64_encode($sign);
  169. return $sign;
  170. }
  171. /**
  172. * 获取要签名的参数内容
  173. * @param $params
  174. * @return string
  175. */
  176. public function getSignContent($params) {
  177. ksort($params);
  178. $stringToBeSigned = "";
  179. $i = 0;
  180. foreach ($params as $k => $v) {
  181. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  182. // 转换成目标字符集
  183. $v = $this->characet($v, "UTF-8");
  184. if ($i == 0) {
  185. $stringToBeSigned .= "$k" . "=" . "$v";
  186. } else {
  187. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  188. }
  189. $i++;
  190. }
  191. }
  192. unset ($k, $v);
  193. return $stringToBeSigned;
  194. }
  195. /**
  196. * 转换字符集编码
  197. * @param $data
  198. * @param $targetCharset
  199. * @return string
  200. */
  201. function characet($data, $targetCharset)
  202. {
  203. if (!empty($data)) {
  204. $fileType = "UTF-8";
  205. if (strcasecmp($fileType, $targetCharset) != 0) {
  206. $data = mb_convert_encoding($data, $targetCharset, $fileType);
  207. // $data = iconv($fileType, $targetCharset.'//IGNORE', $data);
  208. }
  209. }
  210. return $data;
  211. }
  212. /**
  213. * 校验$value是否非空
  214. * if not set ,return true;
  215. * if is null , return true;
  216. **/
  217. protected function checkEmpty($value)
  218. {
  219. if (!isset($value))
  220. return true;
  221. if ($value === null)
  222. return true;
  223. if (trim($value) === "")
  224. return true;
  225. return false;
  226. }
  227. }