分享自己寫的關於順豐同城配送的擴充套件包

ycstar發表於2021-08-09

公司專案需要用到順豐外賣,所以看了下有沒有現成的擴充套件包可用,但是發現沒有,官方也沒有SDK,所以自己寫了一個包,省了不少工作量。

安裝

$ composer require ycstar/sfopenic -vvv

配置

在使用本擴充套件之前,你需要去順豐同城開放平臺註冊賬號,然後申請開發者ID,獲取相應的配置

使用

    use Ycstar\Sfopenic\Sfopenic;

    $config = [
        'host'    => 'xxxxxxxxxxxx',
        'dev_id'  => 'xxxxxxxxxxxx',
        'dev_key' => 'xxxxxxxxxxxx'
    ];

    $sfopenic = new Sfopenic($config);

預建立訂單

    $data = [
        'shop_id' => 'xxxxxxxxxxxx',
        'user_address' => "北京市海淀區學清嘉創大廈A座15層",
        'user_lng' => '116.352569',
        'user_lat' => '40.014838',
        'weight' => 100,
        'product_type' => 1,
        'pay_type' => 1,
        'is_appoint' => 0,
        'is_insured' => 0,
        'is_person_direct' => 0
    ];

    $res = $sfopenic->preCreateOrder($data);

建立訂單

    $array = [
        'shop_id' => 'xxxxxxxxxxxx',
        'shop_order_id' => 'xxxxxxxxxxxx',
        'order_source' => 'xx',
        'pay_type' => 1,
        'order_time' => time(),
        'is_appoint' => 0,
        'is_insured' => 0,
        'is_person_direct' => 0,
        'version' => 17,
        'order_sequence' => 'xx',
        'remark' => 'xx'
    ];

    $receive =[
        'user_name' => "xx",
        'user_phone' => "xxxxxxxxxx",
        'user_address' => "北京市海淀區學清嘉創大廈A座15層",
        'user_lng' => '116.352569',
        'user_lat' => '40.014838',
    ];

    $order_detail = [
        'total_price' => 1,//總金額
        'product_type' => 1, //物品型別 1:送餐 8:飲品
        'weight_gram' => 100,//物品重量
        'product_num' => 3,//物品個數
        'product_type_num' => 1,//物品種類個數
    ];

    $product_detail[]=[
        'product_name'=>'xxx',//物品名稱
        'product_num' => 1,//物品數量
    ];

    $order_detail['product_detail'] = $product_detail;

    $array['order_detail'] = $order_detail;

    $array['receive'] = $receive;

    $res = $sfopenic->createOrder($data);

取消訂單

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
        'cancel_code' => 313, //不填時預設cancel_code=313,cancel_reason=商家發起取消
        'cancel_reason' => ''
    ];

    $res = $sfopenic->cancelOrder($data);

預取消訂單

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
        'cancel_reason' => ''
    ];

    $res = $sfopenic->preCancelOrder($data);

訂單加小費

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
        'gratuity_fee' => 0
    ];

    $res = $sfopenic->addOrderGratuityFee($data);

獲取訂單加小費資訊

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->getOrderGratuityFee($data);

訂單狀態流查詢

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->listOrderFeed($data);

訂單實時資訊查詢

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->getOrderStatus($data);

催單

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->reminderOrder($data);

改單

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
        'user_name'=> '',
        'user_phone'=> '',
        'user_address'=> '',
        'lbs_type'=> 2, //1:百度座標,2:高德座標(預設值為2,下面的經緯度依賴這個座標系,不傳預設高德)
        'user_lng'=> '', //傳入使用者地址經緯度順豐側則不根據使用者地址解析
        'user_lat'=> '',
    ];

    $res = $sfopenic->changeOrder($data);

獲取配送員實時座標介面

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->riderLatestPosition($data);

獲取配送員軌跡H5

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,   //order_type=2時必傳shop_id與shop_type
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->riderViewV2($data);

訂單回撥詳情

    $data = [
        'order_id' => 'xxxxxxxxxxxx',
        'order_type' => 1 //1、順豐訂單號 2、商家訂單號
        'shop_id' => 0,
        'shop_type' => 1, //1、順豐店鋪ID 2、接入方店鋪ID
    ];

    $res = $sfopenic->getCallbackInfo($data);

獲取回撥資料及回覆內容

    $data = $sfopenic->getNotify();
    .
    .
    .
    return $sfopenic->getNotifySuccessReply();

在laravel中使用

在 Laravel 中使用也是同樣的安裝方式,

使用下面的命令來匯出配置檔案

php artisan vendor:publish --tag="ycstar-sfopenic"

配置寫在 config/sfopenic.php 中:

    return [

        'host' => env('SF_OPENIC_HOST'),

        'dev_id' => env('SF_OPENIC_DEV_ID'),

        'dev_key' => env('SF_OPENIC_DEV_KEY')

      ];

然後在 .env 中配置 SF_OPENIC_HOST, SF_OPENIC_DEV_ID, SF_OPENIC_DEV_KEY:

    SF_OPENIC_HOST = xxxxxxxxxxxx
    SF_OPENIC_DEV_ID = xxxxxxxxxxxx
    SF_OPENIC_DEV_KEY = xxxxxxxxxxxx

可以用兩種方式來獲取 Ycstar\Sfopenic\Sfopenic 例項:

  • 方法引數注入
      .
      .
      .
      public function preCreateOrder(Sfopenic $sfopenic)
      {
          $res = $sfopenic->preCreateOrder($data);
      }
      .
      .
      .
  • 服務名訪問
      .
      .
      .
      public function preCreateOrder()
      {
          $res = app('sfopenic')->preCreateOrder($data);
      }
      .
      .
      .

參考

License

MIT

本作品採用《CC 協議》,轉載必須註明作者和本文連結
To live is to change the world

相關文章