| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace addons\HuijuBill\common\global_func;
- use addons\HuijuBill\common\enums\HuijuBillTypeEnum;
- use common\components\payment\HuijuBill;
- use common\helpers\Url;
- use common\models\common\PaymentTrade;
- use common\models\common\PaymentTradeOrder;
- use common\models\common\PaymentTradeGroupOrder;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 公共调用支付
- */
- class Payment
- {
- /**
- * 阿里支付html缓存前缀
- */
- const ALIPAY_CACHE = 'huiju_bill_alipay_html:';
- /**
- * 支付
- *
- * @param array $params
- * @return array
- */
- public static function pay($params)
- {
- if (empty($params['action']) || empty($params['trade_type']) || empty($params['trade'])) {
- return [];
- }
- $action = $params['action'];
- $trade = $params['trade'];
- $trade_type = $params['trade_type'];
- if (!method_exists(self::class, $action)) {
- return [];
- }
- return self::$action($trade, $trade_type, $params);
- }
- /**
- * 汇聚分账聚合支付
- * @param PaymentTrade $trade
- * @param $tradeType
- * @param array $params
- * @return array|mixed
- * @throws Exception
- */
- public static function huiju_bill(PaymentTrade $trade, $tradeType, $params = [])
- {
- $pay_type = HuijuBillTypeEnum::action(HuijuBillTypeEnum::BILL_JOINPAY);
- $order = [
- 'title' => $trade->title,
- 'order_no' => $trade->out_trade_no,
- 'amount' => $trade->pay_fee,
- 'mall_id' => $trade->mall_id,
- 'notify_url' => Url::toApi(['notify/' . $pay_type], true),
- 'type' => $tradeType,
- 'open_id' => $params['openid'] ?? '',
- ];
- $config = self::getConfig($trade->mall_id, $pay_type);
- $pay = Yii::$app->payment->huijuBill($config);
- // 判断支付类型
- if (!in_array($tradeType, HuijuBillTypeEnum::getConstants())) {
- throw new Exception('未定义该支付类型');
- }
- $order = [
- 'trade_id' => $trade->id,
- 'title' => $trade->title,
- 'order_no' => $trade->out_trade_no,
- 'amount' => $trade->pay_fee,
- 'mall_id' => $trade->mall_id,
- 'notify_url' => Url::toApi(['notify/' . $pay_type], TRUE),
- 'type' => $tradeType,
- 'open_id' => $params['openid'] ?? ''
- ];
- $res = $pay->pay($order);
- // 给前端返回订单号和订单金额
- // 增加组合支付判断
- if(!empty($trade->is_pay_group)){
- $paymentOrderObj = PaymentTradeGroupOrder::class;
- }else{
- $paymentOrderObj = PaymentTradeOrder::class;
- }
- $trade_order = $paymentOrderObj::findOne(['trade_id' => $trade->id]);
- $res['order_no'] = $trade_order->order_no;
- $res['amount'] = $trade_order->total_fee;
- // 微信端支付
- if (in_array($tradeType, [HuijuBillTypeEnum::WEIXIN_GZH, HuijuBillTypeEnum::WEIXIN_XCX])) {
- $data = Json::decode($res['result']);
- $data['timestamp'] = $data['timeStamp'];
- return $data;
- }
- self::setAlipayHtml($trade->out_trade_no, $res['result']);
- $res['config'] = Url::toApi(['huiju-bill/v1/pay/index']) . '?order_no=' . $trade->out_trade_no;
- return $res;
- }
- /**
- * 获取商城支付配置
- * @param $mall_id
- * @param $pay_type
- * @return array|mixed
- * @throws
- */
- public static function getConfig($mall_id, $pay_type)
- {
- if ($mall_id) {
- $config = Yii::$app->services->setting->mall->get($mall_id, 'pay.' . $pay_type, true);
- } else {
- //获取系统配置
- $config = Yii::$app->services->setting->platform->get($pay_type, true);
- }
- if (empty($config)) {
- throw new \Exception('查询不到对应的支付配置');
- }
- return $config && is_string($config) ? Json::decode($config, true) : $config;
- }
- /**
- * 设置阿里支付的html缓存
- *
- * @param string $order_no
- * @param string $html
- * @return void
- */
- protected static function setAlipayHtml($order_no, $html)
- {
- $key = self::ALIPAY_CACHE . $order_no;
- Yii::$app->redis->setex($key, 600, $html);
- }
- /**
- * 删除阿里支付的html缓存
- *
- * @param string $order_no
- * @return void
- */
- public static function delAlipayHtml($order_no)
- {
- $key = self::ALIPAY_CACHE . $order_no;
- Yii::$app->redis->del($key);
- }
- /**
- * 获取阿里支付的html缓存
- *
- * @param string $order_no
- * @return void
- */
- public static function getAlipayHtml($order_no)
- {
- $key = self::ALIPAY_CACHE . $order_no;
- return Yii::$app->redis->get($key);
- }
- }
|