时光

时光的时光轴

寄以时间,予以文字
telegram
github
微信公众号

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

这些天闲来无事搭建了一个小商店,在正式开始之前,如果你不介意的话可以逛逛我的两米商店🛒

自带的主题足够好看,后台功能也能满足基本的需要,但是配置商品的时候有一个回调事件的配置项在 Wiki 里面找不到相关说明,也搜不到相关教程;本着功能是有的想法,这篇文章算是一个解决问题的流水账,当然如果能对你有帮助就太好了。实在懒也可以 “花钱购买我的时间。

在前面的检索环节中,你需要对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));

这样就完成了。

祝你玩的开心。

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。