<?php
namespace flow;
use fast\Http;

class Yidian
{
    protected $baseUrl; // 流量平台的基础URL

    public function __construct()
    {


        $this->baseUrl = --'; // 请替换为实际的流量平台基础URL
        $this->channel_id ="--";
        $this->appSecret = '--'; // 请替换为实际的appSecret
        $this->salesMethod ="5"; //必须是字符串 销售方式: 1-零售 2-OTA 3-批发 4-分佣 5-分销 6-其他
    }

    // 获取覆盖国家列表
    public function getCountrys()
    {
        $tradeType = 'F001';
        $tradeData = [
            'salesMethod' => $this->salesMethod,
        ];
        $response = $this->sendRequest($tradeType, $tradeData);
        return $response;
    }

    // 获取商品列表
    public function getCommodities()
    {
        $tradeType = 'F002';
        $tradeData = [
            'salesMethod' => $this->salesMethod
        ];
        $response = $this->sendRequest($tradeType,  $tradeData);
        return $response;
    }

    // 获取商品价格
    public function getCommodityPrice()
    {
        $tradeType = 'F003';
        $tradeData = [
            'salesMethod' => $this->salesMethod,
        ];
        $response = $this->sendRequest($tradeType, $tradeData);
        return $response;
    }

    // 创建充值订单
    //  $iccid :充值卡号数组,不能有重复,范围:1-500个
    //  $skuId : 商品skuId
    //  $copies :份数
    public function CreateRechargeOrder($sn,$csn,$iccid=[],$skuId,$copies,$totalAmount)
    {
      // $sn=date("YmdHis").rand(1000,9999);
        $tradeType = 'F007';
        $tradeData = [
            'channelOrderId' => $sn, //渠道主订单号
            'totalAmount' => $totalAmount, //订单金额
            'orderCreateTome' => date("Y-m-d H:i:s"), //订单日期
            'subOrderList'=>[
                [
                    "channelSubOrderId"=>$csn,
                    "iccid"=> $iccid,
                    "skuId"=> $skuId,
                    "copies"=> $copies
                ]
            ]
        ];
        $response = $this->sendRequest($tradeType, $tradeData);
        return $response;
    }







///////////////////////////////////
    // 发送请求到流量平台
    private function sendRequest($tradeType,$tradeData)
    {
        $data = [
            'tradeType' => $tradeType,
            'tradeTime' => date('Y-m-d H:i:s'),
            'tradeData' => $tradeData,
        ];

        $options = [
            CURLOPT_HTTPHEADER => [
                'Content-type: application/json',
                'x-channel-id:'.$this->channel_id, // 请替换为实际的channel-id
                'x-sign-method:md5', // 签名算法
                'x-sign-value:' . $this->generateSign($data) // 签名值
            ]
        ];
        $response = Http::post($this->baseUrl,json_encode($data), $options);
        $response = json_decode($response,true);
        return $response;
    }

    // 生成签名值
    private function generateSign($data)
    {
        // 根据文档中的签名算法生成签名值
        // 这里需要添加实际的签名逻辑,包括appSecret等
        $signStr = json_encode($data);
        $sign = md5($this->appSecret . $signStr);
        return $sign;
    }
}

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部