公司项目需要用到顺丰外卖,所以看了下有没有现成的扩展包可用,但是发现没有,官方也没有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 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| 'cancel_code' => 313, |
| 'cancel_reason' => '' |
| ]; |
| |
| $res = $sfopenic->cancelOrder($data); |
预取消订单
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| 'cancel_reason' => '' |
| ]; |
| |
| $res = $sfopenic->preCancelOrder($data); |
订单加小费
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| 'gratuity_fee' => 0 |
| ]; |
| |
| $res = $sfopenic->addOrderGratuityFee($data); |
获取订单加小费信息
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->getOrderGratuityFee($data); |
订单状态流查询
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->listOrderFeed($data); |
订单实时信息查询
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->getOrderStatus($data); |
催单
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->reminderOrder($data); |
改单
| $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); |
获取配送员实时坐标接口
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->riderLatestPosition($data); |
获取配送员轨迹H5
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $res = $sfopenic->riderViewV2($data); |
订单回调详情
| $data = [ |
| 'order_id' => 'xxxxxxxxxxxx', |
| 'order_type' => 1 |
| 'shop_id' => 0, |
| 'shop_type' => 1, |
| ]; |
| |
| $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