How to recharge phone bills in php

藏色散人
Release: 2023-03-17 07:30:01
Original
1810 people have browsed it

php method to implement phone recharge: 1. Open the phone recharge interface; 2. Introduce the encapsulated code class; 3. Configure the basic information of the interface; 4. Submit the phone recharge order; 5. Push the status information to Corresponding URL; 6. Check whether recharge is supported based on the mobile phone and denomination; 7. Perform business logic processing through "if ($local_sign == $sign) {...}".

How to recharge phone bills in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to recharge phone bills in php?

Example of aggregated phone bill recharge interface based on PHP

1. Interface application activation

This code is a phone bill recharge function based on the phone bill recharge API based on aggregated data. Before use, you need to:

  • Apply for opening the call charge interface service through https://www.juhe.cn/docs/api/id/85?s=cpphpcn.

  • It can only be used officially after signing a relevant service contract with the aggregation. In the early stage, you can also apply to open a test environment and conduct docking tests.

  • For detailed interface description, please refer to the aggregation official documentation.

2. Interface usage

2.1. Introduce encapsulated code classes

include "JuheHuaFei.class.php";
Copy after login

2.2. Configure some necessary parameters

// 接口基本信息配置
$env = 1; // 接口环境类型,1:正式环境接口 2:测试环境接口
$appKey = 'b842820xxxxxxxxxxxxxxxxxx'; //从聚合申请的话费充值接口key
$openId = 'JHb0d92d94ce6axxxxxxxxxxx'; //注册聚合账号就会分配的openid,在个人中心可以查看
// 初始化
$juheHuaFei = new JuheHuaFei($appKey, $openId, $env);
Copy after login

2.3. Submit phone recharge order

// 提交话费充值订单
$orderId = '111111111'; //自己定义一个订单号,需要保证唯一
$mobile = '189xxxxxxxx'; // 需要充值的手机号码
$perValue = '1'; // 话费面值,可以选择的面额1、2、5、10、20、30、50、100、300
$submitOrderResult = $juheHuaFei->submitOrder($mobile, $perValue, $orderId);
if ($submitOrderResult) {
    if ($submitOrderResult['error_code'] == '0') {
        // 订单提交成功,根据实际业务逻辑进行处理
        echo "订单提交成功,订单号:" . $submitOrderResult['result']['sporder_id'];
        print_r($submitOrderResult);
    } else {
        // 提交返回码error_code非正常状态,依据官方文档错误码说明,进行逻辑处理
        // 比如:10014,系统异常 / 208516,重复的订单号 等,需要进行二次查询/人工确认处理,不要直接失败处理,避免造成不必要的损失
        print_r($submitOrderResult);
    }
} else {
    // 可能网络异常等问题,未获得正确响应结果,建议进行二次查单/人工确认,不要直接失败处理,避免造成不必要的损失
    // 依据自己的业务逻辑进行处理
    echo "请求异常,请确认";
}
Copy after login

Request result:

Array
(
    [reason] => 订单提交成功,等待充值
    [result] => Array
        (
            [cardid] => 10423
            [cardnum] => 1
            [ordercash] => 1.06
            [cardname] => 江苏电信话费1元
            [sporder_id] => J201125162114667xxxxxxxx
            [uorderid] => 111111111
            [game_userid] => 189xxxxxxxx
            [game_state] => 0
        )
    [error_code] => 0
)
Copy after login

2.4. Order status query

In addition to actively querying the order status, you can also provide status callback notifications to the aggregation URL, if the order status changes, the aggregation will actively push the status information to the corresponding URL.

// 话费订单充值状态查询
$orderId = '111111111'; // 需要查询的订单号,即提交订单时传递的orderId
$orderStatusResult = $juheHuaFei->queryOrderStatus($orderId);
if ($orderStatusResult) {
    // 打印返回结果
    print_r($orderStatusResult);
    // 根据实际业务逻辑进行处理
    if ($orderStatusResult['error_code'] == '0') {
        //查询成功
        if ($orderStatusResult['result']['game_state'] == '1') {
            // 订单充值成功了
            echo "订单充值成功";
        } elseif ($orderStatusResult['result']['game_state'] == '9') {
            // 订单充值失败
            echo "订单充值失败";
        } elseif ($orderStatusResult['result']['game_state'] == '0') {
            // 订单充值中
            echo "订单充值中";
        } elseif ($orderStatusResult['result']['game_state'] == '-1') {
            //订单受理失败,可能是如运营商维护、账户余额不足等情况
            echo "订单受理失败";
        }
    } else {
        //查询状态失败,可能订单号不存在等情况
        echo "查询失败:" . $orderStatusResult['reason'] . "(" . $orderStatusResult['error_code'] . ")";
    }
} else {
    // 可能网络异常等问题,未获得正确响应结果,建议进行二次查询
    // 依据自己的业务逻辑进行处理
    echo "请求异常,请确认";
}
Copy after login

