1, 'msg' => '订单信息或订单类型错误' ]; } try { self::init(); $extra['store_id'] = get_store_id(); $params = [ 'app_id' => self::$app_id, 'valid_time' => 48 * 60 * 60, // 两天 'cp_extra' => Json::encode($extra), // 'notify_url' => pay_notify_url(self::$notify_url), 'notify_url' => 'https://chidian.cyyvip.com/index.php/bytedance/notify', 'disable_msg' => 0, 'msg_type' => 'pages/home/home' ]; if ($type == OrderNo::ORDER_UNION) { $params['out_order_no'] = OrderNo::getOrderNo(OrderNo::ORDER_UNION); $params['body'] = $params['subject'] = count($order) . '笔订单合并支付'; $params['total_amount'] = $balance_price > 0 ? floatval($total_pay_price - $balance_price) * 100 : $total_pay_price * 100; $order_union = new OrderUnion(); $order_union->store_id = get_store_id(); $order_union->user_id = get_user()->id; $order_union->order_no = $params['out_trade_no']; $order_union->price = $total_pay_price; $order_union->is_pay = 0; $order_union->created_at = time(); $order_union->is_delete = 0; $order_id_list = []; foreach ($order as $value) { $order_id_list[] = $value->id; } $order_union->order_id_list = json_encode($order_id_list); if (!$order_union->save()) { foreach ($order_union->errors as $error) { return [ 'code' => 1, 'msg' => $error ]; } } } else { $params['out_order_no'] = $order->order_no; $params['body'] = $params['subject'] = $subject; $params['total_amount'] = $balance_price > 0 ? floatval($order->pay_price - $balance_price) * 100 : $order->pay_price * 100; } $params['sign'] = self::sign($params); \Yii::warning($params); $res = self::$bytedance->order->unify($params); \Yii::warning($res); if ($type == OrderNo::ORDER_UNION) { foreach ($order_id_list as $value) { $value->order_union_id = $order_union->id; $value->save(); } } if (!$res['err_no']) { return [ 'code' => 0, 'msg' => 'success', 'data' => $res['data'] ]; } else { return [ 'code' => $res['err_no'], 'msg' => $res['err_tips'] ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @param $subject * @param $price * @param string $body * @param array $extra * @throws Kernel\Exceptions\InvalidArgumentException * @throws Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public static function refund($order_no) { try { self::init(); $extra['store_id'] = get_store_id(); $params = [ 'app_id' => self::$app_id, 'cp_extra' => Json::encode($extra), // 'notify_url' => pay_notify_url(self::$notify_url), 'notify_url' => 'https://chidian.cyyvip.com/index.php/bytedance/notify', 'disable_msg' => 0, 'msg_type' => 'pages/home/home' ]; $params['out_order_no'] = $order_no; $params['out_refund_no'] = 'refund123456'; $params['reason'] = '无货'; $params['refund_amount'] = 499 * 100; $params['sign'] = self::sign($params); \Yii::warning($params); $res = self::$bytedance->order->refund($params); \Yii::warning($res); if (!$res['err_no']) { return [ 'code' => 0, 'msg' => 'success', 'data' => $res['data'] ]; } else { return [ 'code' => $res['err_no'], 'msg' => $res['err_tips'] ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 下单加签 * @param $map * @return string */ private static function sign($map) { $rList = array(); foreach($map as $k =>$v) { if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id") continue; $value = trim(strval($v)); $len = strlen($value); if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len-1) == "\"") $value = substr($value, 1, $len - 1); $value = trim($value); if ($value == "" || $value == "null") continue; array_push($rList, $value); } array_push($rList, self::$salt); sort($rList, 2); return md5(implode('&', $rList)); } /** * @param string $page * @param array $params * @return array */ public static function qrcode($page, $params) { if (empty($page)) { return [ 'code' => 1, 'msg' => 'page不能为空' ]; } self::init(); $str = $page; if (!empty($params)) { $str .= '?'; foreach ($params as $key => $param) { $str .= $key . '=' . $param . '&'; } } $str = substr($str, 0, -1); $file_name = md5($page . $str); // 保存小程序码到文件 $dir = \Yii::$app->runtimePath . '/image/dy_qrcode'; if (! is_dir($dir)) { mkdir($dir, 0777, true); } if (file_exists($dir. '/' .$file_name . '.jpg')) { $url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/dy_qrcode/' . $file_name); return [ 'code' => 0, 'root_path' => $dir . '/' . $file_name . '.jpg', 'url_path' => $url . '.jpg', ]; } $params = [ 'path' => $str, 'set_icon' => true ]; /** * @var \ByteDance\Kernel\Http\Response $response */ $response = self::$bytedance->qrcode->create($params); if ($response instanceof \ByteDance\Kernel\Http\Response) { $filename = $response->save($dir, $file_name); } else { return [ 'code' => 1, 'response' => $response, ]; } $url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/dy_qrcode/' . $filename); return [ 'code' => 0, 'root_path' => $dir . '/' . $filename, 'url_path' => $url, ]; } }