AlipayPaymentService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  79. if ($this->updateOpvipAction($notify)) {
  80. return 'success';
  81. } else {
  82. return 'error';
  83. }
  84. } else {
  85. return 'success';
  86. }
  87. }
  88. /**
  89. * 支付结果处理
  90. * @return string
  91. * @throws InvalidResponseException
  92. */
  93. public function payorder(): string
  94. {
  95. $notify = App::instance($this->config)->notify();
  96. file_put_contents("appayorder.txt", json_encode($notify,true) . "\n" . "\n", FILE_APPEND);
  97. if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  98. if ($this->updatePayorderAction($notify)) {
  99. return 'success';
  100. } else {
  101. return 'error';
  102. }
  103. } else {
  104. return 'success';
  105. }
  106. }
  107. /**
  108. * 查询订单数据
  109. * @param string $orderNo
  110. * @return array
  111. * @throws InvalidResponseException
  112. * @throws LocalCacheException
  113. */
  114. public function query(string $orderNo): array
  115. {
  116. return App::instance($this->config)->query($orderNo);
  117. }
  118. /**
  119. * 支付服务初始化
  120. * @return $this
  121. */
  122. protected function initialize(): AlipayPaymentService
  123. {
  124. $this->config = [
  125. // 沙箱模式
  126. 'debug' => false,
  127. // 签名类型(RSA|RSA2)
  128. 'sign_type' => "RSA2",
  129. // 应用ID
  130. 'appid' => $this->params['alipay_appid'],
  131. // 支付宝公钥 (1行填写,特别注意,这里是支付宝公钥,不是应用公钥,最好从开发者中心的网页上去复制)
  132. 'public_key' => $this->_trimCertHeader($this->params['alipay_public_key']),
  133. // 支付宝私钥 (1行填写)
  134. 'private_key' => $this->_trimCertHeader($this->params['alipay_private_key']),
  135. // 支付成功通知地址
  136. 'notify_url' => '',
  137. // 网页支付回跳地址
  138. 'return_url' => '',
  139. ];
  140. return $this;
  141. }
  142. /**
  143. * 去除证书内容前后缀
  144. * @param string $content
  145. * @return string
  146. */
  147. private function _trimCertHeader(string $content): string
  148. {
  149. return preg_replace(['/\s+/', '/-{5}.*?-{5}/'], '', $content);
  150. }
  151. }