在独角数卡上配置回调事件

本文介绍了在Laravel项目中如何查找和理解回调事件,包括在OrderProcessService和ApiHook类中的操作,以及如何通过队列系统发送包含订单信息和商品数据的POST请求到目标地址。作者提供了示例代码,展示了如何接收和处理从回调事件传递的数据,如自动充值和优惠券发放。
摘要由CSDN通过智能技术生成

在前面的检索环节中,你需要对PHPLaravel有一些基础了解,如果你没有的话就请直接跳到 **“一些例程”** 章节。本文所有代码均来自dujiaoka仓库,使用MIT协议开源。

那我们开始吧。

找到功能定义#

你可以很快在代码中搜索到一条回调事件的注释,位置在app/Service/OrderProcessService.php

iShot_2023-02-21_21.35.48.png

跟着走找到ApiHook类的定义,位置在app/Jobs/ApiHook.php,然后跟着看类的构造函数。

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->goodsService = app('Service\GoodsService');
    }

这里传递了$order变量,然后引入了一个服务。后面的dispatch是 Laravel 提供的队列系统,使得回调事件可以加入队列来执行。

那我们稍微看一下goodsService,在app/Service/GoodsService.php,是一些商品处理的基本函数,那就直接看事件里的handle

public function handle()
    {
        $goodInfo = $this->goodsService->detail($this->order->goods_id);
        // 判断是否有配置支付回调
        if(empty($goodInfo->api_hook)){
            return;
        }
        $postdata = [
            'title' => $this->order->title,
            'order_sn' => $this->order->order_sn,
            'email' => $this->order->email,
            'actual_price' => $this->order->actual_price,
            'order_info' => $this->order->info,
            'good_id' => $goodInfo->id,
            'gd_name' => $goodInfo->gd_name

        ];

        
        $opts = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-type: application/json',
                'content' => json_encode($postdata,JSON_UNESCAPED_UNICODE)
            ]
        ];
        $context  = stream_context_create($opts);
        file_get_contents($goodInfo->api_hook, false, $context);
    }

你会发现其实回调事件没什么玄乎,就是将处理好的商品信息请求到你填写的回调事件,可以是一个 URL。

发送了什么#

那我们要先搞清楚,在回调的时候,系统向我们的目标地址发送了什么。
根据postData

$postdata = [
            'title' => $this->order->title,
            'order_sn' => $this->order->order_sn,
            'email' => $this->order->email,
            'actual_price' => $this->order->actual_price,
            'order_info' => $this->order->info,
            'good_id' => $goodInfo->id,
            'gd_name' => $goodInfo->gd_name

        ];

发送了,订单标题订单编号购买者的邮箱实际支付的价格订单信息商品ID商品标题

这里的订单标题商品标题目前我发现的区别就是订单标题会带下单的数量,格式一般为商品标题 x 数量

这里的订单信息是你商品里的其他输入框配置,根据app/Service/OrderService.php:185可知,输出格式为

key1:value1
key2:value2
...

通过这里的数据,可以实现在页面上填写一些账号信息,实现自动充值之类的,或者自动下发优惠券。

根据环境参数,我们可以得知其他几个信息:

$opts = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-type: application/json',
                'content' => json_encode($postdata,JSON_UNESCAPED_UNICODE)
            ]
        ];
  • 请求方式为 POST
  • 请求的内容经过 JSON 编码
  • JSON 中的中文等特殊字符不会被 Unicode 编码

根据上述信息,我相信有一定基础的你已经可以试着实现一些逻辑来利用回调事件了,不过让我们再进一步,举点例子。

一些例程#

以下例程都是我瞎编的,不保证任何功能可行性,不保证任何代码安全。

接收传递来的数据并存入数组#

<?php
try {
	$data = json_decode(file_get_contents('php://input'), true);
}
catch (JsonException $e) {
	http_response_code(400);
	exit("Not valid data.");
}

将传递来的自定义字段存入数组#

$customs = [];
$custom_lines = explode(PHP_EOL, $data['order_info']);
array_pop($custom_lines);
foreach ($custom_lines as $line) {
	list($key, $value) = explode(":", $line);
	$customs[$key] = $value;
}

假装为用户充个值#

假设你有一个很酷的 API,能直接为用户充值 R 币,你在页面的自定义字段为rr。并且你的商品名称是代充10R币,那么你可以这样做:

$base = 10;
$num = intval(explode(" x ",$data['title']));
file_get_contents("你很酷的API?rr=".$customs['rr']."&num=".($base*$num));

这样就完成了。

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

资源猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值