没有后期数据(bitpay)

时间:2014-10-18 08:11:20

标签: php

我们正在将Bitpay整合到我们的网站,但每次我们付款并回拨通知网址时,我们都会收到“没有发布数据”。我们的网址是https按要求,一切似乎都很好,请帮忙。这是代码。

bp_options.php

global $bpOptions;
$bpOptions['apiKey'] = 'xxxxxxx';
$bpOptions['verifyPos'] = true;
$bpOptions['notificationEmail'] = 'our email';
$bpOptions['notificationURL'] = 'https://www.ourwebsite.com/completesubscription/bitpaynotification';;
$bpOptions['redirectURL'] = 'https://www.ourwebsite.com/completesubscription?method=bitpay';;
$bpOptions['currency'] = 'USD';
$bpOptions['physical'] = true;
$bpOptions['fullNotifications'] = true;
$bpOptions['transactionSpeed'] = 'high';
$bpOptions['logFile'] = '/bplog.txt';
$bpOptions['useLogging'] = TRUE;

bp_lib.php

require_once 'bp_options.php';

function bpLog($contents) { 
   global $bpOptions; 

   try { 
     if(isset($bpOptions['logFile']) && $bpOptions['logFile'] != '') { 
         $file = dirname(__FILE__).$bpOptions['logFile']; 
     } else { 
        // Fallback to using a default logfile name in case the variable is 
        // missing or not set. 
        $file = dirname(__FILE__).'/bplog.txt'; 
     }

     file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);

     if (is_array($contents)) 
       $contents = var_export($contents, true); 
     else if (is_object($contents)) 
       $contents = json_encode($contents);

     file_put_contents($file, $contents."\n", FILE_APPEND);

    } catch (Exception $e) { 
      echo 'Error: ' . $e->getMessage(); 
    } 
}

function bpCurl($url, $apiKey, $post = false) { 
global $bpOptions;

if((isset($url) && trim($url) != '') && (isset($apiKey) && trim($apiKey) != '')) { 
try { 
$curl = curl_init(); 
$length = 0;

if ($post) { 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
$length = strlen($post); 
}

$uname = base64_encode($apiKey);

if($uname) { 
$header = array( 
'Content-Type: application/json', 
'Content-Length: ' . $length, 
'Authorization: Basic ' . $uname, 
'X-BitPay-Plugin-Info: phplib1.5', 
);

curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_PORT, 443); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // verify certificate 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // check existence of CN and verify that it matches hostname 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); 
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);

$responseString = curl_exec($curl);

if($responseString == false) { 
$response = array('error' => curl_error($curl)); 
if($bpOptions['useLogging']) 
bpLog('Error: ' . curl_error($curl)); 
} else { 
$response = json_decode($responseString, true); 
if (!$response) { 
$response = array('error' => 'invalid json: '.$responseString); 
if($bpOptions['useLogging']) 
bpLog('Error - Invalid JSON: ' . $responseString); 
} 
}

curl_close($curl); 
return $response; 
} else { 
curl_close($curl); 
if($bpOptions['useLogging']) 
bpLog('Invalid data found in apiKey value passed to bpCurl. (Failed: base64_encode(apikey))'); 
return array('error' => 'Invalid data found in apiKey value passed to bpCurl. (Failed: base64_encode(apikey))'); 
} 
} catch (Exception $e) { 
@curl_close($curl); 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return array('error' => $e->getMessage()); 
} 
} else { 
// Invalid parameter specified 
if($bpOptions['useLogging']) 
bpLog('Error: You must supply non-empty url and apiKey parameters.'); 
return array('error' => 'You must supply non-empty url and apiKey parameters.'); 
}

}

function bpCreateInvoice($orderId, $price, $posData, $options = array()) {

global $bpOptions;

try { 
$options = array_merge($bpOptions, $options); // $options override any options found in bp_options.php 
$pos = array('posData' => $posData);

if ($bpOptions['verifyPos']) 
$pos['hash'] = bpHash(serialize($posData), $options['apiKey']);

$options['posData'] = json_encode($pos);

if(strlen($options['posData']) > 100) 
return array('error' => 'posData > 100 character limit. Are you using the posData hash?');

$options['orderID'] = $orderId; 
$options['price'] = $price;

$postOptions = array('orderID', 'itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL', 
'posData', 'price', 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName', 
'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone'); 

/* $postOptions = array('orderID', 'itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL', 
'posData', 'price', 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName', 
'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone', 
'pluginName', 'pluginVersion', 'serverInfo', 'serverVersion', 'addPluginInfo'); 
*/ 
// Usage information for support purposes. Do not modify. 
//$postOptions['pluginName'] = 'PHP Library'; 
//$postOptions['pluginVersion'] = '1.3'; 
//$postOptions['serverInfo'] = htmlentities($_SERVER['SERVER_SIGNATURE'], ENT_QUOTES);
//$postOptions['serverVersion'] = htmlentities($_SERVER['SERVER_SOFTWARE'], ENT_QUOTES); 
//$postOptions['addPluginInfo'] = htmlentities($_SERVER['SCRIPT_FILENAME'], ENT_QUOTES);

foreach($postOptions as $o) { 
if (array_key_exists($o, $options)) 
$post[$o] = $options[$o]; 
}

$post = json_encode($post);

$response = bpCurl('bitpay/api/invoice/';, $options['apiKey'], $post);

if($bpOptions['useLogging']) { 
bpLog('Create Invoice: '); 
bpLog($post); 
bpLog('Response: '); 
bpLog($response); 
}

return $response;

} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return array('error' => $e->getMessage()); 
} 
}

