AliPay2.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\Config;
  4. use think\Controller;
  5. class AliPay2 extends Controller
  6. {
  7. /*
  8. 配置参数
  9. */
  10. public function __construct()
  11. {
  12. $this->appId = "20210031286219882222";
  13. $this->rsaPrivateKey = 'MIIEowIBAAKCAQEAhkW8tpvdkVRZklapB+scLgcdL6N8/noWdV4h6ViUDxHd1rSjYJRHLmFgLYLgyKHHyS3tXySPf7qRbWwyImV4A17+WNTxAtdMH+TBAuqS49sAND4JYjurtY6CbQjwBEHUOE506DhISM4+fzkgtLGF8lD0jAJyXFRK4XutL3ddJ2XSzbIUTvT1bIP+cwNQcsmTTmpO145+9gKvx8tkHILKZPockarsoUoeo9gapB3hnNHNrYTbzpsa/zkOETD7L3rgpWbGJSc675t5dk8RaqH13Eon0BTn/Ov5gJ0P8j4VUWXYqdI9cEVur65S002vvhiIFVpy+30nB9SviF+/rcjQnQIDAQABAoIBAAGAdCzir2a5t8rAN2HESmtlXvkybep2uRuU8vImMHm+rGmDWbRx1B37rtSzewojbRWd8uK2iPhXwNqldCYYVnBLNrMNpqhuegZ9S39Z2ky8HVN4QiicHD6KmoDXsb+KcPI0RnB2eLB/xgksPWZn+eSM7T9Ho7nOo4vEE1rNjfgr0hexrbGJwrE1mlXfgsLRFgXfx5/3ndv4HZtX1uULdt9Xei9h8jE/kbkGz5bemjGar3ZC7rGQ+TNHDJ3BBXNCVxt9kJDjYpnf6EPceMEdClfSWwN4pvFvCx9LGcSGtFDx9NOF43alt4nt9VWrBj0H0iXRA1hyIFmO7KeuUoujN00CgYEA79L5cT38sMx3HMQ8aDXjUDvVq3yjHjKMDryt8PB3DVLqZCn7qehUQQG7NuVc4gdX6Ew97tcmx6vs8LRoD36lr8WFvJiY3RVTl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bzpsa/zkOETD7L3rgpWbGJSc675t5dk8RaqH13Eon0BTn/Ov5gJ0P8j4VUWXYqdI9cEVur65S002vvhiIFVpy+30nB9SviF+/rcjQnQIDAQAB';
  15. }
  16. /*
  17. * 支付宝支付
  18. */
  19. public function aliPay($body, $total_amount, $product_code, $notify_url)
  20. {
  21. /**
  22. * 调用支付宝接口。
  23. */
  24. $isdir = dirname(__DIR__)."/library/";//绝对路径
  25. require_once($isdir.'/alipay/aop/AopClient.php');
  26. require_once($isdir.'/alipay/aop/request/AlipayTradeAppPayRequest.php');
  27. $total_amount = floatval($total_amount);
  28. $aop = new AopClient();
  29. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  30. $aop->appId = $this->appId;
  31. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  32. $aop->format = "json";
  33. $aop->charset = "UTF-8";
  34. $aop->signType = "RSA2";
  35. $aop->alipayrsaPublicKey = $this->aliPayRsaPublicKey;
  36. $request = new AlipayTradeAppPayRequest();
  37. $arr['body'] = $body;
  38. $arr['subject'] = $body;
  39. $arr['out_trade_no'] = $product_code;
  40. $arr['timeout_express'] = '30m';
  41. $arr['total_amount'] = $total_amount;
  42. $arr['product_code'] = 'QUICK_MSECURITY_PAY';
  43. $json = json_encode($arr);
  44. $request->setNotifyUrl($notify_url);
  45. $request->setBizContent($json);
  46. $response = $aop->sdkExecute($request);
  47. return $response;
  48. }
  49. //支付宝退款
  50. public function zfbrefund($out_trade_no,$out_refund_no,$total_fee,$refund_reason){
  51. $total_fee = sprintf("%.2f",substr(sprintf("%.4f", $total_fee), 0, -2));
  52. $isdir = dirname(__DIR__)."/library/";//绝对路径
  53. require_once($isdir.'/alipay/aop/AopClient.php');
  54. require_once($isdir.'/alipay/aop/request/AlipayTradeRefundRequest.php');
  55. $aop = new AopClient();
  56. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  57. $aop->appId = $this->appId;
  58. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  59. $aop->format = "json";
  60. $aop->charset = "UTF-8";
  61. $aop->signType = "RSA2";
  62. $aop->apiVersion = '1.0';
  63. // $aop->alipayrsaPublicKey = $this->aliPayRsaPublicKey;
  64. $request = new \AlipayTradeRefundRequest();
  65. //$arr['trade_no'] = $out_refund_no;
  66. $arr['out_trade_no'] = $out_trade_no;
  67. $arr['refund_amount'] = $total_fee;
  68. $arr['refund_reason'] = $refund_reason;
  69. $arr['refund_currency'] = "CNY";
  70. $arr['operator_id'] = "OP001";
  71. $arr['store_id'] = "NJ_S_001";
  72. $arr['terminal_id'] = "NJ_T_001";
  73. $json = json_encode($arr);
  74. $request->setBizContent($json);
  75. $result = $aop->execute($request);
  76. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  77. $resultCode = $result->$responseNode->code;
  78. return $resultCode;
  79. }
  80. //支付宝转账个人账户
  81. public function FundTransToaccount($out_trade_no,$account,$account_real_name,$amount,$remark){
  82. $amount = sprintf("%.2f",substr(sprintf("%.4f", $amount), 0, -2));
  83. $isdir = dirname(__DIR__)."/library/";//绝对路径
  84. require_once($isdir.'/alipay/aop/AopClient.php');
  85. $aop = new AopClient();
  86. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  87. $aop->appId = $this->appId;
  88. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  89. $aop->format = 'json';
  90. $aop->charset = 'UTF-8';
  91. $aop->signType = 'RSA2';
  92. //$aop->alipayrsaPublicKey = $this->aliPayRsaPublicKey;
  93. require_once($isdir.'/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php');
  94. $request = new \AlipayFundTransToaccountTransferRequest();
  95. $arr['out_biz_no'] = $out_trade_no; //订单号
  96. $arr['payee_type'] = 'ALIPAY_LOGONID';
  97. $arr['payee_account'] = $account; //对方支付宝账号
  98. $arr['payee_real_name'] = $account_real_name; //支付宝实名姓名
  99. $arr['amount'] = $amount;
  100. $arr['remark'] = $remark;
  101. $json = json_encode($arr,true);
  102. $request->setBizContent($json);
  103. $result = $aop->execute($request);
  104. $result2 = json_decode(json_encode($result),true);
  105. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  106. $resultCode = $result->$responseNode->code;
  107. $arr = ['code'=>$resultCode,'result'=>$result2];
  108. return $arr;
  109. }
  110. function createLinkstring($para)
  111. {
  112. $arg = "";
  113. while (list ($key, $val) = each($para)) {
  114. $arg .= $key . "=" . $val . "&";
  115. }
  116. //去掉最后一个&字符
  117. $arg = substr($arg, 0, count($arg) - 2);
  118. //如果存在转义字符,那么去掉转义
  119. if (get_magic_quotes_gpc()) {
  120. $arg = stripslashes($arg);
  121. }
  122. return $arg;
  123. }
  124. function argSort($para)
  125. {
  126. ksort($para);
  127. reset($para);
  128. return $para;
  129. }
  130. }