PHP对接Wish接口

25 阅读3分钟

A1

<?php

class Api {
    private static $apiToken = '';
    private static $merchantId;
    public $limit = 50;
    public $version = "v2/";
    public $baseProdPath = "https://china-merchant.wish.com/api/";
    public $baseStagePath = "https://merch.corp.contextlogic.com/api/";

    public function __construct($apiToken, $merchantId = '') {
        $this->apiToken = $apiToken;
        $this->merchantId = $merchantId;
    }

    public function getVersion() {
        return $this->version;
    }

    public function getRequestUrl() {
        return $this->baseProdPath;
    }

    public function requestHost($method, $path, array $params) {
        $params['access_token'] = $this->apiToken;
        $url = $this->getRequestUrl() . $this->getVersion() . $path;
        $curl = curl_init();
        $options = array(
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem',
            CURLOPT_USERAGENT => 'wish-php-sdk',
            CURLOPT_HEADER => 'true'
        );

        if ($method === "GET") {
            $url = $url . "?" . http_build_query($params);
        } else {
            $options[CURLOPT_POSTFIELDS] = $params;
        }

        $options[CURLOPT_URL] = $url;
        curl_setopt_array($curl, $options);

        $i = 0;
        do {
            $result = curl_exec($curl);
            if ($result || $i++ > 2) {
                break;
            }
        } while (true);

        curl_close($curl);
        return $result;
    }

    public function getAllChangedOrdersSince($page = 0, $time = '') {
        $params = array();
        if ($time) {
            $params['since'] = $time;
        }

        $params['limit'] = $this->limit;

        if ($page > 0) {
            $params['start'] = $page * $this->limit;
        } else {
            $params['start'] = $page;
        }

        return $this->requestHost('GET', 'order/multi-get', $params);
    }

    public function getAllUnfulfilledOrdersSince($page = 0, $time = '') {
        $params = array();
        if ($time) {
            $params['since'] = $time;
        }

        $params['limit'] = $this->limit;

        if ($page > 0) {
            $params['start'] = $page * $this->limit;
        } else {
            $params['start'] = $page;
        }

        return $this->requestHost('GET', 'order/get-fulfill', $params);
    }

    public function fulfillOrder($orderId, $provider, $trackNumber, $memo = '') {
        $params = array(
            'tracking_provider' => $provider,
            'tracking_number' => $trackNumber,
            'ship_note' => $memo,
            'id' => $orderId,
            'origin_country_code' => 'CN',
        );

        return $this->requestHost('POST', 'order/fulfill-one', $params);
    }
}

?>

A2

class Api
{
    private $api_key = '***********32位**********';
    private $api_url = 'https://www.wishpost.cn/api/v2/create_order';
    private $order_status_url = 'https://www.wishpost.cn/api/v3/order_status';