Return results:

Array
(
    [reason] => 查询成功
    [result] => Array
        (
            [uordercash] => 1.060
            [sporder_id] => J2011251629516xxxxxxxxxx
            [game_state] => 9
        )
    [error_code] => 0
)
Copy after login

2.5. Check whether recharge is supported based on the mobile phone and denomination

Mainly determine whether recharge is supported based on the number segment. This function may not be used in actual business Small interface.

// 根据手机号码及面额查询是否支持充值
$mobile = '1342966xxxx'; // 手机号码
$perValue = '10'; // 话费面值
$telCheckResult = $juheHuaFei->telCheck($mobile, $perValue);
if ($telCheckResult) {
    if($telCheckResult['error_code'] == '0'){
        //说明支持充值,可以继续充值操作,以下可以根据实际需求修改
        echo "OK";
    }else{
        //暂不支持充值,以下可以根据实际需求修改
        echo "对不起,该面额暂不支持充值";
    }
} else {
    // 可能网络异常等问题,未获得正确响应结果,建议进行二次查询
    // 依据自己的业务逻辑进行处理
    echo "请求异常,请确认";
}
Copy after login

2.6. Obtain product information based on mobile phone and denomination

This small interface does not need to be used in actual business.

// 根据手机号码和面额获取商品信息
$mobile = '1342966xxxx'; // 手机号码
$perValue = '10'; // 话费面值
$telQueryResult = $juheHuaFei->telQuery($mobile, $perValue);
if ($telQueryResult) {
    if($telQueryResult['error_code'] == '0'){
        // 查询成功,可以根据实际逻辑修改
        print_r($telQueryResult);
    }else{
        // 查询失败,可以根据实际逻辑修改
        print_r($telQueryResult);
    }
} else {
    // 可能网络异常等问题,未获得正确响应结果,建议进行二次查询
    // 依据自己的业务逻辑进行处理
    echo "请求异常,请确认";
}
Copy after login

Return result:

Array
(
    [reason] => 查询成功
    [result] => Array
        (
            [cardid] => 10880
            [cardname] => 浙江移动话费10元
            [inprice] => 10.2
            [game_area] => 浙江杭州移动
        )
    [error_code] => 0
)
Copy after login

2.7. Order status notification

Push URL address: Provide it to the aggregation for configuration (for more security, you can also push the aggregation to the server IP is whitelisted)

Push method: POST

Push parameters:

How to recharge phone bills in php

PHP receives asynchronous notification (callback) reference Code:

/**
 * 接受话费\加油卡\流量充值业务 异步通知参数 参考示例
 */
$appkey = "b842820xxxxxxxxxxxxxxxxxx"; //您申请的数据的APIKey
 
$sporder_id = addslashes($_POST['sporder_id']); //聚合订单号
$orderid = addslashes($_POST['orderid']); //商户的单号
$sta = addslashes($_POST['sta']); //充值状态
$sign = addslashes($_POST['sign']); //校验值
 
$local_sign = md5($appkey.$sporder_id.$orderid); //本地sign校验值
 
if ($local_sign == $sign) {
    if ($sta == '1') {
        //充值成功,根据自身业务逻辑进行后续处理
    } elseif ($sta =='9') {
        //充值失败,根据自身业务逻辑进行后续处理
    }
}
Copy after login

2.8, JuheHuaFei.class.php

JuheHuaFei.class.php Complete code

