代码改变世界

mercadolibre 美卡多 本土授权(PKCE)

2023-02-17 14:10  天心PHP  阅读(464)  评论(1编辑  收藏  举报

API接口文档: 身份验证和授权 (mercadolibre.com.mx) https://developers.mercadolibre.com.mx/es_ar/autenticacion-y-autorizacion/

    /**
     * 生成授权链接
     * @param string $redirect_uri
     * @return string
     */
    public function getAuthUrl($redirect_uri,$app_id) {
        $this->redirect_uri = $redirect_uri;
        $randomn = $this->generateRandomString(48);
        $appmodel = new MercadolibreAPP();
        $appmodel->updateAll(['code_verifier'=>$randomn],"id=".$app_id);//产生的随机数记录到数据库

        $encoded = base64_encode(hash('sha256', $randomn, true));//code_verifier经过sha256
        $code_challenge = strtr(rtrim($encoded, '='), '+/', '-_');//整理 得到 code_challenge

        $params = array("client_id" => $this->client_id, "response_type" => "code", "redirect_uri" => $redirect_uri,'code_challenge'=>$code_challenge,'code_challenge_method'=>'S256');
        if(in_array($this->client_id,[8274783443247575,8149149353703108,2401922632679187,6739263324786978,878999950093151,2621145909128904,4224821929868485,4367955135326677])){ //本土账号测试
            return 'https://auth.mercadolibre.com.mx/authorization?'.http_build_query($params);
        }
        return 'https://global-selling.mercadolibre.com/authorization?'.http_build_query($params);
      //  return $this->request_url."/merchant/authorization?".http_build_query($params);
    }
    //生成随机数
    public function generateRandomString($length = 48) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;//随机字符串
    }

上述会得到授权的链接 例如:

https://auth.mercadolibre.com.mx/authorization?client_id=4367955135326677&response_type=code&redirect_uri=https%3A%2F%2Fpublish.xxxxxxxx.com%2Findex.php%2Fservices%2Fmercadolibre%2Fmercadolibreauthorize%2Faccept%2Fstate%2F61&code_challenge=Xgemq5fLBSsZAe3dl2KteKYPg_AJja_0rrcGxNbbH_g&code_challenge_method=S256

里面多了两个参数 code_challenge:随机数(code_verifier)进过 sha256 加密的参数

根据返回的code得到token:

public function authorize($code, $redirect_uri,$account_id) {

        if($redirect_uri)
            $this->redirect_uri = $redirect_uri;

        $body = array(
            "grant_type" => "authorization_code",
            "client_id" => $this->client_id,
            "client_secret" => $this->client_secret,
            "code" => $code,
            "redirect_uri" => $this->redirect_uri.'/state/'.$account_id,
            'code_verifier' =>$this->code_verifier //这里是生成授权链接的时候产生的随机数
        );
        $opts = array(
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $body
        );

        $request = $this->execute(self::$OAUTH_URL, $opts);

        if($request["httpCode"] == 200) {
            $this->access_token = $request["body"]->access_token;

            if($request["body"]->refresh_token)
                $this->refresh_token = $request["body"]->refresh_token;

            return $request;

        } else {
            return $request;
        }
    }

 促销活动

    /**
     * 获取user下面的活动
     */
    public function getuserpromotion($user_id,$token){
        return $this->api()->get('/marketplace/seller-promotions/users/'.$user_id,['access_token' => $token ]);
    }

结果:

