コード例 #1
0
ファイル: PayPalController.cs プロジェクト: vcardins/CoolApp
 public ActionResult Index()
 {
     var payment = new Payment
         {
             SecundaryReceiver = "*****@*****.**",
             Amount = 100.00M,
             PrimaryReceiver = "*****@*****.**",
             ReturnCancelURL = "http://localhost:26927/PayPal",
             ReturnURL = "http://localhost:26927/PayPal/Success"
         };
     return View(payment);
 }
コード例 #2
0
ファイル: PayPalController.cs プロジェクト: vcardins/CoolApp
        public ActionResult PayPreapproved(Payment payment)
        {
            var userId = UserProfile.Current.UserId;
            _preApprovalService = DependencyResolver.Current.GetService<IPreApprovalService>();

            //var userPreApproval = _preApprovalService.Find(x => x.Userd == userId && x.ContractEndDate >= DateTime.Now && x.ContractStartDate <= DateTime.Now).FirstOrDefault();

            var payPal = new PayPalHelper.PayPalHelper();

            //if (userPreApproval != null)
            //{
            //    var userActualPreApproval = payPal.PreapprovalDetailsAPIOperation(userPreApproval.PreApprovalKey);
            //    userPreApproval.Approved = userActualPreApproval.approved != null && userActualPreApproval.approved.Value;
            //    userPreApproval.ContractStartDate = DateTime.Parse(userActualPreApproval.startingDate);
            //    userPreApproval.ContractEndDate = DateTime.Parse(userActualPreApproval.endingDate);
            //    _preApprovalService.SaveOrUpdate(userPreApproval);

            //    if(!userPreApproval.Approved)
            //    {
            //        return View("Error");
            //    }
            //}
            //else
            //{
            //    return View("Error");
            //}

            var preApprovalKeys = new Dictionary<int, string>();
            preApprovalKeys.Add(2, "PA-7RR891314Y878192Y");
            preApprovalKeys.Add(3, "");
            string preApprovalKey = "";
            preApprovalKeys.TryGetValue(userId, out preApprovalKey);
            payment.PreapprovalKey = preApprovalKey;

            var responsePay = payPal.GeneratePreapprovalPayment(payment);
            if (responsePay != null)
            {
                // # Success values
                if (responsePay.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
                {
                    payment.PayKey = responsePay.payKey;
                    return View("Success");
                }
                // # Error Values
                //List<ErrorData> errorMessages = responsePay.error;
                return View("Error");
            }
            return View("Error");
        }
コード例 #3
0
ファイル: PayPalController.cs プロジェクト: vcardins/CoolApp
 public ActionResult Pay(Payment payment)
 {
     var payPal = new PayPalHelper.PayPalHelper();
     var responsePay = payPal.GeneratePayment(payment);
     if (responsePay != null)
     {
         // # Success values
         if (responsePay.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
         {
             payment.PayKey = responsePay.payKey;
             return View(payment);
         }
         // # Error Values
         //List<ErrorData> errorMessages = responsePay.error;
         return View("Error");
     }
     return View("Error");
 }
コード例 #4
0
ファイル: PayPalController.cs プロジェクト: vcardins/CoolApp
 public ActionResult ConfigurePreappoval(Payment payment)
 {
     var payPal = new PayPalHelper.PayPalHelper();
     var preapproval = new Preapproval { CancelURL = payment.ReturnCancelURL, ReturnURL = payment.ReturnURL, IPNHost = "http://localhost:26927/PayPal/IPN" };
     var responsePreapproval = payPal.GeneratePreapproval(preapproval);
     if (responsePreapproval != null)
     {
         // # Success values
         if (responsePreapproval.responseEnvelope != null && responsePreapproval.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
         {
             preapproval.PreapprovalKey = responsePreapproval.preapprovalKey;
             return View(preapproval);
         }
         // # Error Values
         //List<ErrorData> errorMessages = responsePreapproval.error;
         return View("Error");
     }
     return View("Error");
 }
コード例 #5
0
        public PayResponse GeneratePreapprovalPayment(Payment payment)
        {
            // API endpoint for the Refund call in the Sandbox
            const string sAPIEndpoint = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay";

            // Version that you are coding against
            const string sVersion = "1.1.0";

            // Error Langugage
            const string sErrorLangugage = "en_US";

            // Detail Level
            //string sDetailLevel = "ReturnAll";

            // Request Data Binding
            const string sRequestDataBinding = "NV";

            // Response Data Binding
            const string sResponseDataBinding = "NV";

            // Application ID
            const string sAppId = "APP-80W284485P519543T";

            //PayPal credentials
            const string userId = "daniel.smoreira-facilitator_api1.outlook.com";
            const string pass = "******";
            const string signature = "AQU0e5vuZCvSg-XJploSa.sGUDlpAT7Ecw66HFdR0xOK6mL90N8vFFp3";

            const string sCurrencyCode = "USD";                                         // Currency Code
            const string sActionType = "PAY";                                           // Action Type
            string sReturnURL = payment.ReturnURL;                                      // ReturnURL and CancelURL used for approval flow
            string sCancelURL = payment.ReturnCancelURL;                                // ReturnURL and CancelURL used for approval flow
            const string sFeesPayer = "PRIMARYRECEIVER";                                   // who pays the fees
            string sMemo = payment.Memo;                                                // memo field
            string primaryReceiverAmount = payment.Amount.ToString(CultureInfo.InvariantCulture);      // transaction amount
            string secundayReceiverAmount = payment.Amount.ToString(CultureInfo.InvariantCulture);     // transaction amount
            string receiver = payment.PrimaryReceiver;                                  //transaction receive
            string feeReceiver = payment.SecundaryReceiver;                             //transaction receive
            string sPreapprovalKey = payment.PreapprovalKey;                            //preapprovalKey
            string sender = payment.Sender;                                             //transaction sender

            var client = new RestClient(sAPIEndpoint);

            var request = new RestRequest(Method.POST) {RequestFormat = DataFormat.Json};

            request.AddHeader("X-PAYPAL-SECURITY-USERID", userId);
            request.AddHeader("X-PAYPAL-SECURITY-PASSWORD", pass);
            request.AddHeader("X-PAYPAL-SECURITY-SIGNATURE", signature);
            request.AddHeader("X-PAYPAL-DEVICE-IPADDRESS", sVersion);
            request.AddHeader("X-PAYPAL-APPLICATION-ID", sAppId);
            request.AddHeader("X-PAYPAL-REQUEST-DATA-FORMAT", sRequestDataBinding);
            request.AddHeader("X-PAYPAL-RESPONSE-DATA-FORMAT", sResponseDataBinding);

            request.AddParameter("actionType", sActionType);
            request.AddParameter("currencyCode", sCurrencyCode);
            request.AddParameter("feesPayer", sFeesPayer);
            request.AddParameter("memo", sMemo);
            request.AddParameter("preapprovalKey", sPreapprovalKey);
            request.AddParameter("receiverList.receiver(0).amount", primaryReceiverAmount);
            request.AddParameter("receiverList.receiver(0).email", receiver);
            request.AddParameter("receiverList.receiver(0).primary", "true");
            request.AddParameter("receiverList.receiver(1).amount", secundayReceiverAmount);
            request.AddParameter("receiverList.receiver(1).email", feeReceiver);
            request.AddParameter("senderEmail", sender);
            request.AddParameter("returnUrl", sReturnURL);
            request.AddParameter("cancelUrl", sCancelURL);
            request.AddParameter("requestEnvelope.errorLanguage", sErrorLangugage);

            var result = client.Execute(request);

            if (result.ErrorException != null)
            {
                var twilioException = new ApplicationException(result.ErrorMessage, result.ErrorException);
                throw twilioException;
            }

            NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(result.Content);

            var paymentReturnKeys = new List<string>
                {
                    "responseEnvelope.timestamp",
                    "responseEnvelope.ack",
                    "responseEnvelope.correlationId",
                    "responseEnvelope.build",
                    "payKey",
                    "paymentExecStatus"
                };

            var listAckCodes = new List<AckCode>
                {
                    AckCode.FAILUREWITHWARNING,
                    AckCode.WARNING,
                    AckCode.FAILURE,
                    AckCode.SUCCESS,
                    AckCode.SUCCESSWITHWARNING
                };

            var payResponse = new PayResponse
                {
                    responseEnvelope = new ResponseEnvelope
                        {
                            timestamp = queryString[paymentReturnKeys[0]],
                            ack = listAckCodes.FirstOrDefault(x => x.ToString() == queryString[paymentReturnKeys[1]]),
                            correlationId = queryString[paymentReturnKeys[2]],
                            build = queryString[paymentReturnKeys[3]]
                        },
                    payKey = queryString[paymentReturnKeys[4]],
                    paymentExecStatus = queryString[paymentReturnKeys[5]]
                };

            return payResponse;
        }
コード例 #6
0
 public PayResponse GeneratePayment(Payment payment)
 {
     var request = CreatePayment(payment);
     return PayApiOperations(request);
 }
コード例 #7
0
        private PayRequest CreatePayment(Payment payment)
        {
            // # PayRequest
            // The code for the language in which errors are returned
            var envelopeRequest = new RequestEnvelope {errorLanguage = "en_US"};

            var listReceiver = new List<Receiver>();

            // Amount to be credited to the receiver's account
            var amountPrimary = payment.Amount * 0.98M;
            var amountSecundary = payment.Amount * 0.02M;

            var receivePrimary = new Receiver(amountPrimary) {email = payment.SecundaryReceiver};
            var receiveSecundary = new Receiver(amountSecundary) { email = "*****@*****.**" };

            // A receiver's email address
            listReceiver.Add(receivePrimary);
            listReceiver.Add(receiveSecundary);
            var listOfReceivers = new ReceiverList(listReceiver);

            // PayRequest which takes mandatory params:
            //
            // * `Request Envelope` - Information common to each API operation, such
            // as the language in which an error message is returned.
            // * `Action Type` - The action for this request. Possible values are:
            // * PAY - Use this option if you are not using the Pay request in
            // combination with ExecutePayment.
            // * CREATE - Use this option to set up the payment instructions with
            // SetPaymentOptions and then execute the payment at a later time with
            // the ExecutePayment.
            // * PAY_PRIMARY - For chained payments only, specify this value to delay
            // payments to the secondary receivers; only the payment to the primary
            // receiver is processed.
            // * `Cancel URL` - URL to redirect the sender's browser to after
            // canceling the approval for a payment; it is always required but only
            // used for payments that require approval (explicit payments)
            // * `Currency Code` - The code for the currency in which the payment is
            // made; you can specify only one currency, regardless of the number of
            // receivers
            // * `Recevier List` - List of receivers
            // * `Return URL` - URL to redirect the sender's browser to after the
            // sender has logged into PayPal and approved a payment; it is always
            // required but only used if a payment requires explicit approval
            var requestPay = new PayRequest(envelopeRequest, "PAY", payment.ReturnCancelURL, "USD", listOfReceivers, payment.ReturnURL);
            return requestPay;
        }