AlipayPaymentService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\data\service\payment;
  3. use AliPay\App;
  4. use AliPay\Wap;
  5. use AliPay\Web;
  6. use app\data\service\PaymentService;
  7. use think\admin\Exception;
  8. use WeChat\Exceptions\InvalidResponseException;
  9. use WeChat\Exceptions\LocalCacheException;
  10. /**
  11. * 支付宝支付基础服务
  12. * Class AlipayPaymentService
  13. * @package app\data\service\payment
  14. */
  15. class AlipayPaymentService extends PaymentService
  16. {
  17. /**
  18. * 支付参数配置
  19. * @var array
  20. */
  21. protected $config = [];
  22. /**
  23. * 创建订单支付参数
  24. * @param string $openid 用户OPENID
  25. * @param string $orderNo 交易订单单号
  26. * @param string $payAmount 交易订单金额(元)
  27. * @param string $payTitle 交易订单名称
  28. * @param string $payRemark 订单订单描述
  29. * @param string $payReturn 完成回跳地址
  30. * @param string $payImage 支付凭证图片
  31. * @return array
  32. * @throws Exception
  33. */
  34. public function create(string $openid, string $orderNo, string $payAmount, string $payTitle, string $payRemark, string $payReturn = '', string $payImage = '',string $notify_url = '',array $parm = []): array
  35. {
  36. try {
  37. if (isset(static::TYPES[$this->type])) {
  38. $tradeType = static::TYPES[$this->type]['type'];
  39. } else {
  40. throw new Exception(sprintf('支付类型[%s]未配置定义!', $this->type));
  41. }
  42. $this->config['notify_url'] = $notify_url;
  43. if (in_array($tradeType, [static::PAYMENT_ALIPAY_WAP, static::PAYMENT_ALIPAY_WEB])) {
  44. if (empty($payReturn)) {
  45. throw new Exception('支付回跳地址不能为空!');
  46. } else {
  47. $this->config['return_url'] = $payReturn;
  48. }
  49. }
  50. if ($tradeType === static::PAYMENT_ALIAPY_APP) {
  51. $payment = App::instance($this->config);
  52. } elseif ($tradeType === static::PAYMENT_ALIPAY_WAP) {
  53. $payment = Wap::instance($this->config);
  54. } elseif ($tradeType === static::PAYMENT_ALIPAY_WEB) {
  55. $payment = Web::instance($this->config);
  56. } else {
  57. throw new Exception("支付类型[{$tradeType}]暂时不支持!");
  58. }
  59. $data = ['out_trade_no' => $orderNo, 'total_amount' => $payAmount, 'subject' => $payTitle];
  60. if (!empty($payRemark)) $data['body'] = $payRemark;
  61. $result = $payment->apply($data);
  62. // 返回支付参数
  63. return ['result' => $result];
  64. } catch (Exception $exception) {
  65. throw $exception;
  66. } catch (\Exception $exception) {
  67. throw new Exception($exception->getMessage(), $exception->getCode());
  68. }
  69. }
  70. /**
  71. * 支付结果处理
  72. * @return string
  73. * @throws InvalidResponseException
  74. */
  75. public function opvip(): string
  76. {
  77. $notify = App::instance($this->config)->notify();
  78. file_put_contents("openvipzfborder.txt", json_encode($notify,true) . "\n" . "\n", FILE_APPEND);
  79. if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  80. if ($this->updateOpvipAction($notify)) {
  81. return 'success';
  82. } else {
  83. return 'error';
  84. }
  85. } else {
  86. return 'success';
  87. }
  88. }
  89. /**
  90. * 支付结果处理
  91. * @return string
  92. * @throws InvalidResponseException
  93. */
  94. public function payorder(): string
  95. {
  96. $notify = App::instance($this->config)->notify();
  97. file_put_contents("appayorder.txt", json_encode($notify,true) . "\n" . "\n", FILE_APPEND);
  98. if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  99. if ($this->updatePayorderAction($notify)) {
  100. return 'success';
  101. } else {
  102. return 'error';
  103. }
  104. } else {
  105. return 'success';
  106. }
  107. }
  108. /**
  109. * 查询订单数据
  110. * @param string $orderNo
  111. * @return array
  112. * @throws InvalidResponseException
  113. * @throws LocalCacheException
  114. */
  115. public function query(string $orderNo): array
  116. {
  117. return App::instance($this->config)->query($orderNo);
  118. }
  119. /**
  120. * 支付服务初始化
  121. * @return $this
  122. */
  123. protected function initialize(): AlipayPaymentService
  124. {
  125. $this->config = [
  126. // 沙箱模式
  127. 'debug' => false,
  128. // 签名类型(RSA|RSA2)
  129. 'sign_type' => "RSA2",
  130. // 应用ID
  131. 'appid' => $this->params['alipay_appid'],
  132. // 支付宝公钥 (1行填写,特别注意,这里是支付宝公钥,不是应用公钥,最好从开发者中心的网页上去复制)
  133. 'public_key' => $this->_trimCertHeader($this->params['alipay_public_key']),
  134. // 支付宝私钥 (1行填写)
  135. 'private_key' => $this->_trimCertHeader($this->params['alipay_private_key']),
  136. // 支付成功通知地址
  137. 'notify_url' => '',
  138. // 网页支付回跳地址
  139. 'return_url' => '',
  140. ];
  141. return $this;
  142. }
  143. /**
  144. * 去除证书内容前后缀
  145. * @param string $content
  146. * @return string
  147. */
  148. private function _trimCertHeader(string $content): string
  149. {
  150. return preg_replace(['/\s+/', '/-{5}.*?-{5}/'], '', $content);
  151. }
  152. }