OrderPayService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 null $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. public function wechat(){
  60. $payData=Service::submitOrder(
  61. $this->getAmount('f'),
  62. $this->payment->order_no,
  63. 'wechat',
  64. $this->getBody(),
  65. $this->notifyUrl(),
  66. $this->returnUrl(),
  67. 'scan',
  68. '',
  69. $this->expire,
  70. );
  71. $str=$payData['code_url'];
  72. return [
  73. 'qr'=>makeQrcode($str),
  74. ];
  75. }
  76. public function notifyUrl(){
  77. return request()->root(true)."/index/payment/notify/order_no/".$this->payment->order_no;
  78. }
  79. public function returnUrl(){
  80. return h5_link('/user/order/success');
  81. }
  82. public function alipay(){
  83. $payData=Service::submitOrder(
  84. $this->getAmount(),
  85. $this->payment->order_no,
  86. 'alipay',
  87. $this->getBody(),
  88. $this->notifyUrl(),
  89. $this->returnUrl(),
  90. 'scan',
  91. '',
  92. $this->expire,
  93. );
  94. $str=$payData['qr_code'];
  95. return [
  96. 'qr'=>makeQrcode($str),
  97. ];
  98. }
  99. public function bankUnion(){
  100. require __DIR__.'/../library/upacp_demo_b2c/demo/api_01_gateway/Form_6_2_FrontConsume.php';
  101. $html=ylUnifyOrder($this->payment->order_no,$this->getAmount('f'),$this->expire,$this->notifyUrl(),$this->returnUrl());
  102. $cacheKey='payment_'.session_create_id();
  103. Cache::set($cacheKey,$html,60);
  104. return [
  105. 'url'=>sprintf('%s/%s?key=%s',request()->domain(),'redirect',$cacheKey),
  106. ];
  107. }
  108. public function otherUser(){}
  109. public function offline(){}
  110. public function pay(){
  111. $method=Arr::get(self::$methods,$this->payment->pay_type);
  112. return App::invokeMethod([$this,$method]);
  113. }
  114. }