2011-02-14 86 views
4

首先,我精通PHP :)贝宝编程

但我在Paypal进行小白:)所以...我如何才能让单品购买按钮自动?像这样的东西:我做了一个CMS,所以我可以添加新的物品出售。但每次我添加一个项目,然后我必须去PayPal创建该项目,以便它可以买...有没有办法自动做到这一点?这样,当我添加一个新的项目,它可以只是被买了...是否有贝宝解决方案呢? API中的东西?

谢谢!

+0

有几个选择,但首先这取决于你有贝宝账户类型。 – 2011-02-14 22:39:11

回答

3

如果您有商户帐户,则可以使用PayPal NVP API完全从服务器处理付款。我开发了几个这样的系统。文件是好的,但有一些地方你可能会迷路。

这是从我的支付对象方法中的一个样本,它使用贝宝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'];