    public function Create_an_order($trades_id, $otype, $has_battery)
    {
        global $db, $ass;
        $receive = $receive;
        $account = $account;
        $address = $address;
        $orders = $orders;
        $goods = $goods;

        if ($otype == '5001-1' || $otype == '5002-1') {
            $doorpickup = 0;
        } else {
            $doorpickup = 1;
        }


        $Parameters = array(
            'access_token' => $account['lgs_access_token'],//
            'api_key' => $this->api_key,//
            'order' => array(
                // 'bid' => 200 ,//用户提供的单次提交唯一标识
                'guid' => 200,//用户提供的运单唯一标识,将跟随运单号返回/
                'otype' => $otype,// 物流方式/
                'from' => $receive['address_name'],//寄件人姓名(留空将使用地址簿默认信息)/
                'sender_province' => $receive['province_name'],//寄件人省名(留空将使用地址簿默认信息)/
                'sender_city' => $receive['city_name'],//  寄件人城市名(留空将使用地址簿默认信息)/
                'sender_addres' => $receive['address'],//寄件人地址(留空将使用地址簿默认信息)/
                'sender_phone' => $receive['phone'],//寄件人电话(留空将使用地址簿默认信息. 号码可以以一个+开头,可以以-连接或以()围住. 不可以包含其他符号或其他语言字符)/
                'sender_zipcode' => $receive['postcode'],//寄件人邮编(留空将使用地址簿默认信息,地址簿寄件人邮编为空将使用310000)/
                'pickup_from_local' => $receive['address_name'],//揽收人姓名(当地国语言),Sinotrans、IB、EPC、YunExpress和Wiseexpress必填
                'pickup_phone' => $receive['phone'],//揽收人电话,Sinotrans、IB、EPC、YunExpress和Wiseexpress必填
                'pickup_zip_code' => $receive['postcode'],//揽收人邮编
                'pickup_addres_local' => $receive['address'],//揽收人地址(当地国语言),Sinotrans、IB、EPC、YunExpress和Wiseexpress必填
                'pickup_province_local' => $receive['province_name'],//揽收人省名(当地国语言),Sinotrans、IB、EPC、YunExpress和Wiseexpress必填
                'pickup_city_local' => $receive['city_name'],//揽收人城市名(当地国语言),Sinotrans、IB、EPC、YunExpress和Wiseexpress必填
                'pickup_from' => 'xxxxx',//揽收人姓名英文
                'pickup_addres' => 'xxxx',//揽收人地址英文
                'pickup_province' => 'Guangdong Province',//揽收人省名英文
                'pickup_city' => 'Shenzhen',//揽收人城市名英文
                'pickup_country_local' => "中国",//揽收人国家(当地国语言)
                'pickup_country' => 'China',//揽收人国家英文
                'pickup_country_code' => 'CHN',//揽收人国家代码
                'to' => $address['ShipName'],//收件人姓名(必须是英文),Wishpost EPC 选填/
                'recipient_country' => $address['ShipCountryEn'],//收件人国家(英文),Wishpost EPC 选填/
                'recipient_country_short' => $address['ShipCountry'],//收件人国家简码(如:US,GB,UK),Wishpost EPC/
                'recipient_province' => $address['ShipRegion'],//收件人州名(英文),Wishpost EPC 选填/
                'recipient_city' => $address['ShipCity'],//收件人城市名(英文),Wishpost EPC 选填/
                'recipient_addres' => $address['ShipLine'],//收件人地址(英文),Wishpost EPC 选填/
                'recipient_postcode' => $address['ShipPostalCode'],//收件人邮编,Wishpost EPC 选填/
                'recipient_phone' => $address['ShipPhone'],//收件人电话(号码可以以一个+开头,可以以-连接或以()围住。不可以包含其他符号或其他语言字符),Wishpost EPC 选填/
                'to_local' => $address['ShipName'],//收件人姓名(到达国语言, 留空将使用英文信息)/
                'recipient_country_local' => $address['ShipCountryEn'],//收件人国家(到达国语言, 留空将使用英文信息)/
                'recipient_province_local' => $address['ShipRegion'],//收件人州名(到达国语言, 留空将使用英文信息)/
                'recipient_city_local' => $address['ShipCity'],//收件人城市名(到达国语言, 留空将使用英文信息)/
                'recipient_addres_local' => $address['ShipLine'],//收件人地址(到达国语言, 留空将使用英文信息)/
                'type_no' => 3,//内件类型代码 1=礼品 2=文件 3=商品货样 4=其他/
                'from_country' => 'China',//货物原产国(英文)/
                'content' => $goods['goods_declaredname'],//内件物品的详细名称(英文), 必须包含至少一个英文字母/
                //'content_chinese' => $goods['goods_declaredcnname'].$goods['goods_sku'] ,//内件物品的详细中文名称(必填,必须包含中文)/
                'content_chinese' => $goods['goods_sku'] . $goods['goods_declaredcnname'],//内件物品的详细中文名称(必填,必须包含中文)/
                'num' => $orders['QuantityOrdered'],//内件数量/
                'unit_measurement' => 'item',//英文计量单位/
                'unit_measurement_chinese' => "个",//中文计量单位/
                'weight' => ($orders['QuantityOrdered'] * $goods['weight']) / 1000,//货品重量,千克(三位小数)/
                'single_price' => 17,//货品申报价值(两位小数)/
                'trande_no' => $order_no,//在Wish网站的交易号(即Order ID),所有渠道必填。该字段必须是英文字母、数字、或英文逗号字符(分割多个交易单号时使用)中间不得有空格。如不属于Wish平台交易,请填写默认值nonWishOrder,则该物流订单的跟踪号无法回填到Wish商户后台;如属于Wish平台交易号,请正确填写Wish商户后台显示的24位(Order ID)。该字段支持单个交易号,如:**************;也支持最多20个交易单号,如::**************;,:**************;。/
                'trade_amount' => $address['order_money'],//WISH网站的交易金额,单位为美元, 包含物品价值和物流费用/
                'user_desc' => $goods['goods_sku'],//商户自定义信息,例如商户订单号, 商品编号,库位号等/
                'receive_from' => $receive['address_name'],//揽收信息联系人(必须中文)
                'receive_province' => $receive['province_name'],//揽收信息省名(必须中文)
                'receive_city' => $receive['city_name'],//揽收信息城市名(必须中文)
                'receive_addres' => $receive['address'],//揽收信息(必须中文)
                'receive_phone' => $receive['mobile'],//揽收信息电话
                'warehouse_code' => 3,//分仓代码(中邮小包、IB必填) 以下为中邮小包平邮、挂号支持仓库:
                'doorpickup' => $doorpickup,//是否上门揽收   0=自送  1=上门揽收/
                'has_battery' => $has_battery,//是否带电, 必填 0=不带电, 1=带电
                'product_url' => 'https://www.wish.com/c/' . $Order_no,//产品网页链接(必填)
            )
        );


        $result = json_decode(json_encode(simplexml_load_string($this->wish_curl($Parameters, $this->api_url))), true);


        if (isset($result['status']) && $result['status'] == 0) {

            if (is_array($result['barcode'])) {

                $tracking_data = $this->get_order_status($account['lgs_access_token'], $result['wish_standard_tracking_id']);
                //print_r($tracking_data);
                if (strlen($tracking_data['orders']['0']['logistics_order_code']) > 3) {
                    $tracking = array(
                        "status" => $tracking_data['code'],
                        "barcode" => $tracking_data['orders']['0']['logistics_order_code'],

                    );
                } else {
                    $tracking = array(
                        "status" => $tracking_data['orders']['0']['state_code'],
                        "barcode" => $tracking_data['orders']['0']['logistics_order_code'],
                        "error_message" => $tracking_data['orders']['0']['message']
                    );
                }
                return $tracking;
            } else {

                return $result;
            }


        } else {
            return array(
                "status" => 1,
                "error_message" => $result['error_message']
            );
        }


    }


