时光

时光的时光轴

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

Configure callback events on the Unicorn Number Card.

These days, I have set up a small store when I have nothing to do. Before officially starting, if you don't mind, you can visit my 2m Store🛒.

The built-in theme is good-looking enough, and the backend functions can also meet basic needs. However, when configuring the goods, there is a configuration item for "callback events" that cannot be found in the Wiki or any related tutorials. In the spirit of having the functionality, this article can be considered a "diary" of solving the problem. Of course, it would be great if it can be helpful to you. If you're really lazy, you can also "spend money to buy my time."

In the previous search phase, you need to have some basic understanding of PHP and Laravel. If you don't have it, please directly skip to the "Some Examples" section. All the code in this article comes from the dujiaoka repository and is open source under the MIT license.

Let's get started.

Find the Function Definition#

You can quickly search for a comment about "callback events" in the code, located in app/Service/OrderProcessService.php.

iShot_2023-02-21_21.35.48.png

Follow along and find the definition of the ApiHook class, located in app/Jobs/ApiHook.php, and then look at the constructor of the class.

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

Here, the $order variable is passed, and a service is imported. The dispatch afterwards is a queue system provided by Laravel, which allows callback events to be added to the queue for execution.

Now let's take a look at the goodsService in app/Service/GoodsService.php, which contains some basic functions for handling goods. Then, let's directly look at the handle function in the event.

public function handle()
    {
        $goodInfo = $this->goodsService->detail($this->order->goods_id);
        // Check if there is a configured payment callback
        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);
    }

You will find that the "callback event" is actually straightforward. It simply sends the processed product information to the "callback event" you provided, which can be a URL.

What is Sent#

Now let's first figure out what is sent to us when the callback occurs.
According to 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

        ];

It sends the order title, order number, buyer's email, actual price paid, order information, product ID, and product title.

The difference between the order title and product title that I have found so far is that the order title includes the quantity of the order, usually in the format of product title x quantity.

The order information is the other input field configuration in your product. According to app/Service/OrderService.php:185, the output format is:

key1:value1
key2:value2
...

With this data, you can implement some logic to fill in some account information on the page, such as automatic recharge, or automatically issue coupons.

Based on the environment parameters, we can know some other information:

$opts = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-type: application/json',
                'content' => json_encode($postdata,JSON_UNESCAPED_UNICODE)
            ]
        ];
  • The request method is POST.
  • The content of the request is JSON encoded.
  • Special characters such as Chinese characters in the JSON will not be Unicode encoded.

With the above information, I believe that those of you with a certain foundation can already try to implement some logic to utilize the callback event. But let's go further and give some examples.

Some Examples#

The following examples are made up by me. I do not guarantee the feasibility of any functionality or the security of any code.

Receive the Passed Data and Store it in an Array#

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

Store the Passed Custom Fields in an Array#

$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;
}

Pretend to Top Up for a User#

Assuming you have a cool API that can directly top up R coins for users, and you have a custom field on the page called rr. And your product name is Top Up 10 R Coins, then you can do it like this:

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

This way, it's done.

Have fun!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.