<?php
//----------------------------------
// 聚合数据-手机话费充值API调用类--示例代码
// 官方接口文档:https://www.juhe.cn/docs/api/id/85
//----------------------------------
class JuheHuaFei
{
    private $appkey;
    private $openid;
    // 提交订单接口URL
    private $submitUrl;
    // 订单状态查询接口URL
    private $orderStatusUrl;
    // 检测手机号码是否能充值URL
    private $telCheckUrl;
    // 根据手机号和面值查询商品URL
    private $telQueryUrl;
    /**
     * JuheHuaFei constructor.
     * @param [string] $appkey [接口密钥]
     * @param [string] $openid [账号openid]
     * @param [int] [$env 接口环境类型 1:正式环境 2:测试环境]
     */
    public function __construct($appkey, $openid, $env = 1)
    {
        $this->appkey = $appkey; // 申请到的话费接口请求key
        $this->openid = $openid; // OpenID在聚合个人中心查询
        if ($env == 1) {
            // 正式环境,接口地址
            $this->submitUrl = &#39;http://op.juhe.cn/ofpay/mobile/onlineorder&#39;; // 提交订单接口URL
            $this->orderStatusUrl = &#39;http://op.juhe.cn/ofpay/mobile/ordersta&#39;; // 订单状态查询接口URL
            $this->telCheckUrl = &#39;http://op.juhe.cn/ofpay/mobile/telcheck&#39;; // 检测手机号码是否能充值URL
            $this->telQueryUrl = &#39;http://op.juhe.cn/ofpay/mobile/telquery&#39;; // 根据手机号和面值查询商品URL
        } else {
            // 测试环境,接口地址
            $this->submitUrl = &#39;http://test-v.juhe.cn/ofpay/mobile/onlineorder&#39;; // 提交订单接口URL
            $this->orderStatusUrl = &#39;http://test-v.juhe.cn/ofpay/mobile/ordersta&#39;; // 订单状态查询接口URL
            $this->telCheckUrl = &#39;http://test-v.juhe.cn/ofpay/mobile/telcheck&#39;; // 检测手机号码是否能充值URL
            $this->telQueryUrl = &#39;http://test-v.juhe.cn/ofpay/mobile/telquery&#39;; // 根据手机号和面值查询商品URL
        }
    }
    /**
     * 提交话费充值订单
     * @param  [string] $mobile   [手机号码]
     * @param  [int] $pervalue [充值面额]
     * @param  [string] $orderid  [自定义单号]
     * @return  [array]
     */
    public function submitOrder($mobile, $pervalue, $orderid)
    {
        $sign = md5($this->openid . $this->appkey . $mobile . $pervalue . $orderid);// 校验值计算
        $params = array(
            &#39;key&#39; => $this->appkey,
            &#39;phoneno&#39; => $mobile,
            &#39;cardnum&#39; => $pervalue,
            &#39;orderid&#39; => $orderid,
            &#39;sign&#39; => $sign
        );
        $content = $this->juheHttpRequest($this->submitUrl, $params, 1);
        return $this->_returnArray($content);
    }
    /**
     * 查询订单的充值状态
     * @param  [string] $orderid [自定义单号]
     * @return  [array]
     */
    public function queryOrderStatus($orderid)
    {
        $params = &#39;key=&#39; . $this->appkey . &#39;&orderid=&#39; . $orderid;
        $content = $this->juheHttpRequest($this->orderStatusUrl, $params);
        return $this->_returnArray($content);
    }
    /**
     * 根据手机号码及面额查询是否支持充值
     * @param string $mobile [手机号码]
     * @param int $pervalue [充值金额]
     * @return  boolean
     */
    public function telCheck($mobile, $pervalue)
    {
        $params = &#39;key=&#39; . $this->appkey . &#39;&phoneno=&#39; . $mobile . &#39;&cardnum=&#39; . $pervalue;
        $content = $this->juheHttpRequest($this->telCheckUrl, $params);
        return $this->_returnArray($content);
    }
    /**
     * 根据手机号码和面额获取商品信息
     * @param string $mobile [手机号码]
     * @param int $pervalue [充值金额]
     * @return  array
     */
    public function telQuery($mobile, $pervalue)
    {
        $params = &#39;key=&#39; . $this->appkey . &#39;&phoneno=&#39; . $mobile . &#39;&cardnum=&#39; . $pervalue;
        $content = $this->juheHttpRequest($this->telQueryUrl, $params);
        return $this->_returnArray($content);
    }
    /**
     * 将JSON内容转为数据,并返回
     * @param string $content [内容]
     * @return array
     */
    public function _returnArray($content)
    {
        return json_decode($content, true);
    }
    /**
     * 请求接口返回内容
     * @param string $url [请求的URL地址]
     * @param string $params [请求的参数]
     * @param int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juheHttpRequest($url, $params = false, $ispost = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, &#39;JuheData&#39;);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . &#39;?&#39; . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to recharge phone bills in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!