分享自己写的关于顺丰同城配送的扩展包
发表于|更新于
公司项目需要用到顺丰外卖,所以看了下有没有现成的扩展包可用,但是发现没有,官方也没有SDK,所以自己写了一个包,省了不少工作量。
安装
1
| $ composer require ycstar/sfopenic -vvv
|
配置
在使用本扩展之前,你需要去顺丰同城开放平台注册账号,然后申请开发者ID,获取相应的配置
使用
1 2 3 4 5 6 7 8 9
| use Ycstar\Sfopenic\Sfopenic;
$config = [ 'host' => 'xxxxxxxxxxxx', 'dev_id' => 'xxxxxxxxxxxx', 'dev_key' => 'xxxxxxxxxxxx' ];
$sfopenic = new Sfopenic($config);
|
订单管理
预创建订单(店铺)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $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);
|
预创建订单(企业)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| $data = [ 'company_id' => 'xxxxxxxxxxxx', 'settlement_type' => 0, 'city_name' => "北京", 'account_phone' => 'xxxxxxxxxxxx', 'is_insured' => 0, 'is_appoint' => 0, ];
$sender =[ 'sender_name' => "xx", 'sender_phone' => "xxxxxxxxxx", 'sender_address' => "北京市海淀区学清嘉创大厦A座15层", 'sender_lng' => '116.352569', 'sender_lat' => '40.014838', ];
$receiver =[ 'receiver_name' => "xx", 'receiver_phone' => "xxxxxxxxxx", 'receiver_address' => "北京市海淀区学清嘉创大厦A座15层", 'receiver_lng' => '116.352569', 'receiver_lat' => '40.014838', ];
$orderDetail = [ 'total_price' => 1, 'product_type' => 1, 'weight_gram' => 100, ];
$data['sender'] = $sender; $data['receiver'] = $receiver; $data['order_detail'] = $orderDetail;
$res = $sfopenic->preCreateOrder4C($data);
|
创建订单(店铺)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| $data = [ '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', ];
$orderDetail = [ 'total_price' => 1, 'product_type' => 1, 'weight_gram' => 100, 'product_num' => 3, 'product_type_num' => 1, ];
$productDetail = [ [ 'product_name' => 'xxx', 'product_num' => 1, ] ];
$orderDetail['product_detail'] = $productDetail; $data['order_detail'] = $orderDetail; $data['receive'] = $receive;
$res = $sfopenic->createOrder($data);
|
创建订单(企业)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| $data = [ 'user_order_id' => 'xxxxxxxxxxxx', 'company_id' => 'xxxxxxxxxxxx', 'settlement_type' => 0, 'city_name' => "北京", 'account_phone' => 'xxxxxxxxxxxx', 'is_insured' => 0, 'is_appoint' => 0, ];
$sender =[ 'sender_name' => "xx", 'sender_phone' => "xxxxxxxxxx", 'sender_address' => "北京市海淀区学清嘉创大厦A座15层", 'sender_lng' => '116.352569', 'sender_lat' => '40.014838', ];
$receiver =[ 'receiver_name' => "xx", 'receiver_phone' => "xxxxxxxxxx", 'receiver_address' => "北京市海淀区学清嘉创大厦A座15层", 'receiver_lng' => '116.352569', 'receiver_lat' => '40.014838', ];
$orderDetail = [ 'total_price' => 1, 'product_type' => 1, 'weight_gram' => 100, ];
$data['sender'] = $sender; $data['receiver'] = $receiver; $data['order_detail'] = $orderDetail;
$res = $sfopenic->createOrder4C($data);
|
取消订单
1 2 3 4 5 6 7 8 9 10
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, 'cancel_code' => 313, 'cancel_reason' => '' ];
$res = $sfopenic->cancelOrder($data);
|
预取消订单
1 2 3 4 5 6 7 8 9
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, 'cancel_reason' => '' ];
$res = $sfopenic->preCancelOrder($data);
|
订单加小费
1 2 3 4 5 6 7 8 9
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, 'gratuity_fee' => 0 ];
$res = $sfopenic->addOrderGratuityFee($data);
|
获取订单加小费信息
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->getOrderGratuityFee($data);
|
订单状态流查询
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->listOrderFeed($data);
|
订单实时信息查询
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->getOrderStatus($data);
|
催单
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->reminderOrder($data);
|
改单
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, 'user_name'=> '', 'user_phone'=> '', 'user_address'=> '', 'lbs_type'=> 2, 'user_lng'=> '', 'user_lat'=> '', ];
$res = $sfopenic->changeOrder($data);
|
获取配送员实时坐标接口
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->riderLatestPosition($data);
|
获取配送员轨迹H5
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->riderViewV2($data);
|
商家告知餐品制作完成接口
1 2 3 4 5 6 7 8 9
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, 'notice_ready_time' => 'xxxxxxxxxxxx' ];
$res = $sfopenic->notifyProductReady($data);
|
店铺管理
获取店铺账户余额
1 2 3 4 5 6
| $data = [ 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->getShopAccountBalance($data);
|
获取企业信息
1 2 3 4 5
| $data = [ 'company_id' => 0, ];
$res = $sfopenic->getCompanyInfo($data);
|
获取店铺信息
1 2 3 4 5 6
| $data = [ 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->getShopInfo($data);
|
回调相关
1 2 3 4 5 6 7 8
| $data = [ 'order_id' => 'xxxxxxxxxxxx', 'order_type' => 1 'shop_id' => 0, 'shop_type' => 1, ];
$res = $sfopenic->getCallbackInfo($data);
|
获取回调数据及回复内容
1 2 3 4 5
| $data = $sfopenic->getNotify(); . . . return $sfopenic->getNotifySuccessReply();
|
在laravel中使用
在 Laravel 中使用也是同样的安装方式,
使用下面的命令来导出配置文件
1
| php artisan vendor:publish --tag="ycstar-sfopenic"
|
配置写在 config/sfopenic.php 中:
1 2 3 4 5 6 7 8 9
| 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:
1 2 3
| SF_OPENIC_HOST = xxxxxxxxxxxx SF_OPENIC_DEV_ID = xxxxxxxxxxxx SF_OPENIC_DEV_KEY = xxxxxxxxxxxx
|
可以用两种方式来获取 Ycstar\Sfopenic\Sfopenic 实例:
- 方法参数注入
1 2 3 4 5 6 7 8 9 10
| . . . public function preCreateOrder(Sfopenic $sfopenic) { $res = $sfopenic->preCreateOrder($data); } . . .
|
- 服务名访问
1 2 3 4 5 6 7 8 9 10
| . . . public function preCreateOrder() { $res = app('sfopenic')->preCreateOrder($data); } . . .
|
参考