Payment.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace addons\HuijuBill\common\global_func;
  3. use addons\HuijuBill\common\enums\HuijuBillTypeEnum;
  4. use common\components\payment\HuijuBill;
  5. use common\helpers\Url;
  6. use common\models\common\PaymentTrade;
  7. use common\models\common\PaymentTradeOrder;
  8. use common\models\common\PaymentTradeGroupOrder;
  9. use Exception;
  10. use Yii;
  11. use yii\helpers\Json;
  12. /**
  13. * 公共调用支付
  14. */
  15. class Payment
  16. {
  17. /**
  18. * 阿里支付html缓存前缀
  19. */
  20. const ALIPAY_CACHE = 'huiju_bill_alipay_html:';
  21. /**
  22. * 支付
  23. *
  24. * @param array $params
  25. * @return array
  26. */
  27. public static function pay($params)
  28. {
  29. if (empty($params['action']) || empty($params['trade_type']) || empty($params['trade'])) {
  30. return [];
  31. }
  32. $action = $params['action'];
  33. $trade = $params['trade'];
  34. $trade_type = $params['trade_type'];
  35. if (!method_exists(self::class, $action)) {
  36. return [];
  37. }
  38. return self::$action($trade, $trade_type, $params);
  39. }
  40. /**
  41. * 汇聚分账聚合支付
  42. * @param PaymentTrade $trade
  43. * @param $tradeType
  44. * @param array $params
  45. * @return array|mixed
  46. * @throws Exception
  47. */
  48. public static function huiju_bill(PaymentTrade $trade, $tradeType, $params = [])
  49. {
  50. $pay_type = HuijuBillTypeEnum::action(HuijuBillTypeEnum::BILL_JOINPAY);
  51. $order = [
  52. 'title' => $trade->title,
  53. 'order_no' => $trade->out_trade_no,
  54. 'amount' => $trade->pay_fee,
  55. 'mall_id' => $trade->mall_id,
  56. 'notify_url' => Url::toApi(['notify/' . $pay_type], true),
  57. 'type' => $tradeType,
  58. 'open_id' => $params['openid'] ?? '',
  59. ];
  60. $config = self::getConfig($trade->mall_id, $pay_type);
  61. $pay = Yii::$app->payment->huijuBill($config);
  62. // 判断支付类型
  63. if (!in_array($tradeType, HuijuBillTypeEnum::getConstants())) {
  64. throw new Exception('未定义该支付类型');
  65. }
  66. $order = [
  67. 'trade_id' => $trade->id,
  68. 'title' => $trade->title,
  69. 'order_no' => $trade->out_trade_no,
  70. 'amount' => $trade->pay_fee,
  71. 'mall_id' => $trade->mall_id,
  72. 'notify_url' => Url::toApi(['notify/' . $pay_type], TRUE),
  73. 'type' => $tradeType,
  74. 'open_id' => $params['openid'] ?? ''
  75. ];
  76. $res = $pay->pay($order);
  77. // 给前端返回订单号和订单金额
  78. // 增加组合支付判断
  79. if(!empty($trade->is_pay_group)){
  80. $paymentOrderObj = PaymentTradeGroupOrder::class;
  81. }else{
  82. $paymentOrderObj = PaymentTradeOrder::class;
  83. }
  84. $trade_order = $paymentOrderObj::findOne(['trade_id' => $trade->id]);
  85. $res['order_no'] = $trade_order->order_no;
  86. $res['amount'] = $trade_order->total_fee;
  87. // 微信端支付
  88. if (in_array($tradeType, [HuijuBillTypeEnum::WEIXIN_GZH, HuijuBillTypeEnum::WEIXIN_XCX])) {
  89. $data = Json::decode($res['result']);
  90. $data['timestamp'] = $data['timeStamp'];
  91. return $data;
  92. }
  93. self::setAlipayHtml($trade->out_trade_no, $res['result']);
  94. $res['config'] = Url::toApi(['huiju-bill/v1/pay/index']) . '?order_no=' . $trade->out_trade_no;
  95. return $res;
  96. }
  97. /**
  98. * 获取商城支付配置
  99. * @param $mall_id
  100. * @param $pay_type
  101. * @return array|mixed
  102. * @throws
  103. */
  104. public static function getConfig($mall_id, $pay_type)
  105. {
  106. if ($mall_id) {
  107. $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.' . $pay_type, true);
  108. } else {
  109. //获取系统配置
  110. $config = Yii::$app->services->setting->platform->get($pay_type, true);
  111. }
  112. if (empty($config)) {
  113. throw new \Exception('查询不到对应的支付配置');
  114. }
  115. return $config && is_string($config) ? Json::decode($config, true) : $config;
  116. }
  117. /**
  118. * 设置阿里支付的html缓存
  119. *
  120. * @param string $order_no
  121. * @param string $html
  122. * @return void
  123. */
  124. protected static function setAlipayHtml($order_no, $html)
  125. {
  126. $key = self::ALIPAY_CACHE . $order_no;
  127. Yii::$app->redis->setex($key, 600, $html);
  128. }
  129. /**
  130. * 删除阿里支付的html缓存
  131. *
  132. * @param string $order_no
  133. * @return void
  134. */
  135. public static function delAlipayHtml($order_no)
  136. {
  137. $key = self::ALIPAY_CACHE . $order_no;
  138. Yii::$app->redis->del($key);
  139. }
  140. /**
  141. * 获取阿里支付的html缓存
  142. *
  143. * @param string $order_no
  144. * @return void
  145. */
  146. public static function getAlipayHtml($order_no)
  147. {
  148. $key = self::ALIPAY_CACHE . $order_no;
  149. return Yii::$app->redis->get($key);
  150. }
  151. }