分享一個順豐同城配送的擴展包并在laravel中使用

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

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

安裝

$ composer require ycstar/sfopenic -vvv

配置

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

使用

    use YcstarSfopenicSfopenic;      $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 中使用也是同樣的安裝方式,【推薦:《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

可以用兩種方式來獲取 YcstarSfopenicSfopenic 實例:

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

相關推薦:laravel教程? ? ? ?

以上就是分享一個順豐同城配送的擴展包并在

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享