Array
(
    [body] => stdClass Object
        (
            [results] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => MLM1029
                            [type] => DEAL
                            [status] => started
                            [start_date] => 2019-05-15T05:00:00Z
                            [finish_date] => 2030-05-16T04:59:00Z
                            [deadline_date] => 2030-05-16T04:59:00Z
                            [name] => supermercado discovery 2
                        )

                    [1] => stdClass Object
                        (
                            [id] => MLM28215
                            [type] => DEAL
                            [status] => pending
                            [start_date] => 2023-02-27T14:00:00Z
                            [finish_date] => 2023-03-20T18:00:00Z
                            [deadline_date] => 2023-03-20T15:00:00Z
                            [name] => hogar de ensueno
                        )

                    [2] => stdClass Object
                        (
                            [id] => MLM28462
                            [type] => DEAL
                            [status] => pending
                            [start_date] => 2023-03-01T06:00:00Z
                            [finish_date] => 2023-04-03T18:00:00Z
                            [deadline_date] => 2023-04-02T05:00:00Z
                            [name] => Liquidación FULL CBT2
                        )

                    [3] => stdClass Object
                        (
                            [id] => MLM28447
                            [type] => DEAL
                            [status] => pending
                            [start_date] => 2023-03-06T06:00:00Z
                            [finish_date] => 2023-04-03T18:00:00Z
                            [deadline_date] => 2023-03-18T05:00:00Z
                            [name] => nueva temporada primavera
                        )

                )

            [paging] => stdClass Object
                (
                    [offset] => 0
                    [limit] => 50
                    [total] => 4
                )

        )

    [httpCode] => 200
)
    /**
     * 获取产品的详细信息
     */
    public function get_one_listing($mpid,$token)
    {
        return $this->api()->get('/marketplace/items/'.$mpid,['access_token' => $token ]);
    }

结果

Array
(
    [body] => stdClass Object
        (
            [id] => MLM1369197772
            [site_id] => MLM
            [date_created] => 2022-01-25T06:33:24Z
            [title] => Cinta Cinta De Costura De Césped Cinta Adhesiva De Hierba Pa
            [descriptions] => Array
                (
                )

            [listing_type_id] => gold_pro
            [permalink] => https://articulo.mercadolibre.com.mx/MLM-1369197772-cinta-cinta-de-costura-de-cesped-cinta-adhesiva-de-hierba-pa-_JM
            [shipping] => stdClass Object
                (
                    [mode] => me2
                    [free_shipping] => 
                    [logistic_type] => fulfillment
                    [dimensions] => 
                    [tags] => Array
                        (
                            [0] => cbt_fulfillment
                        )

                )

            [status] => active
            [sub_status] => Array
                (
                )

            [tags] => Array
                (
                    [0] => cbt_item
                    [1] => good_quality_picture
                    [2] => good_quality_thumbnail
                    [3] => immediate_payment
                    [4] => cart_eligible
                )

            [warranty] => Garantía del vendedor: 1 meses
            [catalog_product_id] => 
            [domain_id] => MLM-FENCE_PRIVACY_TAPES
            [international_delivery_mode] => none
            [inventory_id] => 
            [category_id] => MLM191808
            [official_store_id] => 
            [sold_quantity] => 81
            [available_quantity] => 73
            [last_updated] => 2023-02-25T07:23:16Z
            [seller_id] => 535002606
            [price] => 10.94
            [original_price] => 11
            [currency_id] => USD
            [base_exchange_rate] => 0
            [user_logistic_type] => fulfillment
            [owner_id] => 534959766
            [cbt_item_id] => CBT1451245163
            [variations] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 174127346769
                            [price] => 10.94
                            [currency_id] => USD
                            [attribute_combinations] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => WIDTH
                                            [name] => Ancho
                                            [value_id] => 
                                            [value_name] => 100 cm
                                            [value_struct] => stdClass Object
                                                (
                                                    [number] => 100
                                                    [unit] => cm
                                                )

                                            [values] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [id] => 
                                                            [name] => 100 cm
                                                            [struct] => stdClass Object
                                                                (
                                                                    [number] => 100
                                                                    [unit] => cm
                                                                )

                                                        )

                                                )

                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => COLOR
                                            [name] => Color
                                            [value_id] => 52049
                                            [value_name] => Negro
                                            [value_struct] => 
                                            [values] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [id] => 52049
                                                            [name] => Negro
                                                            [struct] => 
                                                        )

                                                )

                                        )

                                )

                            [available_quantity] => 73
                            [sold_quantity] => 81
                            [sale_terms] => Array
                                (
                                )

                            [picture_ids] => Array
                                (
                                    [0] => 956224-CBT49038628127_022022
                                    [1] => 687526-CBT49038570874_022022
                                    [2] => 969069-CBT49038569907_022022
                                    [3] => 966222-CBT49038623212_022022
                                    [4] => 601244-CBT49038599809_022022
                                    [5] => 664989-CBT49038610537_022022
                                )

                            [seller_custom_field] => 
                            [catalog_product_id] => 
                            [inventory_id] => XKUP63703
                            [item_relations] => Array
                                (
                                )

                        )

                )

            [item_relations] => Array
                (
                )

        )

    [httpCode] => 200
)
    /**
     * 获取产品参加的促销活动
     */
    public function checkpromotion($item_id,$user_id,$token){
        return $this->api()->get('/marketplace/seller-promotions/items/'.$item_id,['user_id'=>$user_id,'access_token' => $token ]);
    }

