贝宝编程

时间:2011-02-14 22:32:32

标签: php paypal paypal-ipn

首先,我精通PHP:)

但我在Paypal上是一个菜鸟:)所以...我怎样才能让单个项目自动购买按钮?这样的事情:我做了一个CMS,所以我可以添加新的待售物品。但每次我添加一个项目,然后我必须去Paypal创建该项目,以便它可以买...有任何方法自动执行此操作吗?这样,当我添加一个新项目时,它就可以被买了......这有Paypal解决方案吗? API中有什么东西?

谢谢!

1 个答案:

答案 0 :(得分:3)

如果您有商家帐户,则可以使用Paypal NVP API完全从服务器处理付款。我开发了几个这样的系统。文档还可以,但有一些重要的地方可以让你迷路。

这是我的一种付款对象方法的示例,它使用paypal API并获取响应代码:

// this line gets a query string out of the posted payment data
$details = $this->___prepare_for_paypal($details);
if (!$details)
    return false;

$API_USERNAME = 'USERNAMEHERE';
$API_PASSWORD = 'PASSWORDHERE';
$API_CERTIFICATE = '/usr/web/paypal/cert_key_pem';
$API_SIGNATURE = '';
$API_ENDPOINT = 'https://api.paypal.com/nvp';
$PAYPAL_URL = 'https://www.paypal.com/webscr&cmd=_express-checkout&token=';
$VERSION = '51.0';

//setting the curl parameters.
$ch = curl_init();

curl_setopt($ch, CURLOPT_SSLCERT, $API_CERTIFICATE);
curl_setopt($ch, CURLOPT_URL,$API_ENDPOINT);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);

//NVPRequest for submitting to server
$nvpreq="METHOD=doDirectPayment&VERSION=".urlencode($VERSION)."&PWD=".urlencode($API_PASSWORD)."&USER=".urlencode($API_USERNAME).$details; 

//setting the nvpreq as POST FIELD to curl
curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
//getting response from server
$response = curl_exec($ch);
//convrting NVPResponse to an Associative Array
$nvpResArray=$this->___deformat_NVP($response);
// check the API response array.  ACK will contain the word 'Success' if everything went through ok
$ack = strtoupper($nvpResArray["ACK"]);
// if we get no love from paypal, stick the transaction into the DB for reference 
if($ack=="FAILURE") {
    $err_details = addslashes(serialize($nvpResArray));
    $err_details .= '||||'.addslashes($nvpreq);
    $sql = 'INSERT INTO payment_errors (time, user_id, details) VALUES ('.time().','.(user::export()->id ? user::export()->id : '0').',"'.$err_details.'")';
    db::update($sql);
    if (isset($nvpResArray['L_LONGMESSAGE0']))
        $this->_data['error_message'] = $nvpResArray['L_LONGMESSAGE0'];
    if (isset($nvpResArray['L_LONGMESSAGE0']))
        $this->_data['error_code'] = $nvpResArray['L_ERRORCODE0'];
    return false;
}

return $nvpResArray['TRANSACTIONID'];