Day 0x10 - 整理解密函数与 Webhook api

0x1 前言

昨天确认接到讯息回覆了,今天来把解密函数跟 receive_msg 整理一下

0x2 整理函数

昨天有看到 receive_msg 传入并且也要求 查询讯息内容,内容也是需要解密,把 create_order 解密的那一块独立出来,建立 reply_message_decrypt 函数

// app/Http/Controllers/Controller.php
protected function reply_message_decrypt(Sinopac $sinopac, $message)
{
    $reply_nonce = $message['Nonce'] | '';
    if (!$reply_nonce) {
        $msg = 'Reply message haven\'t Nonce';
        Log::error($msg , $message);
        throw new \Exception($msg);
    }

    // 1. nonce 计算 iv
    $iv = $sinopac->calculateIv($reply_nonce);
    // 2. 计算 hash_id (AES key)
    $hash_id = $sinopac->calcHashId();
    // 3. message 解密
    $decrypt_message = $sinopac->decryptMessage($message['Message'], $hash_id, $iv);
    // 4. 验证 sign
    $sign = $sinopac->generateSign($decrypt_message, $reply_nonce, $hash_id);

    if (!($sign === $message['Sign'])) {
        return ['msg' => '验证错误,内文签章不同'];
    }

    return $decrypt_message;
}

receive_msgcreate_order 修改一下

// app/Http/Controllers/Controller.php

public function create_order(Request $request)
{
    ...
    $message = $sinopac->callApi('https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order', $data);

    $decrypt_message = $this->reply_message_decrypt($sinopac, $message);
    // 这里的 – 是 \xE2  不是 \x2D
    $description = explode(' – ', $decrypt_message['Description']);
    ...
}

public function receive_msg(Request $request)
{
    ...
    $decrypt_message = $this->reply_message_decrypt($sinopac, $message);
    Log::info('Reply message', (array) $decrypt_message);
    ...
}

喔,对了 PayToken 跟 APType 要记录下来

php .\artisan make:migration --table sale_orders alter_sale_order
// database/migrations/[datetime]_update_sale_order.php
public function up()
{
    Schema::table('sale_orders', function (Blueprint $table) {
        $table->string('pay_token', 110)->nullable()->comment('付款回应讯息 token');
        $table->string('ap_type', 30)->nullable()->comment('讯息类型');
    });
}
    
public function down()
{
    Schema::table('sale_orders', function (Blueprint $table) {
        $table->dropColumn(['pay_token', 'ap_type']);
    });
}
php .\artisan migrate

再次修改 receive_msg,把需要更新的纪录写进去

// app/Http/Controllers/Controller.php
public function receive_msg(Request $request)
{
    Log::alert('Receive message Content', $request->all());
    $PayToken = $request->get('PayToken');

    if (!$PayToken) {
        Log::alert('PayToken Not exist');
        return ['Status' => 'F'];
    }

    $sinopac = $this->initSinopac();
    $data = $sinopac->requestDataset('OrderPayQuery', $request->all());
    $message = $sinopac->callApi('https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order', $data);

    $decrypt_message = $this->reply_message_decrypt($sinopac, $message);
    Log::info('Reply message', (array) $decrypt_message);

    $record = sale_order::where('ts_no', $decrypt_message['TSResultContent']['TSNo']);
    if (!$record->count()) {
        Log::alert('Not found order!');
        return ['Status' => 'F'];
    }

    $record->update([
        'pay_token'     => $decrypt_message['PayToken'],
        'ap_type'       => $decrypt_message['TSResultContent']['APType'],
        'status'        => $decrypt_message['Status'],
        'description'   => $decrypt_message['Description']
    ]);

    return ['Status' => 'S'];
}

传送几笔订单後,等待讯息回覆
https://ithelp.ithome.com.tw/upload/images/20210926/20141805CryTZl3J9p.png

0x3 今日结语

深深地觉得,这已经当日记在写了XD,
完全没有规划的随心所欲,看到缺什麽补什麽
明天把建立订单 - 信用卡的部分完成,
还有两个礼拜,加油 :D


<<:  [Python 爬虫这样学,一定是大拇指拉!] DAY11 - HTTP / HTTPS (2)

>>:  [Day 14] 多棵决策树更厉害:随机森林 (Random forest)

Day 12 强化学习 (Reinforcement Learning)

强化学习什麽是? 简称RL,在没有以往资料的前提下,将模型放到使用环境中,透过一些操作观察环境状态,...

从 JavaScript 角度学 Python(12) - 运算子

前言 运算子是一个非常常用的方法,因此在基础观念中也是绝对闪不了的。 运算子 最基本的运算子不外乎就...

[Day8] THM Bolt

今天再来试玩一个 Try Hack Me 上面的简单题目,攻打一个 CMS (Content Man...

23.unity储存文字内容(List、foreach)

今天要用List来储存记事本内的对话资料 0.和昨天一样,先准备好对话.txt 1.写脚本,先检查有...

[Day18] TS:理解 Omit 的实作

是我们今天要聊的内容,老样的,如果你已经可以轻松看懂,欢迎直接左转去看同事 Ken 精彩的文章 —...