luqita
luqita

Reputation: 4085

paypal programming

First of all, I'm proficient at PHP :)

But I'm a noob at Paypal :) So... How can I make single items buy buttons automatic? Something like this: I made a CMS so I can add new items for sale. But every time I add an item then I have to go to Paypal to create that item so it can be bought... Is there any way to do this automatic? This way, when I add a new item it can just be bought... Is there a Paypal solution for this? Something in the API?

Thank you!

Upvotes: 4

Views: 924

Answers (1)

Chris Baker
Chris Baker

Reputation: 50612

If you have a merchant account, you can use the Paypal NVP API to process payments completely from the server. I have developed several such systems. The documentation is ok, but there are some big spots where you can just get lost.

This is a sample from one of my payment object methods, it uses the paypal API and gets back a response code:

// 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'];

Upvotes: 3

Related Questions