gpt4 book ai didi

php - paypal api dodirectpayment nvp php 没有响应

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:41 25 4
gpt4 key购买 nike

我正在尝试使用 dodirectpayment NVP 设置一个 paypal api,我从在线资源中收集了一些代码,但是当我提交它时,我没有得到任何响应,只是一个白屏。我注意到我的代码中没有任何回显,但我不确定如何回显 paypal 发送的任何响应。任何帮助都会很棒。所有客户信息和凭据都来自沙盒。谢谢!

  enter code here <?php 

class Paypal {
/**
* Last error message(s)
* @var array
*/
protected $_errors = array();

/**
* API Credentials
* Use the correct credentials for the environment in use (Live / Sandbox)
* @var array
*/
protected $_credentials = array(
'USER' => 'centerfusiondesign-test_api1.gmail.com',
'PWD' => 'JBTYXGQHZY37RXGH',
'SIGNATURE' => 'ANRk81o3BhdjleyZOhWslseXywLQAfcftsn6e71ykaqxRzNASgC3NYUn',
);

/**
* API endpoint
* Live - https://api-3t.paypal.com/nvp
* Sandbox - https://api-3t.sandbox.paypal.com/nvp
* @var string
*/
protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';

/**
* API Version
* @var string
*/
protected $_version = ' 95.0';

/**
* Make API request
*
* @param string $method string API method to request
* @param array $params Additional request parameters
* @return array / boolean Response array / boolean false on failure
*/
public function request($method,$params = array()) {
$this -> _errors = array();
if( empty($method) ) { //Check if API method is not empty
$this -> _errors = array('API method is missing');
return false;
}

//Our request parameters
$requestParams = array(
'METHOD' => $method,
'VERSION' => $this -> _version
) + $this -> _credentials;

//Building our NVP string
$request = http_build_query($requestParams + $params);

//cURL settings
$curlOptions = array (
CURLOPT_URL => $this -> _endPoint,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $request
);

$ch = curl_init();
curl_setopt_array($ch,$curlOptions);

//Sending our request - $response will hold the API response
$response = curl_exec($ch);

//Checking for cURL errors
if (curl_errno($ch)) {
$this -> _errors = curl_error($ch);
curl_close($ch);
return false;
//Handle errors
} else {
curl_close($ch);
$responseArray = array();
parse_str($response,$responseArray); // Break the NVP string to an array
return $responseArray;
}
}
}

$requestParams = array(
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
'PAYMENTACTION' => 'Sale'
);

$creditCardDetails = array(
'CREDITCARDTYPE' => 'Visa',
'ACCT' => '4032032109229382',
'EXPDATE' => '012020',

);

$payerDetails = array(
'FIRSTNAME' => 'John',
'LASTNAME' => 'Doe',
'COUNTRYCODE' => 'US',
'STATE' => 'NY',
'CITY' => 'New York',
'STREET' => '14 Argyle Rd.',
'ZIP' => '10010'
);

$orderParams = array(
'AMT' => '500',
'ITEMAMT' => '496',
'SHIPPINGAMT' => '4',
'CURRENCYCODE' => 'GBP'
);

$item = array(
'L_NAME0' => 'iPhone',
'L_DESC0' => 'White iPhone, 16GB',
'L_AMT0' => '496',
'L_QTY0' => '1'
);

$paypal = new Paypal();
$response = $paypal -> request('DoDirectPayment',
$requestParams + $creditCardDetails + $payerDetails + $orderParams + $item
);

if( is_array($response) && $response['ACK'] == 'Failure') { // Payment successful
// We'll fetch the transaction ID for internal bookkeeping
$transactionId = $response['TRANSACTIONID'];


}


?>

我对 PHP 有点陌生,所以希望我的错误太愚蠢了,谢谢!

最佳答案

您似乎启用了 CURL 对等证书验证,出于测试目的您可能希望禁用它,直到您在服务器上设置它。

替换:

CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem', //CA cert file

与:

CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,

此外,要检查响应,只需添加

print_r($response);

下面:

$response = $paypal -> request('DoDirectPayment',
$requestParams + $creditCardDetails + $payerDetails + $orderParams + $item
);

让我帮你得到下面的完整代码:

<?php 
class Paypal {
/**
* Last error message(s)
* @var array
*/
protected $_errors = array();

/**
* API Credentials
* Use the correct credentials for the environment in use (Live / Sandbox)
* @var array
*/
protected $_credentials = array(
'USER' => 'centerfusiondesign-test_api1.gmail.com',
'PWD' => 'JBTYXGQHZY37RXGH',
'SIGNATURE' => 'ANRk81o3BhdjleyZOhWslseXywLQAfcftsn6e71ykaqxRzNASgC3NYUn',
);

/**
* API endpoint
* Live - https://api-3t.paypal.com/nvp
* Sandbox - https://api-3t.sandbox.paypal.com/nvp
* @var string
*/
protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';

/**
* API Version
* @var string
*/
protected $_version = ' 95.0';

/**
* Make API request
*
* @param string $method string API method to request
* @param array $params Additional request parameters
* @return array / boolean Response array / boolean false on failure
*/
public function request($method,$params = array()) {
$this -> _errors = array();
if( empty($method) ) { //Check if API method is not empty
$this -> _errors = array('API method is missing');
return false;
}

//Our request parameters
$requestParams = array(
'METHOD' => $method,
'VERSION' => $this -> _version
) + $this -> _credentials;

//Building our NVP string
$request = http_build_query($requestParams + $params);

//cURL settings
$curlOptions = array (
CURLOPT_URL => $this -> _endPoint,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $request
);

$ch = curl_init();
curl_setopt_array($ch,$curlOptions);

//Sending our request - $response will hold the API response
$response = curl_exec($ch);

//Checking for cURL errors
if (curl_errno($ch)) {
$this -> _errors = curl_error($ch);
curl_close($ch);
return false;
//Handle errors
} else {
curl_close($ch);
$responseArray = array();
parse_str($response,$responseArray); // Break the NVP string to an array
return $responseArray;
}
}
}

$requestParams = array(
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
'PAYMENTACTION' => 'Sale'
);

$creditCardDetails = array(
'CREDITCARDTYPE' => 'Visa',
'ACCT' => '4032032109229382',
'EXPDATE' => '012020',

);

$payerDetails = array(
'FIRSTNAME' => 'John',
'LASTNAME' => 'Doe',
'COUNTRYCODE' => 'US',
'STATE' => 'NY',
'CITY' => 'New York',
'STREET' => '14 Argyle Rd.',
'ZIP' => '10010'
);

$orderParams = array(
'AMT' => '500',
'ITEMAMT' => '496',
'SHIPPINGAMT' => '4',
'CURRENCYCODE' => 'GBP'
);

$item = array(
'L_NAME0' => 'iPhone',
'L_DESC0' => 'White iPhone, 16GB',
'L_AMT0' => '496',
'L_QTY0' => '1'
);

$paypal = new Paypal();
$response = $paypal -> request('DoDirectPayment',
$requestParams + $creditCardDetails + $payerDetails + $orderParams + $item
);
print_r($response);

if( is_array($response) && $response['ACK'] == 'Failure') { // Payment successful
// We'll fetch the transaction ID for internal bookkeeping
$transactionId = $response['TRANSACTIONID'];
}
?>

此外,您可能希望将您的沙盒企业帐户升级到专业版,请引用以下链接: https://stackoverflow.com/a/21926016/4410290

希望这会有所帮助.. :)

关于php - paypal api dodirectpayment nvp php 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com