0

I am working on the recurring payment for paypal in php. I have seen some examples and below are the code which seems simple to me to understand.

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="info@capleswebdev.com">
<!-- Specify a Subscribe button. -->
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<!-- Identify the subscription. -->
<input type="hidden" name="item_name" value="Alice's Weekly Digest">
<input type="hidden" name="item_number" value="DIG Weekly">
<!-- Set the terms of the regular subscription. -->
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="a3" value="5.00">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="t3" value="M">
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >
</form> 

According to the above code, it will charge 5 dollar every month. I really don't know if that code will work or not. I also want to know that in the above code there is no option for Paypal API username , Password and Secret key only it got the business which is the email id registered with the paypal.I am totally confused if it will work or not or how to do it properly. Please suggest me something and how shall i able to recieve the data after user canceled or successfully paid the money to my paypal account

  • Hiya, look here, http://stackoverflow.com/questions/24318396/is-there-a-way-to-set-up-recurring-payments-with-the-paypal-rest-api/25288111#25288111 – Liam Sep 20 '15 at 01:42

1 Answers1

0

To avoid double testing you can use price for testing like 0,01. To create a recurring payment use this html form:

<form method="post" name="formName" id="submitThisForm" action="https://www.paypal.com/cgi-bin/webscr" >
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="your@papypamail.com" />
<input type="hidden" name="item_name" value="Your Membership" />
<input type="hidden" name="a3" value="0.01">
<input type="hidden" name="p3" value="1"> 
<input type="hidden" name="t3" value="M">
<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1">
<input type="hidden" name="item_number" value="2" />
<input type="hidden" name="custom" value="SECURITYCODE" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="quantity" value="1" />
<input type="hidden" name="no_shipping" value="1" />
<input type="hidden" name="return" value="page going after payment" />
<input type="hidden" name="cancel_return" value="" />
<input type="hidden" name="cbt" value="ITEM DESCRIPTION" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="notify_url" value="your_listener_file.php" />

When the user is cancel a membership, paypal will notify on "notify_url" - in your case this will be file your_listener_file.php. Inside the this file you must check paypal POST variable 'txn_type' = 'subscr_cancel'. There is a several important points:

  1. You must validate the ipn transaction:

        $post           = array( 'cmd' => '_notify-validate' );
    foreach($_POST as $key=>$value){
            $post[$key] = $value;
    }
    $c  = curl_init();
    curl_setopt_array($c, array(
        CURLOPT_FOLLOWLOCATION  => TRUE,
        CURLOPT_RETURNTRANSFER  => TRUE,
        CURLOPT_CONNECTTIMEOUT  => 15,
        CURLOPT_MAXREDIRS       => 15,
        CURLOPT_TIMEOUT         => 15,
        CURLOPT_URL             => 'https://www.paypal.com/cgi-bin/webscr',
        CURLOPT_POST            => TRUE,
        CURLOPT_POSTFIELDS      => $post,
    ));
    $res = curl_exec($c);
    curl_close($c);
    $res    = trim($res);
    
    if( $res != 'VERIFIED' ) {
        exit();
    }
    
  2. Second - Check is the transaction exist in your database using the unique key. You must use Paypal POST variable 'custom'.

  3. If the transaction exist, just make some simple check:

    if( !empty($_POST['txn_type']) && $_POST['txn_type'] == 'subscr_cancel' )
        $paypalData['approved'] = 0;
    
Pavel Kenarov
  • 944
  • 1
  • 9
  • 21