我的HTTP.call()代码会返回不需要的乱码。 下面是HTTP.call()代码:
HTTP.call('POST', 'http://2117c3a8.ngrok.io/AConnect/c2b.php', {
headers: { apiKey: 'AC6ea8e1541e7ee0c40915e' },
data: postData
}, function(error, result) {
if (error) {
console.log(error);
}
if (result) {
console.log(result);
}
});
理想的回报应该看起来像这样:
The payment of KES 222 has been sent your account
但我终于在终端得到了这个胡言乱语:
I20170712-21:18:51.366(3)? { statusCode: 200,
I20170712-21:18:51.368(3)? content: '<br />\n<b>Notice</b>: Undefined index: recipient in <b>/opt/lampp/htdocs/AfricasTalking/escrow/c2b.php</b> on line <b>19</b> <br />\n<br />\n<b>Notice</b>: Undefined index: amount in <b>/opt/lampp/htdocs/AfricasTalking/escrow/c2b.php</b> on line <b>21</b> <br />\n<br />\n<b>Notice</b>: Undefined index: payer in <b>/opt/lampp/htdocs/AfricasTalking/escrow/c2b.php</b> on line <b>26</b> <br />\n<br />\n<b>Notice</b>: Undefined index: amount in <b>/opt/lampp/htdocs/AfricasTalking/escrow/c2b.php</b> on line <b>28</b> <br />\nReceived error response: The request content was malformed:\nExpected String as JsString, but got null',
I20170712-21:18:51.368(3)? headers:
I20170712-21:18:51.369(3)? { date: 'Wed, 12 Jul 2017 18:18:49 GMT',
I20170712-21:18:51.370(3)? server: 'Apache/2.4.18 (Unix) OpenSSL/1.0.2g PHP/5.6.20 mod_perl/2.0.8-dev Perl/v5.16.3',
I20170712-21:18:51.371(3)? 'x-powered-by': 'PHP/5.6.20',
I20170712-21:18:51.371(3)? 'content-length': '619',
I20170712-21:18:51.372(3)? connection: 'close',
I20170712-21:18:51.373(3)? 'content-type': 'text/html; charset=UTF-8' },
I20170712-21:18:51.376(3)? data: null }
I20170712-21:18:51.400(3)? Exception in callback of async function: ReferenceError: msg is not defined
I20170712-21:18:51.401(3)? at server/main.js:39:33
I20170712-21:18:51.402(3)? at runWithEnvironment (packages/meteor.js:1176:24)
HTTP.call()调用的文件( http://2117c3a8.ngrok.io/AConnect/c2b.php )看起来像这样
<?php
require_once "AConnect.php";
//Specify your credentials
$username = "007";
$apiKey = "AC6ea8e1541e7ee0c40915e";
// NOTE: If connecting to the sandbox, please use your sandbox login credentials
//Create an instance of our awesome gateway class and pass your credentials
$gateway = new AConnect($username, $apiKey, "sandbox");
// Specify the name of your AConnect payment product
$productName = "cente";
// The 3-Letter ISO currency code for the checkout amount
// Provide the details of a mobile money recipient
$recipient = array(
"phoneNumber" => $_POST['recipient'],
"currencyCode" => "KES",
"amount" => $_POST['amount'],
"metadata" => array(
"name" => "Something...",
"reason" => "Som other thing...")
);
$phoneNumber = $_POST['payer'];
$currencyCode = "KES";
$amount = $_POST['amount'];
$metadata = array("name" => "Something...", "reason" => "Som other thing...");
try {
$transactionId = $gateway->initiateMobilePaymentCheckout($productName,
$phoneNumber,
$currencyCode,
$amount,
$metadata);
$msg = 'The payment of '. $currencyCode .' '. $amount .' has been sent your account';
$gateway->sendMessage($_POST['recipient'].','.$_POST['payer'], $msg);
echo $msg;
} catch(AConnect $e){
echo "Received error response: ".$e->getMessage();
}
?>
以下是我的server / main.js:
Meteor.methods({
'c2b': function(recipientsNumber, recipientsAmount, payer){
var postData = {
params: {
"payer": payer,
"recipient": recipientsNumber,
"amount": recipientsAmount
}
}
HTTP.call('POST', 'http://2117c3a8.ngrok.io/AConnect/c2b.php',
{ headers: { apiKey: 'AC6ea8e1541e7ee0c40915e' }, params: postData},
function(error, response) {
if (error) {
console.log(error);
}
if (response) {
console.log(response);
}
}
);
}
});
请帮助我解释如何修改HTTP.call()以便它返回相关信息。