| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace ByteDance;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderUnion;
- use app\utils\OrderNo;
- use GuzzleHttp\Exception\GuzzleException;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class ByteDance
- {
- /**
- * @var 是否是平台小程序
- */
- public static $is_platform;
- public static $store_id;
- public static $app_id;
- public static $salt;
- public static $notify_url = 'bytedance/notify';
- /**
- * @var \ByteDance\MiniProgram\Application
- */
- public static $bytedance;
- public static function init() {
- self::$is_platform = get_params('is_platform', 'yes');
- self::$store_id = get_store_id();
- if (self::$is_platform == 'no') {
- $bytedance_config = Option::get('douyin', self::$store_id, 'store');
- if (!empty($bytedance_config['value'])) {
- $bytedance_config = Json::decode($bytedance_config['value']);
- $config['app_id'] = $bytedance_config['app_id'];
- $config['app_secret'] = $bytedance_config['app_secret'];
- $config['salt'] = $bytedance_config['salt'];
- }
- } else {
- $bytedance_config = Option::getSaasPlatformBytedance();
- $config['app_id'] = $bytedance_config['appid'];
- $config['app_secret'] = $bytedance_config['key'];
- $config['salt'] = $bytedance_config['salt'];
- }
- self::$app_id = $config['app_id'];
- self::$salt = $config['salt'];
- self::$bytedance = Factory::miniProgram($config);
- }
- /**
- * @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 pay($order, $type, $subject, $total_pay_price = 0, $balance_price = 0, $extra = []) {
- if (!$order || (!empty($type) && !in_array($type, OrderNo::$validOrderType)) || ($type != OrderNo::ORDER_UNION && !$subject)) {
- return [
- 'code' => 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,
- ];
- }
- }
|