Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Learn 4 Easy Steps to Integrate Standard PayPal Gateway in PHP

    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 1
    • 0
    • 2.00k
    Comment on it

    PayPal is an American Company Founded in 1998 by Palo Alto of California "United States". It provides services to users of online payment globally such as money transfer and traditional payment methods like checks and money orders.

     

     

    In today's world PayPal has become a very popular payment gateway system to send and receive payment worldwide.

    Also, It is an easiest and simplest way to the web developer for implementing payment system on the website. Hence it is true to say that PayPal is a widely used payment gateway to accept payment in the web application.

    In this tutorial, we will learn to "Integrated Standard PayPal Gateway in PHP".

     

    Step 1: First you have to create a Paypal Sandbox account.

    There are two accounts in Paypal Sandbox

    • Personal/Buyer => This account represents the buyer or sender, When user goes for the transaction.
    • Business/Merchant => This account represents the merchant, or receiver, in a transaction.

     

    Step 2: Now create products.php for payment system

    <?php
    $paypalUrl='https://www.sandbox.paypal.com/cgi-bin/webscr'; // Test Paypal API URL of sendbox
    $paypalId='your_business_id'; // write here your business email id
    ?>
    <h4>Welcome, User</h4>
     
    <div class="product">            
      
        <div class="name">
            Paypall Payment
        </div><br>
        <div class="item_price">
            Price:$100
        </div><br>
        <div class="btn">
            <form action="<?php echo $paypalUrl; ?>" method="post" name="frmPayPal1">
            <input type="hidden" name="business" value="<?php echo $paypalId; ?>">
            <input type="hidden" name="cmd" value="_xclick">
            <input type="hidden" name="item_name" value="paypall Payment">
            <input type="hidden" name="item_number" value="1">
            <input type="hidden" name="credits" value="200">
            <input type="hidden" name="userid" value="1">
            <input type="hidden" name="amount" value="100">
            <input type="hidden" name="currency_code" value="USD">
            <input type="hidden" name="cancel_return" value="http://your project url/cancel.php">
            <input type="hidden" name="return" value="http://your project url/success.php">
            <input type="image" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/buy-logo-medium.png" alt="" />
            
            </form> 
        </div>
    </div>

    Using above code your products.php page will be created for payment system on that page you will have subscribe button.

     

    Step 3:Now you have to click on subscribe button and you will redirect to PayPal payment page, on that page you have to login with your PayPal Sandbox id "Personal/Buyer" and after that you will be able to do payment where user will find two options, one will be, proceed for payment and another one will be, cancel payment and once if you select proceed for payment you will redirect on success.php page with below data.

    <?php
    $item_no            = $_REQUEST['item_number'];
    $transaction_id     = $_REQUEST['tx']; // Paypal transaction ID
    $amount             = $_REQUEST['amt']; // Paypal received amount
    $currency_code      = $_REQUEST['cc']; // Paypal received currency type
     
    //checking the payment details
    if(!empty($transaction_id))
    {
        echo "<h1>Welcomem, User</h1>";
        echo "<h1>Your payment has been Successful.</h1>";
    }
    else
    {
        echo "<h1>Error ! Payment Failed .</h1>";
    }
    ?>

     

    Or If you select cancel payment option then you will redirect to below cancel.php page

    <?php
    echo "<h1>Welcome, User</h1>";
    echo "<h1>Your payment has benn Payment Canceled</h1>";
    ?>

     

    Step 4 -> Once everything is set, after that you should test your web application payment system workflow and if it is working fine then you have to change your form action API URL to original API URLs also you have to change seller email id.

    you can see below for better understanding

    $paypalUrl='https://www.sandbox.paypal.com/cgi-bin/webscr'; // it is sandbox url
    

    now here you will change your original API URLs, as below:

    https://www.paypal.com/cgi-bin/webscr

     

    Well, this was fun. Isn’t it? If you have any question and wanted to share your thoughts please feel free to share in the comment section below.

    Learn 4 Easy Steps to Integrate Standard PayPal Gateway in PHP

 3 Comment(s)

  • Hello @jozospisiak@gmail.com

    For verify paypal payment you have to send transaction id to paypal with below code :
     
    <?php
    //for verify payment by paypall
    $ch = curl_init();
    $clientId = "AYnfAxcwUmqMWMuiZeXDa96asNKmarUEDhS4jRL3bzVGLTl4P20PEzBArmdge6JclwH2_W66UFilvW9P"; //Clinet id of merchant accont
    $secret = "EB5hxlMM9643evfWHImKgS0CpZRv-n0HjFf1xDSYhnd0bApaxfwT-GRPjsBvEy0uwr4dexX5Fd8Q3_CU";//Secret key of merchant accont
    
    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSLVERSION , 6);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
    
    $result = curl_exec($ch);
    
    if(empty($result)){ die("Error: No response.");
    
    }else{
     
    //This is the data to be sent in the call
    $postdata = array(
    'USER' => 'merchant.sujit_api1.gmail.com', //write here merchant email
    'PWD' => 'AXT8J3ET38DVXA7D', // write here password
    'SIGNATURE' => 'AFcWxV21C7fd0v3bYYYRCpSSRl31AxAM8YJrWrtM.D-CmZicgGLTLt0K', // write here signature
    'METHOD' => 'GetTransactionDetails',
    'VERSION' => '123',
    'TransactionID' => $transaction_id // write transaction id here
    );
    
    $postdata = http_build_query($postdata);
    
        $json = json_decode($result);
     
        curl_close($ch);
        $url = 'https://api-3t.sandbox.paypal.com/nvp';
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl, CURLOPT_SSLVERSION , 6);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Authorization: Bearer '.$json->access_token,
                    'Accept: application/json',
                    'Content-Type: application/json'
                    ));
    
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        $response = curl_exec( $curl );
        $verify_tx = urldecode($response);
        parse_str($verify_tx,$paypal_detail);
    
        print_r($paypal_detail);
        die;
    
    
    }
    ?>



    After executing above code you will get a responce of all details of payment and now you can save all necessary details in database.

    Note :- Also check in database with transaction id if this transaction id already exist or not.
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: