/// <summary>
 /// Constructor with arguments
 /// </summary>
 public DoReferenceTransactionRequestType(DoReferenceTransactionRequestDetailsType doReferenceTransactionRequestDetails)
 {
     this.DoReferenceTransactionRequestDetails = doReferenceTransactionRequestDetails;
 }
        private void populateRequestObject(DoReferenceTransactionRequestType request)
        {
            DoReferenceTransactionRequestDetailsType referenceTransactionDetails = new DoReferenceTransactionRequestDetailsType();
            request.DoReferenceTransactionRequestDetails = referenceTransactionDetails;
            referenceTransactionDetails.ReferenceID = referenceId.Value;
            referenceTransactionDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), paymentAction.SelectedValue);

            // Populate payment requestDetails.
            PaymentDetailsType paymentDetails = new PaymentDetailsType();
            referenceTransactionDetails.PaymentDetails = paymentDetails;
            double orderTotal = 0.0;
            double itemTotal = 0.0;
            CurrencyCodeType currency = (CurrencyCodeType)
                Enum.Parse(typeof(CurrencyCodeType), currencyCode.SelectedValue);

            if (shippingTotal.Value != "")
            {
                paymentDetails.ShippingTotal = new BasicAmountType(currency, shippingTotal.Value);
                orderTotal += Double.Parse(shippingTotal.Value);
            }
            if (insuranceTotal.Value != "")
            {
                paymentDetails.InsuranceTotal = new BasicAmountType(currency, insuranceTotal.Value);
                paymentDetails.InsuranceOptionOffered = "true";
                orderTotal += Double.Parse(insuranceTotal.Value);
            }
            if (handlingTotal.Value != "")
            {
                paymentDetails.HandlingTotal = new BasicAmountType(currency, handlingTotal.Value);
                orderTotal += Double.Parse(handlingTotal.Value);
            }
            if (taxTotal.Value != "")
            {
                paymentDetails.TaxTotal = new BasicAmountType(currency, taxTotal.Value);
                orderTotal += Double.Parse(taxTotal.Value);
            }
            if (orderDescription.Value != "")
            {
                paymentDetails.OrderDescription = orderDescription.Value;
            }
            paymentDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), paymentAction.SelectedValue);

            // Each payment can include requestDetails about multiple payment items
            // This example shows just one payment item
            if (itemName.Value != null && itemAmount.Value != null && itemQuantity.Value != null)
            {
                PaymentDetailsItemType itemDetails = new PaymentDetailsItemType();
                itemDetails.Name = itemName.Value;
                itemDetails.Amount = new BasicAmountType(currency, itemAmount.Value);
                itemDetails.Quantity = Int32.Parse(itemQuantity.Value);
                itemDetails.ItemCategory = (ItemCategoryType)
                    Enum.Parse(typeof(ItemCategoryType), itemCategory.SelectedValue);
                itemTotal += Double.Parse(itemDetails.Amount.value) * itemDetails.Quantity.Value;
                if (salesTax.Value != "")
                {
                    itemDetails.Tax = new BasicAmountType(currency, salesTax.Value);
                    orderTotal += Double.Parse(salesTax.Value);
                }
                if (itemDescription.Value != "")
                {
                    itemDetails.Description = itemDescription.Value;
                }
                paymentDetails.PaymentDetailsItem.Add(itemDetails);
            }
            orderTotal += itemTotal;
            paymentDetails.ItemTotal = new BasicAmountType(currency, itemTotal.ToString());
            paymentDetails.OrderTotal = new BasicAmountType(currency, orderTotal.ToString());
        }
        private void populateRequestObject(DoReferenceTransactionRequestType request)
        {
            DoReferenceTransactionRequestDetailsType referenceTransactionDetails = new DoReferenceTransactionRequestDetailsType();
            request.DoReferenceTransactionRequestDetails = referenceTransactionDetails;
            // (Required) A transaction ID from a previous purchase, such as a credit card charge using the DoDirectPayment API, or a billing agreement ID.
            referenceTransactionDetails.ReferenceID = referenceId.Value;
            // (Optional) 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.
            // * Sale – This is a final sale for which you are requesting payment.
            referenceTransactionDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), paymentAction.SelectedValue);

            // Populate payment requestDetails.
            PaymentDetailsType paymentDetails = new PaymentDetailsType();
            referenceTransactionDetails.PaymentDetails = paymentDetails;
            // (Required) The total cost of the transaction to the buyer. If shipping cost and tax charges are known, include them in this value. If not, this value should be the current subtotal of the order. If the transaction includes one or more one-time purchases, this field must be equal to the sum of the purchases. Set this field to 0 if the transaction does not include a one-time purchase such as when you set up a billing agreement for a recurring payment that is not immediately charged. When the field is set to 0, purchase-specific fields are ignored.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            double orderTotal = 0.0;
            // (Optional) Sum of cost of all items in this order.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            double itemTotal = 0.0;
            CurrencyCodeType currency = (CurrencyCodeType)
                Enum.Parse(typeof(CurrencyCodeType), currencyCode.SelectedValue);

            // (Optional) Total shipping costs for this order.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            if (shippingTotal.Value != string.Empty)
            {
                paymentDetails.ShippingTotal = new BasicAmountType(currency, shippingTotal.Value);
                orderTotal += Convert.ToDouble(shippingTotal.Value);
            }
            // (Optional) Total handling costs for this order.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            if (handlingTotal.Value != string.Empty)
            {
                paymentDetails.HandlingTotal = new BasicAmountType(currency, handlingTotal.Value);
                orderTotal += Convert.ToDouble(handlingTotal.Value);
            }
            // (Optional) Sum of tax for all items in this order.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            if (taxTotal.Value != string.Empty)
            {
                paymentDetails.TaxTotal = new BasicAmountType(currency, taxTotal.Value);
                orderTotal += Convert.ToDouble(taxTotal.Value);
            }
            // (Optional) Description of items the buyer is purchasing.
            // Note: The value you specify is available only if the transaction includes a purchase. This field is ignored if you set up a billing agreement for a recurring payment that is not immediately charged.
            if (orderDescription.Value != string.Empty)
            {
                paymentDetails.OrderDescription = orderDescription.Value;
            }

            // Each payment can include requestDetails about multiple payment items
            // This example shows just one payment item
            if (itemName.Value != null && itemAmount.Value != null && itemQuantity.Value != null)
            {
                PaymentDetailsItemType itemDetails = new PaymentDetailsItemType();
                // Item name. This field is required when you pass a value for ItemCategory.
                itemDetails.Name = itemName.Value;
                // Cost of item. This field is required when you pass a value for ItemCategory.
                itemDetails.Amount = new BasicAmountType(currency, itemAmount.Value);
                // Item quantity. This field is required when you pass a value forItemCategory.
                itemDetails.Quantity = Convert.ToInt32(itemQuantity.Value);
                // Indicates whether the item is digital or physical. For digital goods, this field is required and you must set it to Digital to get the best rates. It is one of the following values:
                // * Digital
                // * Physical
                // This field is introduced in version 69.0.
                itemDetails.ItemCategory = (ItemCategoryType)
                    Enum.Parse(typeof(ItemCategoryType), itemCategory.SelectedValue);
                itemTotal += Convert.ToDouble(itemDetails.Amount.value) * itemDetails.Quantity.Value;
                //if (salesTax.Value != string.Empty)
                //{
                //    itemDetails.Tax = new BasicAmountType(currency, salesTax.Value);
                //    orderTotal += Convert.ToDouble(salesTax.Value);
                //}
                // (Optional) Item description.
                // This field is available since version 53.0.
                if (itemDescription.Value != string.Empty)
                {
                    itemDetails.Description = itemDescription.Value;
                }
                paymentDetails.PaymentDetailsItem.Add(itemDetails);
            }
            orderTotal += itemTotal;
            paymentDetails.ItemTotal = new BasicAmountType(currency, itemTotal.ToString());
            paymentDetails.OrderTotal = new BasicAmountType(currency, orderTotal.ToString());
            // (Optional) Your URL for receiving Instant Payment Notification (IPN) about this transaction. If you do not specify this value in the request, the notification URL from your Merchant Profile is used, if one exists.
            // Important:
            // The notify URL applies only to DoExpressCheckoutPayment. This value is ignored when set in SetExpressCheckout or GetExpressCheckoutDetails.
            paymentDetails.NotifyURL = ipnNotificationUrl.Value.Trim();
        }
    //#DoReferenceTransaction API Operation
    //The DoReferenceTransaction API operation processes a payment from a buyer’s account, which is identified by a previous transaction. 
    public DoReferenceTransactionResponseType DoReferenceTransactionAPIOperation()
    {
        DoReferenceTransactionResponseType responseDoReferenceTransactionResponseType = new DoReferenceTransactionResponseType();

        try
        {
            // Create the DoReferenceTransactionReq object
            DoReferenceTransactionReq doReferenceTransaction = new DoReferenceTransactionReq();

            // Information about the payment.
            PaymentDetailsType paymentDetails = new PaymentDetailsType();

            // The total cost of the transaction to the buyer. If shipping cost and
            // tax charges are known, include them in this value. If not, this value
            // should be the current subtotal of the order.

            // If the transaction includes one or more one-time purchases, this field must be equal to
            // the sum of the purchases. Set this field to 0 if the transaction does
            // not include a one-time purchase such as when you set up a billing
            // agreement for a recurring payment that is not immediately charged.
            // When the field is set to 0, purchase-specific fields are ignored
            //
            // * `Currency ID` - You must set the currencyID attribute to one of the
            // 3-character currency codes for any of the supported PayPal
            // currencies.
            // * `Amount`
            BasicAmountType orderTotal = new BasicAmountType(CurrencyCodeType.USD, "3.00");
            paymentDetails.OrderTotal = orderTotal;

            // IPN URL
            // * PayPal Instant Payment Notification is a call back system that is initiated when a transaction is completed        
            // * The transaction related IPN variables will be received on the call back URL specified in the request       
            // * The IPN variables have to be sent back to the PayPal system for validation, upon validation PayPal will send a response string "VERIFIED" or "INVALID"     
            // * PayPal would continuously resend IPN if a wrong IPN is sent        
            paymentDetails.NotifyURL = "http://IPNhost";

            // `DoReferenceTransactionRequestDetails` takes mandatory params:
            //
            // * `Reference Id` - A transaction ID from a previous purchase, such as a
            // credit card charge using the DoDirectPayment API, or a billing
            // agreement ID.
            // * `Payment Action Code` - How you want to obtain payment. It is one of
            // the following values:
            // * Authorization
            // * Sale
            // * Order
            // * None
            // * `Payment Details`
            DoReferenceTransactionRequestDetailsType doReferenceTransactionRequestDetails
                = new DoReferenceTransactionRequestDetailsType("97U72738FY126561H", PaymentActionCodeType.SALE, paymentDetails);
            DoReferenceTransactionRequestType doReferenceTransactionRequest = new DoReferenceTransactionRequestType(doReferenceTransactionRequestDetails);
            doReferenceTransaction.DoReferenceTransactionRequest = doReferenceTransactionRequest;

            // Create the service wrapper object to make the API call
            PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();

            // # API call
            // Invoke the DoReferenceTransaction method in service wrapper object
            responseDoReferenceTransactionResponseType = service.DoReferenceTransaction(doReferenceTransaction);

            if (responseDoReferenceTransactionResponseType != null)
            {
                // Response envelope acknowledgement
                string acknowledgement = "DoReferenceTransaction API Operation - ";
                acknowledgement += responseDoReferenceTransactionResponseType.Ack.ToString();
                logger.Info(acknowledgement + "\n");
                Console.WriteLine(acknowledgement + "\n");

                // # Success values
                if (responseDoReferenceTransactionResponseType.Ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
                {
                    // The final amount charged, including any shipping and taxes from your Merchant Profile
                    logger.Info("Amount : " + responseDoReferenceTransactionResponseType.DoReferenceTransactionResponseDetails.Amount.currencyID
                        + " " + responseDoReferenceTransactionResponseType.DoReferenceTransactionResponseDetails.Amount.value + "\n");
                    Console.WriteLine("Amount : " + responseDoReferenceTransactionResponseType.DoReferenceTransactionResponseDetails.Amount.currencyID
                        + " " + responseDoReferenceTransactionResponseType.DoReferenceTransactionResponseDetails.Amount.value + "\n");
                }
                // # Error Values
                else
                {
                    List<ErrorType> errorMessages = responseDoReferenceTransactionResponseType.Errors;
                    foreach (ErrorType error in errorMessages)
                    {
                        logger.Debug("API Error Message : " + error.LongMessage);
                        Console.WriteLine("API Error Message : " + error.LongMessage + "\n");
                    }
                }
            }
        }
        // # Exception log    
        catch (System.Exception ex)
        {
            // Log the exception message       
            logger.Debug("Error Message : " + ex.Message);
            Console.WriteLine("Error Message : " + ex.Message);
        }
        return responseDoReferenceTransactionResponseType;
    }