AliPayService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop100%开源免费商用电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
  10. // | 访问官网:https://www.likemarket.net
  11. // | 访问社区:https://home.likemarket.net
  12. // | 访问手册:http://doc.likemarket.net
  13. // | 微信公众号:好象科技
  14. // | 好象科技开发团队 版权所有 拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | Author: LikeShopTeam-段誉
  17. // +----------------------------------------------------------------------
  18. namespace app\common\service\pay;
  19. use app\common\enum\PayEnum;
  20. use app\common\model\PayConfig;
  21. use app\common\service\pay\alipay\AlipayV2Default;
  22. use app\common\service\pay\alipay\AlipayV2Service;
  23. use app\common\service\pay\alipay\AlipayV3Default;
  24. use app\common\service\pay\alipay\AlipayV3Service;
  25. /**
  26. * 支付宝支付
  27. * Class AliPayService
  28. * @package app\common\server
  29. */
  30. class AliPayService extends BasePayService
  31. {
  32. /**
  33. * 支付实例
  34. * @var
  35. */
  36. protected $pay;
  37. /**
  38. * 初始化设置
  39. * AliPayService constructor.
  40. * @throws \Exception
  41. */
  42. public function __construct($terminal = null)
  43. {
  44. $config = (new PayConfig())->where(['pay_way' => PayEnum::ALI_PAY])->findOrEmpty()->toArray();
  45. if (empty($config['id'])) {
  46. throw new \Exception('请配置好支付设置');
  47. }
  48. $initData = [
  49. 'terminal' => $terminal,
  50. 'config' => $config,
  51. 'pay_way' => PayEnum::ALI_PAY,
  52. ];
  53. switch ($config['config']['merchant_type'] . '-' . $config['config']['mode']) {
  54. // 普通商户 普通加密
  55. case 'ordinary_merchant-normal_mode':
  56. $this->pay = new AlipayV2Default($initData);
  57. break;
  58. // 服务商 普通加密
  59. case 'service-normal_mode':
  60. $this->pay = new AlipayV2Service($initData);
  61. break;
  62. // 普通商户 证书加密
  63. case 'normal-v3':
  64. $this->pay = new AlipayV3Default($initData);
  65. break;
  66. // 服务商 证书加密
  67. case 'service-v3':
  68. $this->pay = new AlipayV3Service($initData);
  69. break;
  70. default:
  71. throw new \Exception('不支持的支付方式');
  72. }
  73. }
  74. function realPay()
  75. {
  76. return $this->pay;
  77. }
  78. function pay($form, $order)
  79. {
  80. return $this->pay->pay($form, $order);
  81. }
  82. function notify($data)
  83. {
  84. $this->pay->set_params([ 'notify_data' => $data ]);
  85. return $this->pay->payNotify();
  86. }
  87. function checkPay($orderSn)
  88. {
  89. $this->pay->set_params([ 'query_pay_data' => [ 'order_sn' => $orderSn ] ]);
  90. return $this->pay->queryPay();
  91. }
  92. function refund($orderSn, $orderAmount, $outRequestNo)
  93. {
  94. $this->pay->set_params([
  95. 'refund_data' => [
  96. 'order_sn' => $orderSn,
  97. 'order_amount' => $orderAmount,
  98. 'out_request_no' => $outRequestNo,
  99. ]
  100. ]);
  101. return $this->pay->refund();
  102. }
  103. function queryRefund($orderSn, $refundSn)
  104. {
  105. $this->pay->set_params([
  106. 'query_refund_data' => [
  107. 'order_sn' => $orderSn,
  108. 'refund_sn' => $refundSn,
  109. ]
  110. ]);
  111. return $this->pay->queryRefund();
  112. }
  113. }