A basic code and explanation for paypal direct payment using name value pairs NVP. For a complete list of other paypal payment methods click here or download the samples from paypal here. By default this code connects to the sandbox url of paypal. You can change that to the live url after experimenting. You need to get api username, api password and api signature from merchant to whom the users will pay. Here paypal had provided with a testing username, password and signature which i have used in the code as given by paypal. The procedure is very simple. The below form will be submitted to a php file. That script will store the post data into variables and will create a query string including the authentication credentials. Then we connect to the paypal server and will post the request data using CURL. The response is converted into an array and is parsed to find whether the payment is a success. Please note that when success the server gives a transaction id which we shall store it in the database for furtuer references of that payment through the admin panel though the client can access all his transactions through his paypal login.

You can also copy the html part of the code by viewing the source code of this page. Since the html code is very simple to copy i did not provide the html code here. In the zip file you can find both the html and the php script.

and we call the function PPHttpPost with the post string as argument. This function is included to seperate the code from the actual request CURL code. Just like that. No other purpose. Just readability. The function returns an array with the data from the payment gateway. The key ACK holds whether the process was a success or a failure. Now let us see what is in that function PPHttpPost.

Now the above associative array which is the actual key value pairs which the payment gateway understands is converted into a query string. The query string which is to be posted to the payment gateway is stored in $post_string.

In this function we append the key value pairs of the username, password and signature to the post data query string which we already created.

Now we connect and post the data to the payment gateway using the following curl functions. The line where this code is $httpResponse = curl_exec($ch); will fetch the response and stores in the variable $httpResponse.

Here if some thing had gone wrong like connection interrupted or timeout then we end up inside the following if condition which means we got an invalid data from the payment gateway. If the response is good then we return the array to the code where it was called and that is where we call the function PPHttpPost.

According to the key ACK in the return array we determine whether the payment was successful or not. So if the key ACK of the returned array holds success the you know what to do. Just do print_r and find the return values of the payment gateway and use it according to your need. You can store the transaction id from the response for furture reference about the payment.