OrderPayService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\common\service;
  3. use addons\epay\library\Service;
  4. use app\common\model\Orders;
  5. use app\common\model\Payment;
  6. use fast\Arr;
  7. use think\App;
  8. use think\Cache;
  9. class OrderPayService{
  10. /** @var Payment */
  11. protected $payment;
  12. /** @var string */
  13. protected $body;
  14. protected $expire=null;
  15. public static $methods=[
  16. Orders::PT_QYWY=>'companyBank',
  17. Orders::PT_WX=>'wechat',
  18. Orders::PT_ZFB=>'alipay',
  19. Orders::PT_YL=>'bankUnion',
  20. Orders::PT_DF=>'otherUser',
  21. Orders::PT_OFF=>'offline',
  22. ];
  23. /**
  24. * @param $expire
  25. */
  26. public function setExpire($expire): void
  27. {
  28. $this->expire = $expire;
  29. }
  30. /**
  31. * @param Payment $payment
  32. */
  33. public function setPayment(Payment $payment): void
  34. {
  35. $this->payment = $payment;
  36. }
  37. /**
  38. * @param string $body
  39. */
  40. public function setBody(string $body): void
  41. {
  42. $this->body = $body;
  43. }
  44. /**
  45. * @return string
  46. */
  47. public function getBody(): string
  48. {
  49. return $this->body;
  50. }
  51. /**
  52. * @return mixed
  53. */
  54. public function getAmount($unit='y')
  55. {
  56. return moneyFormat($this->payment->amount,$unit);
  57. }
  58. public function companyBank(){
  59. require __DIR__.'/../library/upacp_demo_b2b/demo/api_02_b2b/Form_6_2_FrontConsume.php';
  60. $html=uniCompanyPay($this->payment->order_no,$this->getAmount('f'),$this->expire,$this->notifyUrl(),$this->returnUrl());
  61. $cacheKey='payment_'.session_create_id();
  62. Cache::set($cacheKey,$html,60);
  63. return [
  64. 'url'=>sprintf('%s/%s?key=%s',request()->root(true),'redirect',$cacheKey),
  65. ];
  66. }
  67. public function wechat(){
  68. $payData=Service::submitOrder(
  69. $this->getAmount(),
  70. $this->payment->order_no,
  71. 'wechat',
  72. $this->getBody(),
  73. $this->notifyUrl(),
  74. $this->returnUrl(),
  75. 'scan',
  76. '',
  77. $this->expire
  78. );
  79. $str=$payData['code_url'];
  80. return [
  81. 'qr'=>makeQrcode($str),
  82. ];
  83. }
  84. public function notifyUrl(){
  85. return request()->root(true)."/index/payment/notify/order_no/".$this->payment->order_no;
  86. }
  87. public function returnUrl(){
  88. return h5_link('/user/order/success');
  89. }
  90. public function alipay(){
  91. $payData=Service::submitOrder(
  92. $this->getAmount(),
  93. $this->payment->order_no,
  94. 'alipay',
  95. $this->getBody(),
  96. $this->notifyUrl(),
  97. $this->returnUrl(),
  98. 'scan',
  99. '',
  100. $this->expire,
  101. );
  102. $str=$payData['qr_code'];
  103. return [
  104. 'qr'=>makeQrcode($str),
  105. ];
  106. }
  107. public function bankUnion(){
  108. require __DIR__.'/../library/upacp_demo_b2c/demo/api_01_gateway/Form_6_2_FrontConsume.php';
  109. $html=ylUnifyOrder($this->payment->order_no,$this->getAmount('f'),$this->expire,$this->notifyUrl(),$this->returnUrl());
  110. $cacheKey='payment_'.session_create_id();
  111. Cache::set($cacheKey,$html,60);
  112. return [
  113. 'url'=>sprintf('%s/%s?key=%s',request()->root(true),'redirect',$cacheKey),
  114. ];
  115. }
  116. public function otherUser(){
  117. return [
  118. 'expire'=>3
  119. ];
  120. }
  121. public function offline(){}
  122. public function pay(){
  123. $method=Arr::get(self::$methods,$this->payment->pay_type);
  124. return App::invokeMethod([$this,$method]);
  125. }
  126. }