asp.netasp.net-mvcpaypalexpress-checkoutpaypal-nvp

Payment using Paypal doesn't work after returning from Paypal site


My method of payment with Paypal does not work. After the user arrived on the Paypal page with the description of what he bought, it is directed to my site that says the payment was successful on

http://example.com/Store/ReturnPaypal/?token=EC-8F726826L7777777&PayerID=PEWR9EA

but when I go to my PayPal account, I don't see any extra money

public ActionResult OrderPaypal()
{
    string api_paypal = "https://api-3t.paypal.com/nvp?";
    string user = "info_api1.example.com";
    string pass = "testpass";
    string signature = "test.test-test";
    string version = "124.0"; 
    string requete = api_paypal + "VERSION=" + version + "&USER=" + user + "&PWD=" + pass + "&SIGNATURE=" + signature; 

    requete = requete + "&METHOD=SetExpressCheckout" +
               "&CANCELURL=" + HttpUtility.UrlEncode("http://example.com/Store/CancelPaypal/") +
                "&RETURNURL=" + HttpUtility.UrlEncode("http://example.com/Store/ReturnPaypal/") +
                "&AMT=" + SessionData.CurrentOrder.Total.ToString().Replace(',','.') +
                "&CURRENCYCODE=EUR" +
                "&DESC=" + HttpUtility.UrlEncode("Paiement total:" + SessionData.CurrentOrder.Total+" €") +
                "&LOCALECODE=FR" +
                "&HDRIMG=" + HttpUtility.UrlEncode("http://example.com/assets/images/home/image_10000.png");

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(requete);

    WebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        string rep = streamReader.ReadToEnd();
        if (!string.IsNullOrEmpty(rep))
        {
            string[] list = rep.Split('&');
            Dictionary<string,string> dicParam = new Dictionary<string,string>();
            foreach(string str in list)
            {
                string key = str.Split('=')[0];
                string value = str.Split('=')[1];

                dicParam.Add(key, value);
            }

            if (dicParam["ACK"] == "Success")
                return Redirect("https://www.paypal.com/webscr&cmd=_express-checkout&token=" + dicParam["TOKEN"]);
            else
            {
                ViewBag.State = "-1";
                return View("ReturnSolution",dicParam["L_SHORTMESSAGE0"]);
            }
        }
        else
        {
            ViewBag.State = "-1";
            return View("ReturnSolution", "Error communication PayPal ");
        }
    }           
}

Solution

  • I do not see you completing the transaction with the DoExpressCheckout call.

    It looks like you are doing the SetExpressCheckout which will give you the Payer ID and the Express Checkout token. You get this in the URL:

    http://example.com/Store/ReturnPaypal/?token=EC-8F726826L7777777&PayerID=PEWR9EA

    token=EC-8F726826L7777777

    PayerID=PEWR9EA

    This is returned in the URL and is used to complete the DoExpressCheckout call (which I do not see in your code).

    Here is a sample script of a SetExpressCheckout and DoExpressCheckout in PHP.

    <a href='http://marshalcurrier.com/paypal/ExpressCheckout/custom/SetDo.php'>RESET</a><br>
    <form method='post'><input type="text" name="CHARGE" value="1"/><input type="submit" value="Pay Now"/><form>
    <?php
    session_start();
    
    $PPUSER = 'marshal_api1.clubcovert.com';
    $PPPWD = 'LL6NV7TDRB9RFXQ5';
    $PPSIG = 'ANc3YRaMB1Tgm9TediH0gENHB02JAksSKWD08wVNN3w3pwHqdBW8Im6y';
    function url(){                 //PayPal Payment URL (TEST or LIVE)
        $url = "https://api-3t.sandbox.paypal.com/nvp";
        return $url;
    }
    function curlCall($nvp){        // Function for Curl Call to PayPal.
        $url = url();
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_HEADER, FALSE); 
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($nvp) );
        //echo http_build_query($nvp);  //Print String
        curl_setopt($ch, CURLOPT_URL, $url); 
        $server_output = curl_exec($ch);
        mb_parse_str($server_output, $arr);
        return $arr;
    }
    if(isset($_POST['CHARGE'])){    // SetExpressCheckout Call
        $_SESSION['AMT'] = $_POST['CHARGE'];
        $nvp = array(
            'USER'    => $PPUSER,
            'PWD'   => $PPPWD,
            'SIGNATURE' => $PPSIG,
            'METHOD' => 'SetExpressCheckout',
            'VERSION' => '123',
            'PAYMENTREQUEST_0_PAYMENTACTION' => 'SALE',
            'PAYMENTREQUEST_0_AMT' => $_POST['CHARGE'],
            'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
            'RETURNURL' => 'http://marshalcurrier.com/paypal/ExpressCheckout/SetDo.php',
            'CANCELURL' => 'http://marshalcurrier.com/paypal/ExpressCheckout/SetDo.php',
        );
        $arr = curlCall($nvp);
        echo '<br><br>SetExpressCheckout Call to PayPal:<br><pre>';
        print_r ($nvp);
        echo '</pre>';
        echo 'SetExpressCheckout Server Response:<br><pre>';
        print_r ($arr);
        echo '</pre>';
        echo '<a href="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.($arr['TOKEN']).'">Go To PayPal</a>';
    }
    if(isset($_GET['PayerID']) && (isset($_POST['CHARGE'])) == false){  // DoExpressCheckoutPayment Call
        if (isset($_SESSION['AMT'])){
            $AMT = $_SESSION['AMT'];
        }else{
            $AMT = null;
        }
        $nvp = array(
            'METHOD' => 'DoExpressCheckoutPayment',
            'VERSION' => '123',
            'USER'    => $PPUSER,
            'PWD'   => $PPPWD,
            'SIGNATURE' => $PPSIG,
            'PAYERID' => $_GET['PayerID'],
            'TOKEN' => $_GET['token'],
            'PAYMENTREQUEST_0_PAYMENTACTION' => 'SALE',
            'PAYMENTREQUEST_0_AMT' => $AMT,
            'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
        );
        $arr = curlCall($nvp);
        echo '<br><br>DoExpressCheckoutPayment Call to PayPal:<br><pre>';
        print_r ($nvp);
        echo '</pre>';
        echo 'DoExpressCheckoutPayment Server Response:<br><pre>';
        print_r ($arr);
        echo '</pre>';
        unset($_SESSION['AMT']); 
    }
    ?>