如何将变量值从一个功能发送到另一页中的其他功能

时间:2019-04-18 04:33:38

标签: php oop frameworks

我是php oop的新手,我想将变量值从一个函数发送到另一页中的另一个函数。因此,当前我在一个页面中有一个功能,我想将数据发送到另一页面中的另一个功能。也许有可能吗?

这是sendData.php中的第一个函数

public function main($data) {

    $settings = new Settings();

    $hash_code = md5('standard' . '10068' . '08f94110d5697a2497511594c31704d0' .'3.00');
    $std_post = array(
                        'apitype'=>'standard',              //fix value
                        'apiid'=>'10068',               //your api id from ibill
                        'apiorderid'=>'OPC0001#00000282',               //your order id
                        'apihashcode'=>$hash_code,          //generate hash code as above
                        'apiamount'=>'3.00',                //your customer transaction amount
                        'apiemail'=>'alif4arsenal97@gmail.com');        //your customer email

   $callbackJSON = json_encode($std_post);

    $url = 'https://ibill.my/merchant/?ng=callback_api';        //link need to send data
    $ch = curl_init($url);                                      // where to post                                                                   
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $callbackJSON);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    $headers   = array();
    $headers[] = "Cache-Control: no-cache";
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    $results = curl_exec($ch);
    if (curl_errno($ch)) {
      echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    //echo $results;

    $objJSON = json_decode($results);                       //decode json result

    //should return 'SUCCESS'
    $callback_status = $objJSON->{'callback_status'};       //callback Status
    $message = $objJSON->{'message'};                       //callback Message 

    //Refer on statuspage.php
    $std_status_code = $objJSON->{'std_status_code'};       //payment status code
    $std_status = $objJSON->{'std_status'};                 //payment status
    $std_order_id = $objJSON->{'std_order_id'};             //your order id
    $std_purchase_code = $objJSON->{'std_purchase_code'};   //ibill transaction id
    $std_amount = $objJSON->{'std_amount'};                 //transaction amount
    $std_datepaid = $objJSON->{'std_datepaid'};             //transaction date time

    //Hash code for security
    $std_hash_code = $objJSON->{'std_hash_code'};           //Hash code
    $hash_code = md5('08f94110d5697a2497511594c31704d0'.'10068'.$std_order_id.$std_amount); //hash code format

    $data = [
        'callback_status' => $callback_status,
        'message' => $message,
        'std_status_code' => $std_status_code,
        'std_status' => $std_status,
        'std_order_id' => $std_order_id,
        'std_purchase_code' => $std_purchase_code,
        'std_amount' => $std_amount,
        'std_datepaid' => $std_datepaid,
        'std_hash_code' => $std_hash_code,
        'hash_code' => $hash_code 
    ];

    processPayment($data);

}

这是第二个函数,不同之处是我希望将第一页中的数据发送到test.php

public function processPayment($data)
{
    if (!isset($data['std_status_code'])) return false;
    if (!isset($data['std_hash_code'])) return false;

    $settings = new Settings();

    $sale_id = (int) substr($data['std_order_id'], 8);

    $sale = Sales::get($sale_id);

    if (empty($sale)) return false;

    if ($sale['status'] == 1) return $sale;

    if ($sale['payment_method'] !== 'ibill' || $sale['status'] != 0) return false;

    $sale_uid = $sale['uid'];
    $sale_method = $sale['method'];

    $paid_amount = bcadd($sale['total_amount'], $sale['handling_charge'], 2);

    // Verify the data integrity sent by iBill
    $hash = md5($settings->ibill_secret_key . $settings->ibill_merchant_id . $data['std_order_id'] . $data['std_amount']);

    $payment_processor_status = -1;
    $sale_status = 0;

    // Check provided hash and status
    if ($hash === $data['std_hash_code'] && $data['std_status_code'] == 00) {
        $payment_processor_status = 1;
        $sale_status = 1;
    }

    if ($sale_status === 0) {
        if ($data['std_status_code'] != 00) {
            $data['std_status'] = '<span style="color: red">' . $data['std_status'] . '</span>';
        }

        if ($data['std_hash_code'] !== $hash) {
            $data['std_hash_code'] = '<span style="color: red">' . $data['std_hash_code'] . '</span>';
        }
    }

    // Prepare updated sale data
    $now = new DateTime();

    $sale = [
        'payment_processor_status' => $payment_processor_status,
        'payment_processor_data' => $data,
        'payment_time' => $now->format('g:i:s A'),
        'payment_date' => $now->format('d-m-Y')
    ];

    Sales::update($sale_id, $sale);

    if ($sale_status === 1) {
        Sales::confirmSale($sale_id, false);
    }

    return ['uid' => $sale_uid, 'method' => $sale_method];
}

1 个答案:

答案 0 :(得分:2)

这些函数是类方法,而不仅仅是函数。 您可以通过创建它们的类的实例来使用它们(或将数据从一个传递到另一个)。例如这样的东西:

class one {
    public function f1($data) {
        // do something
        $instance = new two();
        $instance->f2($data);
    }
}

class two {
    public function f2($data) {
        // do something else
    }
}

我希望它对您有用。