paypal express checkout integration payer id为null

时间:2015-03-03 06:16:44

标签: c# asp.net paypal paypal-sandbox

我正在我的asp.net mvc应用程序中集成express checkout。一切都运作正常,响应是成功,但当我尝试呼叫" GetExpressCheckoutDetailsResponseDetails"我在" PayerID"中得到零。下面的字段是" 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");
        }
    }

1 个答案:

答案 0 :(得分:0)

正如@geewiz所提到的,您错过了将客户重定向到PayPal以批准付款的步骤。

请参阅PayPal Developer上的How to Create One-Time Payments Using Express Checkout指南,其中概述了快速结账的相关步骤。

在您的代码中,您需要检索用于来自setECResponse对象的重定向的EC令牌,然后使用该令牌将客户重定向到PayPal站点:

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;