/// <summary>
        /// ConfirmPayment: The method that calls SetExpressCheckout API, invoked from the 
        /// Billing Page EC placement
        /// </summary>
        /// <param name="token"></param>
        /// <param ref name="retMsg"></param>
        /// <returns></returns>
        public bool ConfirmPayment(string finalPaymentAmount, string token, string PayerId, ref NVPCodec decoder, ref string retMsg )
        {
            if (bSandbox)
            {
                pendpointurl = "https://api-3t.sandbox.paypal.com/nvp";
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "DoExpressCheckoutPayment";
            encoder["TOKEN"] = token;
            encoder["PAYMENTACTION"] = "Sale";
            encoder["PAYERID"] = PayerId;
            encoder["AMT"] = finalPaymentAmount;
            encoder["CURRENCYCODE"] = "EUR";

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                         "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                         "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }
示例#2
0
        /// <summary>
        /// Permet de confirmer la commande auprès de Paypal et de débiter le compte du client.
        /// </summary>
        /// <param name="token">id transaction-paypal</param>
        /// <param name="payerId">id du compte du client</param>
        /// <param name="finalAmount">somme finale, au cas ou le panier aurait été mis à jour entre temps ou modifié frauduleusement.</param>
        /// <param name="returnedMsg">message de retour, "" si pas d'erreur. (à gérer coté controller)</param>
        /// <returns>renvoi validated si ok et nonvalidated si erreur.</returns>
        public static string OrderConfirmation(string token, string payerId, decimal finalAmount, ref string returnedMsg)
        {
            /*DEBUT PAYPAL */
            NVPAPICaller test = new NVPAPICaller();

            string retMsg = "";
            NVPCodec decoder = new NVPCodec();
            string finalAmountStr = finalAmount.ToString(CultureInfo.InvariantCulture).Replace(',', '.');
            bool ret = test.ConfirmPayment(finalAmountStr, token, payerId, ref decoder, ref retMsg);
            if (ret)
            {
                // Unique transaction ID of the payment. Note:  If the PaymentAction of the request was Authorization or Order, this value is your AuthorizationID for use with the Authorization & Capture APIs.
                string transactionId = decoder["TRANSACTIONID"];

                // The type of transaction Possible values: l  cart l  express-checkout
                string transactionType = decoder["TRANSACTIONTYPE"];

                // Indicates whether the payment is instant or delayed. Possible values: l  none l  echeck l  instant
                string paymentType = decoder["PAYMENTTYPE"];

                // Time/date stamp of payment
                string orderTime = decoder["ORDERTIME"];

                // The final amount charged, including any shipping and taxes from your Merchant Profile.
                string amt = decoder["AMT"];

                // A three-character currency code for one of the currencies listed in PayPay-Supported Transactional Currencies. Default: USD.
                string currencyCode = decoder["CURRENCYCODE"];

                // PayPal fee amount charged for the transaction
                string feeAmt = decoder["FEEAMT"];

                // Amount deposited in your PayPal account after a currency conversion.
                string settleAmt = decoder["SETTLEAMT"];

                // Tax charged on the transaction.
                string taxAmt = decoder["TAXAMT"];

                //' Exchange rate if a currency conversion occurred. Relevant only if your are billing in their non-primary currency. If
                string exchangeRate = decoder["EXCHANGERATE"];

                //enable to send a mail easily. Not used in that sample.
                //new SendMail.Paypal().TransactionDone(transactionId, transactionType, paymentType, orderTime, amt, currencyCode, feeAmt, settleAmt, taxAmt, exchangeRate);

                return "VALIDATED";
            }
            returnedMsg = retMsg;
            return "NonValidated";
            //return RedirectToAction("Index", "PaypalErrors", new { ErrorCode = retMsg });
        }
        /// <summary>
        /// Credentials added to the NVP string
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        private string buildCredentialsNVPString()
        {
            NVPCodec codec = new NVPCodec();

            if (!IsEmpty(APIUsername))
                codec["USER"] = APIUsername;

            if (!IsEmpty(APIPassword))
                codec[PWD] = APIPassword;

            if (!IsEmpty(APISignature))
                codec[SIGNATURE] = APISignature;

            if (!IsEmpty(Subject))
                codec["SUBJECT"] = Subject;

            codec["VERSION"] = "2.3";

            return codec.Encode();
        }
        /// <summary>
        /// ShortcutExpressCheckout: The method that calls SetExpressCheckout API
        /// </summary>
        /// <param name="amt"></param>
        /// <param ref name="token"></param>
        /// <param ref name="retMsg"></param>
        /// <returns></returns>
        public bool ShortcutExpressCheckout(string amt, ref string token, ref string retMsg)
        {
            string host = "www.paypal.com";
            if (bSandbox)
            {
                pendpointurl = "https://api-3t.sandbox.paypal.com/nvp";
                host = "www.sandbox.paypal.com";
            }

            //string returnURL = "http://dev.osmosource.fr/ShoppingCart/Confirmation";
            //string cancelURL = "http://dev.osmosource.fr/";

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "SetExpressCheckout";
            encoder["RETURNURL"] = returnURL;
            encoder["CANCELURL"] = cancelURL;
            encoder["AMT"] = amt;
            encoder["PAYMENTACTION"] = "Sale";
            encoder["CURRENCYCODE"] = "EUR";

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                token = decoder["TOKEN"];

                string ECURL = "https://" + host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token;

                retMsg = ECURL;
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                         "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                         "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }
        /// <summary>
        /// MarkExpressCheckout: The method that calls SetExpressCheckout API, invoked from the 
        /// Billing Page EC placement
        /// </summary>
        /// <param name="amt"></param>
        /// <param ref name="token"></param>
        /// <param ref name="retMsg"></param>
        /// <returns></returns>
        public bool MarkExpressCheckout(string amt, 
                                        string shipToName, string shipToStreet, string shipToStreet2,
                                        string shipToCity, string shipToState, string shipToZip, 
                                        string shipToCountryCode,ref string token, ref string retMsg)
        {
            string host = "www.paypal.com";
            if (bSandbox)
            {
                pendpointurl = "https://api-3t.sandbox.paypal.com/nvp";
                host = "www.sandbox.paypal.com";
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "SetExpressCheckout";
            encoder["RETURNURL"] = returnURL;
            encoder["CANCELURL"] = cancelURL;
            encoder["AMT"] = amt;
            encoder["PAYMENTACTION"] = "Sale";
            encoder["CURRENCYCODE"]  = "EUR";

            //Optional Shipping Address entered on the merchant site
            encoder["SHIPTONAME"]       = shipToName;
            encoder["SHIPTOSTREET"]     = shipToStreet;
            encoder["SHIPTOSTREET2"]    = shipToStreet2;
            encoder["SHIPTOCITY"]       = shipToCity;
            encoder["SHIPTOSTATE"]      = shipToState;
            encoder["SHIPTOZIP"]        = shipToZip;
            encoder["SHIPTOCOUNTRYCODE"]= shipToCountryCode;

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall(pStrrequestforNvp);

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                token = decoder["TOKEN"];

                string ECURL = "https://" + host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token;

                retMsg = ECURL;
                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                         "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                         "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }
        /// <summary>
        /// GetShippingDetails: The method that calls SetExpressCheckout API, invoked from the 
        /// Billing Page EC placement
        /// </summary>
        /// <param name="token"></param>
        /// <param ref name="retMsg"></param>
        /// <returns></returns>
        public bool GetShippingDetails(string token, ref string PayerId, ref string ShippingAddress, ref string retMsg)
        {
            if (bSandbox)
            {
                pendpointurl = "https://api-3t.sandbox.paypal.com/nvp";
            }

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "GetExpressCheckoutDetails";
            encoder["TOKEN"] = token;

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = HttpCall( pStrrequestforNvp );

            NVPCodec decoder = new NVPCodec();
            decoder.Decode( pStresponsenvp );

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                ShippingAddress = "<table><tr>";
                ShippingAddress += "<td> First Name </td><td>" + decoder["FIRSTNAME"] + "</td></tr>";
                ShippingAddress += "<td> Last Name </td><td>" + decoder["LASTNAME"] + "</td></tr>";
                ShippingAddress += "<td colspan='2'> Shipping Address</td></tr>";
                ShippingAddress += "<td> Name </td><td>" + decoder["SHIPTONAME"] + "</td></tr>";
                ShippingAddress += "<td> Street1 </td><td>" + decoder["SHIPTOSTREET"] + "</td></tr>";
                ShippingAddress += "<td> Street2 </td><td>" + decoder["SHIPTOSTREET2"] + "</td></tr>";
                ShippingAddress += "<td> City </td><td>" + decoder["SHIPTOCITY"] + "</td></tr>";
                ShippingAddress += "<td> State </td><td>" + decoder["SHIPTOSTATE"] + "</td></tr>";
                ShippingAddress += "<td> Zip </td><td>" + decoder["SHIPTOZIP"] + "</td>";
                ShippingAddress += "</tr>";

                return true;
            }
            else
            {
                retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" +
                         "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" +
                         "Desc2=" + decoder["L_LONGMESSAGE0"];

                return false;
            }
        }