Pay.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\alipay\model;
  13. use addon\alipay\data\sdk\AopClient;
  14. use addon\alipay\data\sdk\request\AlipayTradeWapPayRequest;
  15. use addon\alipay\data\sdk\request\AlipayTradePagePayRequest;
  16. use addon\alipay\data\sdk\request\AlipayTradeAppPayRequest;
  17. use addon\alipay\data\sdk\request\AlipayTradeCloseRequest;
  18. use addon\alipay\data\sdk\request\AlipayTradeRefundRequest;
  19. use app\model\system\Pay as PayCommon;
  20. use app\model\BaseModel;
  21. use think\facade\Log;
  22. use addon\alipay\data\sdk\request\AlipayFundTransToaccountTransferRequest;
  23. /**
  24. * 支付宝支付配置
  25. */
  26. class Pay extends BaseModel
  27. {
  28. public $aop;
  29. function __construct()
  30. {
  31. // 获取支付宝支付参数(统一支付到平台账户)
  32. $config_model = new Config();
  33. $config_result = $config_model->getPayConfig();
  34. $config = $config_result["data"];
  35. if(!empty($config)){
  36. $config_info = $config["value"];
  37. }
  38. // 获取支付宝支付参数(统一支付到平台账户)
  39. $this->aop = new AopClient();
  40. $this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  41. $this->aop->appId = $config_info["app_id"] ?? "";
  42. $this->aop->rsaPrivateKey = $config_info['private_key'] ?? "";
  43. $this->aop->alipayrsaPublicKey =$config_info['public_key'] ?? "";
  44. $this->aop->alipayPublicKey = $config_info['alipay_public_key'] ?? "";
  45. $this->aop->apiVersion = '1.0';
  46. $this->aop->signType = 'RSA2';
  47. $this->aop->postCharset = 'UTF-8';
  48. $this->aop->format = 'json';
  49. }
  50. /**
  51. * 生成支付
  52. * @param $param
  53. */
  54. public function pay($param){
  55. //构造要请求的参数数组,无需改动
  56. $parameter = array(
  57. "out_trade_no" => $param["out_trade_no"],
  58. "subject" => str_sub($param["pay_body"], 15),
  59. "total_amount" => (float)$param["pay_money"],
  60. "body" => str_sub($param["pay_body"], 60),
  61. "product_code" => 'FAST_INSTANT_TRADE_PAY',
  62. );
  63. $parameter = json_encode($parameter);
  64. switch ($param["app_type"])
  65. {
  66. case "h5":
  67. $request = new AlipayTradeWapPayRequest();
  68. break;
  69. case "pc":
  70. $request = new AlipayTradePagePayRequest();
  71. break;
  72. case "app":
  73. $request = new AlipayTradeAppPayRequest();
  74. break;
  75. }
  76. $request->setBizContent($parameter);
  77. $request->SetReturnUrl($param["return_url"]);
  78. $request->SetNotifyUrl($param["notify_url"]);
  79. $result = $this->aop->pageExecute($request, 'get');
  80. return $this->success([
  81. 'type' => 'url',
  82. 'data' => $result
  83. ]);
  84. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  85. $resultCode = $result->$responseNode->code;
  86. if (!empty($resultCode) && $resultCode == 10000) {
  87. return $this->success();
  88. } else {
  89. return $this->error("", $resultCode);
  90. }
  91. }
  92. /**
  93. * 支付关闭
  94. * @param unknown $orderNumber
  95. * @return multitype:number string |multitype:number mixed
  96. */
  97. public function close($param)
  98. {
  99. $parameter = array(
  100. "out_trade_no" => $param["out_trade_no"]
  101. );
  102. // 建立请求
  103. $request = new AlipayTradeCloseRequest();
  104. $request->setBizContent(json_encode($parameter));
  105. $result = $this->aop->execute($request);
  106. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  107. $resultCode = $result->$responseNode->code;
  108. if (!empty($resultCode) && $resultCode == 10000) {
  109. return $this->success();
  110. } else {
  111. return $this->error("", $resultCode->sub_msg);
  112. }
  113. }
  114. /**
  115. * 支付宝支付原路返回
  116. * @param unknown $param 支付参数
  117. */
  118. public function refund($param)
  119. {
  120. $pay_info = $param["pay_info"];
  121. $refund_no = $param["refund_no"];
  122. $out_trade_no = $pay_info["trade_no"] ?? '';
  123. $refund_fee = $param["refund_fee"];
  124. $parameter = array(
  125. 'trade_no' => $out_trade_no,
  126. 'refund_amount' => sprintf("%.2f", $refund_fee),
  127. 'out_request_no' => $refund_no
  128. );
  129. // 建立请求
  130. $request = new AlipayTradeRefundRequest ();
  131. $request->setBizContent(json_encode($parameter));
  132. $result = $this->aop->execute($request);
  133. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  134. $resultCode = $result->$responseNode->code;
  135. if (!empty($resultCode) && $resultCode == 10000) {
  136. return $this->success();
  137. } else {
  138. return $this->error("", $resultCode->sub_msg);
  139. }
  140. }
  141. /**
  142. * 支付宝转账
  143. * @param $param
  144. * @return array
  145. * @throws \think\Exception
  146. */
  147. public function payTransfer($param)
  148. {
  149. try {
  150. $config_model = new Config();
  151. $config_result = $config_model->getPayConfig();
  152. if ($config_result['code'] < 0) return $config_result;
  153. $config = $config_result['data']['value'];
  154. if (empty($config)) return $this->error([], '平台未配置支付宝支付');
  155. if (!$config['transfer_status']) return $this->error([], '平台未启用支付宝转账');
  156. $parameter = [
  157. 'out_biz_no' => $param['out_trade_no'],
  158. 'payee_type' => 'ALIPAY_LOGONID',
  159. 'payee_account' => $param["account_number"],
  160. 'amount' => sprintf("%.2f", $param['amount']),
  161. 'payee_real_name' => $param["real_name"],
  162. 'remark' => $param["desc"]
  163. ];
  164. // 建立请求
  165. $request = new AlipayFundTransToaccountTransferRequest();
  166. $request->setBizContent(json_encode($parameter));
  167. $result = $this->aop->execute($request);
  168. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  169. $resultCode = $result->$responseNode->code;
  170. if(!empty($resultCode) && $resultCode == 10000){
  171. return $this->success([
  172. 'out_trade_no' => $result->$responseNode->out_biz_no, // 商户交易号
  173. 'payment_no' => $result->$responseNode->order_id, // 微信付款单号
  174. 'payment_time' => date_to_time($result->$responseNode->pay_date) // 付款成功时间
  175. ]);
  176. } else {
  177. return $this->error([], $result->$responseNode->sub_msg);
  178. }
  179. } catch (\Exception $e) {
  180. return $this->error([], $e->getMessage());
  181. }
  182. }
  183. /**
  184. * 异步完成支付
  185. * @param $param
  186. */
  187. public function payNotify(){
  188. // Log::write('pay_notifiy_log:alipay:'.json_encode(input()), 'notice');
  189. try{
  190. $res = $this->aop->rsaCheckV1($_POST, $this->aop->alipayrsaPublicKey, $this->aop->signType);
  191. if ($res) { // 验证成功
  192. $out_trade_no = $_POST['out_trade_no'];
  193. // 支付宝交易号
  194. $trade_no = $_POST['trade_no'];
  195. // 交易状态
  196. $trade_status = $_POST['trade_status'];
  197. $pay_common = new PayCommon();
  198. if($trade_status == "TRADE_SUCCESS"){
  199. $retval = $pay_common->onlinePay($out_trade_no, "alipay", $trade_no, "alipay");
  200. }
  201. echo "success";
  202. } else {
  203. // 验证失败
  204. echo "fail";
  205. }
  206. } catch (\Exception $e) {
  207. echo "fail";
  208. }
  209. }
  210. }