    public function get_order_status($acces_token, $wish_standard_tracking_ids)
    {

        $Parameters = array(
            'access_token' => $acces_token,//
            'wish_standard_tracking_ids' => [$wish_standard_tracking_ids],//
        );

        return json_decode($this->wish_order_status_cur($Parameters, $this->order_status_url), true);

    }


    public function arrayToXml($data)
    {
        if (!is_array($data) || count($data) <= 0) {
            return false;
        }
        $xml = "<xml>";
        foreach ($data as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">";
                foreach ($val as $key2 => $val2) {
                    if (is_numeric($key2)) {
                        $xml .= "<" . $key2 . ">" . $val2 . "</" . $key2 . ">";
                    } else {
                        $xml .= "<" . $key2 . "><![CDATA[" . $val2 . "]]></" . $key2 . ">";
                    }

                }
                $xml .= "</" . $key . ">";
            } else {
                if (is_numeric($val)) {
                    $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
                } else {
                    $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                }
            }

        }
        $xml .= "</xml>";
        return $xml;
    }

    public function wish_curl($Parameters, $url)
    {
        $header[] = "Content-Type: text/xml; charset=utf-8";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->arrayToXml($Parameters));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

    public function wish_order_status_cur($Parameters, $url)
    {
        $header[] = "Content-Type: text/xml; charset=utf-8";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Parameters));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

}