function bpVerifyNotification($apiKey = false) { 
global $bpOptions;

try { 
if (!$apiKey) 
$apiKey = $bpOptions['apiKey'];

$post = file_get_contents("php://input");

if (!$post) 
return 'No post data';

$json = json_decode($post, true);

if (is_string($json)) 
return $json; // error

if (!array_key_exists('posData', $json)) 
return 'no posData';

$posData = json_decode($json['posData'], true);

if($bpOptions['verifyPos'] and $posData['hash'] != bpHash(serialize($posData['posData']), $apiKey)) 
return 'authentication failed (bad hash)';

$json['posData'] = $posData['posData'];

return $json; 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return array('error' => $e->getMessage()); 
} 
}

function bpGetInvoice($invoiceId, $apiKey=false) { 
global $bpOptions;

try { 
if (!$apiKey) 
$apiKey = $bpOptions['apiKey'];

$response = bpCurl('bitpay/invoice/'.$invoiceId, $apiKey);

if (is_string($response)) 
return $response; // error

$response['posData'] = json_decode($response['posData'], true); 
$response['posData'] = $response['posData']['posData'];

return $response; 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return 'Error: ' . $e->getMessage(); 
} 
}

function bpHash($data, $key) { 
global $bpOptions; 

try { 
$hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE)); 
return strtr($hmac, array('+' => '-', '/' => '_', '=' => '')); 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return 'Error: ' . $e->getMessage(); 
} 
}

function bpDecodeResponse($response) { 
global $bpOptions; 

try { 
if (empty($response) || !(is_string($response))) 
return 'Error: decodeResponse expects a string parameter.';

return json_decode($response, true); 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return 'Error: ' . $e->getMessage(); 
} 
}

function bpCurrencyList() { 
global $bpOptions;

$currencies = array(); 
$rate_url = 'bitpay/rates';;

try { 
$clist = json_decode(file_get_contents($rate_url),true);

foreach($clist as $key => $value) 
$currencies[$value['code']] = $value['name'];

return $currencies; 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return 'Error: ' . $e->getMessage(); 
} 
}

function bpGetRate($code = 'USD') { 
global $bpOptions;

$rate_url = 'bitpay/rates';;

try { 
$clist = json_decode(file_get_contents($rate_url),true);

foreach($clist as $key => $value) { 
if($value['code'] == $code) 
$rate = number_format($value['rate'], 2, '.', ''); 
} 

return $rate; 
} catch (Exception $e) { 
if($bpOptions['useLogging']) 
bpLog('Error: ' . $e->getMessage()); 
return 'Error: ' . $e->getMessage(); 
} 
}

send.php

$amount = $this->input->post('total_amount'); 
$id_order = $this->input->post('id_order'); 
$post_data = array( 
'notificationURL' => '/completesubscription/bitpaynotification';, 
'id_order' => $this->input->post('id_order') 
); 
$addl_options = array( 
'itemDesc' => $this->input->get('option') 
); 
$response = bpCreateInvoice($id_order, $amount, $post_data, $addl_options); 
if(!empty($response)){ 
header('Location:'.$response['url'].'');

notification.php

function BitpayNotification() 
{ 
global $bpOptions; 
$session_data = $this->session->userdata('logged_in'); 
echo $bpOptions['apiKey']; 
$response = bpVerifyNotification($bpOptions['apiKey']); 
$order_id = $response['posData']; 
$sessionid = $order_id; 
$this->createSubscribe($sessionid,$session_data['id']); 
}

redirect.php

$data['method'] = 'bitpay'; 
$this->load->view('complete_subscribe_view', $data);

如果我做错了,请检查并告诉我。

0 个答案:

没有答案