Hammad Nasir
Hammad Nasir

Reputation: 145

paypal express checkout integration payer id is null

i am integrating express checkout in my asp.net mvc application. everything works ok even, response is success but when i try to call "GetExpressCheckoutDetailsResponseDetails" i am getting null in "PayerID". The field below is "requestDetails.PayerID"

 public ActionResult PayPalExpressCheckout()
    {

        PaymentDetailsType paymentDetail = new PaymentDetailsType();
        CurrencyCodeType currency = (CurrencyCodeType)EnumUtils.GetValue("GBP", typeof(CurrencyCodeType));

        List<PaymentDetailsItemType> paymentItems = new List<PaymentDetailsItemType>();


        var AppCart = GetAppCart();

        foreach(var item in AppCart.Items)
        {
            PaymentDetailsItemType paymentItem = new PaymentDetailsItemType();
            paymentItem.Name = item.Name;
            paymentItem.Description = item.Description;
            double itemAmount = Convert.ToDouble(item.Price);
            paymentItem.Amount = new BasicAmountType(CurrencyCodeType.GBP, itemAmount.ToString());                
            paymentItem.Quantity = 1;                

            paymentItems.Add(paymentItem);

        }

        paymentDetail.PaymentDetailsItem = paymentItems;

        paymentDetail.PaymentAction = (PaymentActionCodeType)EnumUtils.GetValue("Sale", typeof(PaymentActionCodeType));
        paymentDetail.OrderTotal = new BasicAmountType(CurrencyCodeType.GBP, (AppCart.TotalPrice).ToString()); 
        List<PaymentDetailsType> paymentDetails = new List<PaymentDetailsType>();
        paymentDetails.Add(paymentDetail);

        SetExpressCheckoutRequestDetailsType ecDetails = new SetExpressCheckoutRequestDetailsType();
        ecDetails.ReturnURL = "http://Orchard.Club/Purchase/PayPalExpressCheckoutAuthorisedSuccess";
        ecDetails.CancelURL = "http://Orchard.Club/Purchase/CancelPayPalTransaction";
        ecDetails.PaymentDetails = paymentDetails;

        SetExpressCheckoutRequestType request = new SetExpressCheckoutRequestType();
        ecDetails.FundingSourceDetails = new FundingSourceDetailsType();            
        //request.Version = "104.0";            
        ecDetails.LandingPage = LandingPageType.BILLING;
        ecDetails.SolutionType = SolutionTypeType.SOLE;                       
        ecDetails.FundingSourceDetails.UserSelectedFundingSource = UserSelectedFundingSourceType.CREDITCARD;
        request.SetExpressCheckoutRequestDetails = ecDetails;


        SetExpressCheckoutReq wrapper = new SetExpressCheckoutReq();
        wrapper.SetExpressCheckoutRequest = request;
        Dictionary<string, string> sdkConfig = new Dictionary<string, string>();
        sdkConfig.Add("mode", "sandbox");
        sdkConfig.Add("account1.apiUsername", "mrhammad-facilitator_api1.hotmail.com");
        sdkConfig.Add("account1.apiPassword", "1369812511");
        sdkConfig.Add("account1.apiSignature", "AJxdrs7c7cXRinyNUS5p1V4s1m4uAGR.wOJ7KzgkewEYmTOOtHrPgSxR");
        PayPalAPIInterfaceServiceService service = new  PayPalAPIInterfaceServiceService(sdkConfig); 

        SetExpressCheckoutResponseType setECResponse = service.SetExpressCheckout(wrapper);


        if (setECResponse.Ack.Equals(AckCodeType.SUCCESS))
        {
            GetExpressCheckoutDetailsReq getECWrapper = new GetExpressCheckoutDetailsReq();
            // (Required) A timestamped token, the value of which was returned by SetExpressCheckout response.
            // Character length and limitations: 20 single-byte characters
            getECWrapper.GetExpressCheckoutDetailsRequest = new GetExpressCheckoutDetailsRequestType(setECResponse.Token);
            // # API call 
            // Invoke the GetExpressCheckoutDetails method in service wrapper object
            GetExpressCheckoutDetailsResponseType getECResponse = service.GetExpressCheckoutDetails(getECWrapper);

            // Create request object
            DoExpressCheckoutPaymentRequestType expressrequest = new DoExpressCheckoutPaymentRequestType();
            DoExpressCheckoutPaymentRequestDetailsType requestDetails = new DoExpressCheckoutPaymentRequestDetailsType();
            expressrequest.DoExpressCheckoutPaymentRequestDetails = requestDetails;

            requestDetails.PaymentDetails = getECResponse.GetExpressCheckoutDetailsResponseDetails.PaymentDetails;
            // (Required) The timestamped token value that was returned in the SetExpressCheckout response and passed in the GetExpressCheckoutDetails request.
            requestDetails.Token = setECResponse.Token;
            // (Required) Unique PayPal buyer account identification number as returned in the GetExpressCheckoutDetails response
            requestDetails.PayerID = requestDetails.PayerID;
            // (Required) How you want to obtain payment. It is one of the following values:
            // * Authorization – This payment is a basic authorization subject to settlement with PayPal Authorization and Capture.
            // * Order – This payment is an order authorization subject to settlement with PayPal Authorization and Capture.
            // * Sale – This is a final sale for which you are requesting payment.
            // Note: You cannot set this value to Sale in the SetExpressCheckout request and then change this value to Authorization in the DoExpressCheckoutPayment request.
            requestDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), "SALE");                

            // Invoke the API
            DoExpressCheckoutPaymentReq expresswrapper = new DoExpressCheckoutPaymentReq();
            expresswrapper.DoExpressCheckoutPaymentRequest = expressrequest;
            // # API call 
            // Invoke the DoExpressCheckoutPayment method in service wrapper object
            DoExpressCheckoutPaymentResponseType doECResponse = service.DoExpressCheckoutPayment(expresswrapper);
            // Check for API return status

            if (doECResponse.Ack.Equals(AckCodeType.FAILURE) ||
                (doECResponse.Errors != null && doECResponse.Errors.Count > 0))
            {                                
                return RedirectToAction("PostPaymentFailure");
            }
            else
            {
               TempData["TransactionResult"] = "Transaction ID:" + doECResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID + Environment.NewLine + "Payment status" + doECResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus.Value.ToString();
                return RedirectToAction("SaveCustomer", "SignupOrLogin");
            }    
        }
        else
        {
            return RedirectToAction("Error", "Purchase");
        }
    }

Upvotes: 0

Views: 1483

Answers (1)

Jason Z
Jason Z

Reputation: 874

As @geewiz mentioned, you're missing the step of redirecting the customer to PayPal to approve the payment.

Refer to How to Create One-Time Payments Using Express Checkout guide on PayPal Developer that outlines the steps involved with Express Checkout.

In your code, you will want to retrieve the EC token to use for the redirect from the setECResponse object and then redirect the customer to the PayPal site using that token:

SetExpressCheckoutResponseType setECResponse = service.SetExpressCheckout(wrapper);

// Note: Add appropriate logic for targeting live URL based on your config settings
var redirectUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + setECResponse.Token;

Upvotes: 1

Related Questions