结果:

Array
(
    [body] => Array
        (
            [0] => stdClass Object
                (
                    [id] => MLM28447
                    [type] => DEAL
                    [status] => pending
                    [start_date] => 2023-03-06T00:00:00-06:00
                    [finish_date] => 2023-04-03T13:00:00-05:00
                    [deadline_date] => 2023-03-18T05:00:00Z
                    [name] => nueva temporada primavera
                )

            [1] => stdClass Object
                (
                    [id] => MLM20378
                    [type] => DEAL
                    [status] => started
                    [start_date] => 2022-06-17T12:00:00-05:00
                    [finish_date] => 2030-12-31T22:59:00-06:00
                    [name] => New Sellers ramp up
                )

            [2] => stdClass Object
                (
                    [id] => 
                    [type] => PRICE_DISCOUNT
                    [status] => candidate
                    [start_date] => 
                    [finish_date] => 
                )

        )

    [httpCode] => 200
)
    /**
     * 获取促销活动下面的产品
     */
    public function getpromotionitem($user_id,$deal_id,$token){
        return $this->api()->get('/marketplace/seller-promotions/promotions/'.$deal_id.'/items',['user_id'=>$user_id,'access_token' => $token ]);
    }

结果:

Array
(
    [body] => stdClass Object
        (
            [results] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => MLM935784236
                            [status] => candidate
                            [price] => 10.78
                            [original_price] => 14
                            [currency_id] => USD
                        )

                    [1] => stdClass Object
                        (
                            [id] => MLM835936593
                            [status] => candidate
                            [price] => 13.71
                            [original_price] => 18.28
                            [currency_id] => USD
                        )

                    [2] => stdClass Object
                        (
                            [id] => MLM785998594
                            [status] => candidate
                            [price] => 11.74
                            [original_price] => 14.5
                            [currency_id] => USD
                        )

                    [3] => stdClass Object
                        (
                            [id] => MLM779766727
                            [status] => candidate
                            [price] => 6.72
                            [original_price] => 8
                            [currency_id] => USD
                        )

                    [4] => stdClass Object
                        (
                            [id] => MLM1405348263
                            [status] => candidate
                            [price] => 7.7
                            [original_price] => 14
                            [currency_id] => USD
                        )

                    [5] => stdClass Object
                        (
                            [id] => MLM1369243224
                            [status] => candidate
                            [price] => 25.79
                            [original_price] => 29.99
                            [currency_id] => USD
                        )

                    [6] => stdClass Object
                        (
                            [id] => MLM1366950004
                            [status] => candidate
                            [price] => 95.24
                            [original_price] => 126.99
                            [currency_id] => USD
                        )

                    [7] => stdClass Object
                        (
                            [id] => MLM1341152907
                            [status] => candidate
                            [price] => 17.27
                            [original_price] => 22.44
                            [currency_id] => USD
                        )

                )

            [paging] => stdClass Object
                (
                    [offset] => 0
                    [limit] => 50
                    [total] => 8
                )

        )

    [httpCode] => 200
)

修改促销价格 和成本价格   其中的deal_id 获得的状态必须是 [status] => started  对于 [status] => pending和[status] => candidate 无效

$data = ['deal_id'=>'MLM1029','regular_price'=>12,'deal_price'=>10.98,'promotion_type'=>'DEAL'];
/**
  * 修改促销价格和成本价格
*/
public function addpromotion($item_id,$user_id,$data,$token){
    return $this->api()->post('/marketplace/seller-promotions/items/'.$item_id,$data,['user_id'=>$user_id,'access_token' => $token ]);
}