alipay.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare (strict_types = 1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-10-04 18:31
  12. // +----------------------------------------------------------------------
  13. use Alipay\EasySDK\Kernel\Factory;
  14. use Alipay\EasySDK\Kernel\Config;
  15. class alipay {
  16. private $config;
  17. public function __construct($config = [])
  18. {
  19. if(empty($config)) $config = config('alipay');
  20. $this->config = $config;
  21. Factory::setOptions($this->getOptions());
  22. }
  23. /**
  24. * 手机支付
  25. * @param $data
  26. */
  27. public function wapPay($data) {
  28. $alipay = config('alipay');
  29. $result = Factory::payment()
  30. ->wap()
  31. ->pay($data['name'],$data['order_id'], $data['money'],$alipay['returnUrl'],$alipay['returnUrl']);
  32. return $result->body;
  33. }
  34. /**
  35. * 电脑支付
  36. * @param $data
  37. * @return \Alipay\EasySDK\Payment\Page\Models\AlipayTradePagePayResponse
  38. * @throws \Exception
  39. */
  40. public function pay($data) {
  41. $alipay = config('alipay');
  42. $result = Factory::payment()->page()->pay($data['name'],
  43. $data['order_id'],
  44. $data['money'],
  45. $alipay['returnUrl']
  46. );
  47. return $result;
  48. }
  49. /**
  50. * 支付验证
  51. * @param $data
  52. * @return bool
  53. * @throws \Exception
  54. */
  55. public function verifyNotify($data) {
  56. $result = Factory::payment()->common()->verifyNotify($data);
  57. return $result;
  58. }
  59. /**
  60. * 查询订单号
  61. * @param $outTradeNo
  62. * @throws \Exception
  63. */
  64. public function query($outTradeNo){
  65. $result = Factory::payment()->common()->query($outTradeNo);
  66. return $result;
  67. }
  68. /**
  69. * 转账操作
  70. * @param $bank
  71. * @param $trans_amount
  72. * @param $name
  73. * @throws \Exception
  74. */
  75. public function accountTransfer($bank,$trans_amount,$name){
  76. $data = [];
  77. $data['out_biz_no'] = md5($bank . time());
  78. $data['trans_amount'] = $trans_amount;
  79. $data['product_code'] = 'TRANS_ACCOUNT_NO_PWD';
  80. $data['biz_scene'] = 'DIRECT_TRANSFER';
  81. $data['order_title'] = '提现转账';
  82. $data['payee_info'] = [
  83. 'identity' => $bank,
  84. 'identity_type' => 'ALIPAY_LOGON_ID',
  85. 'name' => $name
  86. ];
  87. require_once ROOT . '/../vendor/alipaysdk/oldsdk/AopCertClient.php';
  88. require_once ROOT . '/../vendor/alipaysdk/oldsdk/request/AlipayFundTransUniTransferRequest.php';
  89. $aop = new \AopCertClient();
  90. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  91. $aop->appId = $this->config['appId'];
  92. $aop->rsaPrivateKey = $this->config['merchantPrivateKey'];
  93. $aop->alipayrsaPublicKey= $aop->getPublicKey($this->config['alipayCertPath']);
  94. $aop->appCertSN = $aop->getCertSN($this->config['merchantCertPath']);//调用getCertSN获取证书序列号
  95. $aop->alipayRootCertSN = $aop->getRootCertSN($this->config['alipayRootCertPath']);
  96. $aop->isCheckAlipayPublicCert = true;
  97. $aop->apiVersion = '1.0';
  98. $aop->signType = 'RSA2';
  99. $aop->postCharset='utf-8';
  100. $aop->format='json';
  101. $request = new \AlipayFundTransUniTransferRequest();
  102. $request->setBizContent(json_encode($data,\JSON_UNESCAPED_UNICODE));
  103. $result = $aop->execute ( $request);
  104. if($result->alipay_fund_trans_uni_transfer_response->code == 10000) {
  105. return ['code'=>1000,
  106. 'msg'=>'成功',
  107. 'out_biz_no'=>$data['out_biz_no'],
  108. 'pay_fund_order_id' => $result->alipay_fund_trans_uni_transfer_response->pay_fund_order_id,
  109. 'order_id' => $result->alipay_fund_trans_uni_transfer_response->order_id
  110. ];
  111. } else {
  112. return ['code'=>-1000,'msg'=>$result->alipay_fund_trans_uni_transfer_response->sub_msg,'orderId'=>$data['out_biz_no']];
  113. }
  114. }
  115. function getOptions()
  116. {
  117. $options = new Config();
  118. $options->protocol = 'https';
  119. $options->gatewayHost = 'openapi.alipay.com';
  120. $options->signType = 'RSA2';
  121. $options->appId = $this->config['appId'];
  122. //请填写您的应用私钥
  123. $options->merchantPrivateKey = $this->config['merchantPrivateKey'];
  124. //支付宝公钥证书
  125. $options->alipayCertPath = $this->config['alipayCertPath'];
  126. //请填写您的支付宝根证书文件路径
  127. $options->alipayRootCertPath = $this->config['alipayRootCertPath'];
  128. //请填写您的应用公钥证书文件路径
  129. $options->merchantCertPath = $this->config['merchantCertPath'];
  130. //请填写您的支付类接口异步通知接收服务地址
  131. $options->notifyUrl = $this->config['notifyUrl'];
  132. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  133. // $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
  134. return $options;
  135. }
  136. }