/// <summary> /// Cancels a recurring payment /// </summary> /// <param name="cancelPaymentRequest">Request</param> /// <returns>Result</returns> public override CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest request) { var result = new CancelRecurringPaymentResult(); var order = request.Order; var settings = CommonServices.Settings.LoadSetting <TSetting>(order.StoreId); var req = new ManageRecurringPaymentsProfileStatusReq(); req.ManageRecurringPaymentsProfileStatusRequest = new ManageRecurringPaymentsProfileStatusRequestType(); req.ManageRecurringPaymentsProfileStatusRequest.Version = PayPalHelper.GetApiVersion(); var details = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); req.ManageRecurringPaymentsProfileStatusRequest.ManageRecurringPaymentsProfileStatusRequestDetails = details; details.Action = StatusChangeActionType.Cancel; //Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response details.ProfileID = order.SubscriptionTransactionId; using (var service = new PayPalAPIAASoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(settings); var response = service.ManageRecurringPaymentsProfileStatus(req); string error = ""; if (!PayPalHelper.CheckSuccess(_helper, response, out error)) { result.AddError(error); } } return(result); }
/// <summary> /// Handles refund /// </summary> /// <param name="request">RefundPaymentRequest</param> /// <returns>RefundPaymentResult</returns> public override RefundPaymentResult Refund(RefundPaymentRequest request) { var result = new RefundPaymentResult(); string transactionId = request.Order.CaptureTransactionId; var req = new RefundTransactionReq(); req.RefundTransactionRequest = new RefundTransactionRequestType(); //NOTE: Specify amount in partial refund req.RefundTransactionRequest.RefundType = RefundType.Full; req.RefundTransactionRequest.RefundTypeSpecified = true; req.RefundTransactionRequest.Version = PayPalHelper.GetApiVersion(); req.RefundTransactionRequest.TransactionID = transactionId; using (var service = new PayPalAPISoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(Settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(Settings); RefundTransactionResponseType response = service.RefundTransaction(req); string error = string.Empty; bool Success = PayPalHelper.CheckSuccess(_helper, response, out error); if (Success) { result.NewPaymentStatus = PaymentStatus.Refunded; //cancelPaymentResult.RefundTransactionID = response.RefundTransactionID; } else { result.AddError(error); } } return(result); }
/// <summary> /// Voids a payment /// </summary> /// <param name="voidPaymentRequest">Request</param> /// <returns>Result</returns> public override VoidPaymentResult Void(VoidPaymentRequest request) { var result = new VoidPaymentResult() { NewPaymentStatus = request.Order.PaymentStatus }; string transactionId = request.Order.AuthorizationTransactionId; var settings = CommonServices.Settings.LoadSetting <TSetting>(request.Order.StoreId); if (String.IsNullOrEmpty(transactionId)) { transactionId = request.Order.CaptureTransactionId; } var req = new DoVoidReq(); req.DoVoidRequest = new DoVoidRequestType(); req.DoVoidRequest.Version = PayPalHelper.GetApiVersion(); req.DoVoidRequest.AuthorizationID = transactionId; using (var service = new PayPalAPIAASoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(settings); DoVoidResponseType response = service.DoVoid(req); string error = ""; bool success = PayPalHelper.CheckSuccess(_helper, response, out error); if (success) { result.NewPaymentStatus = PaymentStatus.Voided; //result.VoidTransactionID = response.RefundTransactionID; } else { result.AddError(error); } } return(result); }
/// <summary> /// Captures payment /// </summary> /// <param name="capturePaymentRequest">Capture payment request</param> /// <returns>Capture payment result</returns> public override CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest) { var result = new CapturePaymentResult() { NewPaymentStatus = capturePaymentRequest.Order.PaymentStatus }; var settings = CommonServices.Settings.LoadSetting <TSetting>(capturePaymentRequest.Order.StoreId); string authorizationId = capturePaymentRequest.Order.AuthorizationTransactionId; var req = new DoCaptureReq(); req.DoCaptureRequest = new DoCaptureRequestType(); req.DoCaptureRequest.Version = PayPalHelper.GetApiVersion(); req.DoCaptureRequest.AuthorizationID = authorizationId; req.DoCaptureRequest.Amount = new BasicAmountType(); req.DoCaptureRequest.Amount.Value = Math.Round(capturePaymentRequest.Order.OrderTotal, 2).ToString("N", new CultureInfo("en-us")); req.DoCaptureRequest.Amount.currencyID = (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), Helper.CurrencyCode, true); req.DoCaptureRequest.CompleteType = CompleteCodeType.Complete; using (var service = new PayPalAPIAASoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(settings); DoCaptureResponseType response = service.DoCapture(req); string error = ""; bool success = PayPalHelper.CheckSuccess(_helper, response, out error); if (success) { result.NewPaymentStatus = PaymentStatus.Paid; result.CaptureTransactionId = response.DoCaptureResponseDetails.PaymentInfo.TransactionID; result.CaptureTransactionResult = response.Ack.ToString(); } else { result.AddError(error); } } return(result); }
/// <summary> /// Process a payment /// </summary> /// <param name="processPaymentRequest">Payment info required for an order processing</param> /// <returns>Process payment result</returns> public override ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest) { var result = new ProcessPaymentResult(); var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId); var settings = CommonServices.Settings.LoadSetting <PayPalDirectPaymentSettings>(processPaymentRequest.StoreId); var req = new DoDirectPaymentReq(); req.DoDirectPaymentRequest = new DoDirectPaymentRequestType(); req.DoDirectPaymentRequest.Version = PayPalHelper.GetApiVersion(); var details = new DoDirectPaymentRequestDetailsType(); req.DoDirectPaymentRequest.DoDirectPaymentRequestDetails = details; details.IPAddress = CommonServices.WebHelper.GetCurrentIpAddress(); if (details.IPAddress.IsEmpty()) { details.IPAddress = "127.0.0.1"; } if (settings.TransactMode == TransactMode.Authorize) { details.PaymentAction = PaymentActionCodeType.Authorization; } else { details.PaymentAction = PaymentActionCodeType.Sale; } //credit card details.CreditCard = new CreditCardDetailsType(); details.CreditCard.CreditCardNumber = processPaymentRequest.CreditCardNumber; details.CreditCard.CreditCardType = PayPalHelper.GetPaypalCreditCardType(processPaymentRequest.CreditCardType); details.CreditCard.ExpMonthSpecified = true; details.CreditCard.ExpMonth = processPaymentRequest.CreditCardExpireMonth; details.CreditCard.ExpYearSpecified = true; details.CreditCard.ExpYear = processPaymentRequest.CreditCardExpireYear; details.CreditCard.CVV2 = processPaymentRequest.CreditCardCvv2; details.CreditCard.CardOwner = new PayerInfoType(); details.CreditCard.CardOwner.PayerCountry = PayPalHelper.GetPaypalCountryCodeType(customer.BillingAddress.Country); details.CreditCard.CreditCardTypeSpecified = true; //billing address details.CreditCard.CardOwner.Address = new AddressType(); details.CreditCard.CardOwner.Address.CountrySpecified = true; details.CreditCard.CardOwner.Address.Street1 = customer.BillingAddress.Address1; details.CreditCard.CardOwner.Address.Street2 = customer.BillingAddress.Address2; details.CreditCard.CardOwner.Address.CityName = customer.BillingAddress.City; if (customer.BillingAddress.StateProvince != null) { details.CreditCard.CardOwner.Address.StateOrProvince = customer.BillingAddress.StateProvince.Abbreviation; } else { details.CreditCard.CardOwner.Address.StateOrProvince = "CA"; } details.CreditCard.CardOwner.Address.Country = PayPalHelper.GetPaypalCountryCodeType(customer.BillingAddress.Country); details.CreditCard.CardOwner.Address.PostalCode = customer.BillingAddress.ZipPostalCode; details.CreditCard.CardOwner.Payer = customer.BillingAddress.Email; details.CreditCard.CardOwner.PayerName = new PersonNameType(); details.CreditCard.CardOwner.PayerName.FirstName = customer.BillingAddress.FirstName; details.CreditCard.CardOwner.PayerName.LastName = customer.BillingAddress.LastName; //order totals var payPalCurrency = PayPalHelper.GetPaypalCurrency(_currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)); details.PaymentDetails = new PaymentDetailsType(); details.PaymentDetails.OrderTotal = new BasicAmountType(); details.PaymentDetails.OrderTotal.Value = Math.Round(processPaymentRequest.OrderTotal, 2).ToString("N", new CultureInfo("en-us")); details.PaymentDetails.OrderTotal.currencyID = payPalCurrency; details.PaymentDetails.Custom = processPaymentRequest.OrderGuid.ToString(); details.PaymentDetails.ButtonSource = SmartStoreVersion.CurrentFullVersion; //shipping if (customer.ShippingAddress != null) { if (customer.ShippingAddress.StateProvince != null && customer.ShippingAddress.Country != null) { var shippingAddress = new AddressType(); shippingAddress.Name = customer.ShippingAddress.FirstName + " " + customer.ShippingAddress.LastName; shippingAddress.Street1 = customer.ShippingAddress.Address1; shippingAddress.CityName = customer.ShippingAddress.City; shippingAddress.StateOrProvince = customer.ShippingAddress.StateProvince.Abbreviation; shippingAddress.PostalCode = customer.ShippingAddress.ZipPostalCode; shippingAddress.Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), customer.ShippingAddress.Country.TwoLetterIsoCode, true); shippingAddress.CountrySpecified = true; details.PaymentDetails.ShipToAddress = shippingAddress; } } //send request using (var service = new PayPalAPIAASoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(settings); DoDirectPaymentResponseType response = service.DoDirectPayment(req); string error = ""; bool success = PayPalHelper.CheckSuccess(Helper, response, out error); if (success) { result.AvsResult = response.AVSCode; result.AuthorizationTransactionCode = response.CVV2Code; if (settings.TransactMode == TransactMode.Authorize) { result.AuthorizationTransactionId = response.TransactionID; result.AuthorizationTransactionResult = response.Ack.ToString(); result.NewPaymentStatus = PaymentStatus.Authorized; } else { result.CaptureTransactionId = response.TransactionID; result.CaptureTransactionResult = response.Ack.ToString(); result.NewPaymentStatus = PaymentStatus.Paid; } } else { result.AddError(error); } } return(result); }
/// <summary> /// Process recurring payment /// </summary> /// <param name="processPaymentRequest">Payment info required for an order processing</param> /// <returns>Process payment result</returns> public override ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest) { var result = new ProcessPaymentResult(); var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId); var settings = CommonServices.Settings.LoadSetting <PayPalDirectPaymentSettings>(processPaymentRequest.StoreId); var req = new CreateRecurringPaymentsProfileReq(); req.CreateRecurringPaymentsProfileRequest = new CreateRecurringPaymentsProfileRequestType(); req.CreateRecurringPaymentsProfileRequest.Version = PayPalHelper.GetApiVersion(); var details = new CreateRecurringPaymentsProfileRequestDetailsType(); req.CreateRecurringPaymentsProfileRequest.CreateRecurringPaymentsProfileRequestDetails = details; details.CreditCard = new CreditCardDetailsType(); details.CreditCard.CreditCardNumber = processPaymentRequest.CreditCardNumber; details.CreditCard.CreditCardType = PayPalHelper.GetPaypalCreditCardType(processPaymentRequest.CreditCardType); details.CreditCard.ExpMonthSpecified = true; details.CreditCard.ExpMonth = processPaymentRequest.CreditCardExpireMonth; details.CreditCard.ExpYearSpecified = true; details.CreditCard.ExpYear = processPaymentRequest.CreditCardExpireYear; details.CreditCard.CVV2 = processPaymentRequest.CreditCardCvv2; details.CreditCard.CardOwner = new PayerInfoType(); details.CreditCard.CardOwner.PayerCountry = PayPalHelper.GetPaypalCountryCodeType(customer.BillingAddress.Country); details.CreditCard.CreditCardTypeSpecified = true; details.CreditCard.CardOwner.Address = new AddressType(); details.CreditCard.CardOwner.Address.CountrySpecified = true; details.CreditCard.CardOwner.Address.Street1 = customer.BillingAddress.Address1; details.CreditCard.CardOwner.Address.Street2 = customer.BillingAddress.Address2; details.CreditCard.CardOwner.Address.CityName = customer.BillingAddress.City; if (customer.BillingAddress.StateProvince != null) { details.CreditCard.CardOwner.Address.StateOrProvince = customer.BillingAddress.StateProvince.Abbreviation; } else { details.CreditCard.CardOwner.Address.StateOrProvince = "CA"; } details.CreditCard.CardOwner.Address.Country = PayPalHelper.GetPaypalCountryCodeType(customer.BillingAddress.Country); details.CreditCard.CardOwner.Address.PostalCode = customer.BillingAddress.ZipPostalCode; details.CreditCard.CardOwner.Payer = customer.BillingAddress.Email; details.CreditCard.CardOwner.PayerName = new PersonNameType(); details.CreditCard.CardOwner.PayerName.FirstName = customer.BillingAddress.FirstName; details.CreditCard.CardOwner.PayerName.LastName = customer.BillingAddress.LastName; //start date details.RecurringPaymentsProfileDetails = new RecurringPaymentsProfileDetailsType(); details.RecurringPaymentsProfileDetails.BillingStartDate = DateTime.UtcNow; details.RecurringPaymentsProfileDetails.ProfileReference = processPaymentRequest.OrderGuid.ToString(); //schedule details.ScheduleDetails = new ScheduleDetailsType(); details.ScheduleDetails.Description = Helper.GetResource("RecurringPayment"); details.ScheduleDetails.PaymentPeriod = new BillingPeriodDetailsType(); details.ScheduleDetails.PaymentPeriod.Amount = new BasicAmountType(); details.ScheduleDetails.PaymentPeriod.Amount.Value = Math.Round(processPaymentRequest.OrderTotal, 2).ToString("N", new CultureInfo("en-us")); details.ScheduleDetails.PaymentPeriod.Amount.currencyID = PayPalHelper.GetPaypalCurrency(_currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId)); details.ScheduleDetails.PaymentPeriod.BillingFrequency = processPaymentRequest.RecurringCycleLength; switch (processPaymentRequest.RecurringCyclePeriod) { case RecurringProductCyclePeriod.Days: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Day; break; case RecurringProductCyclePeriod.Weeks: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Week; break; case RecurringProductCyclePeriod.Months: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Month; break; case RecurringProductCyclePeriod.Years: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Year; break; default: throw new SmartException(Helper.GetResource("NotSupportedPeriod")); } details.ScheduleDetails.PaymentPeriod.TotalBillingCycles = processPaymentRequest.RecurringTotalCycles; details.ScheduleDetails.PaymentPeriod.TotalBillingCyclesSpecified = true; using (var service = new PayPalAPIAASoapBinding()) { service.Url = PayPalHelper.GetPaypalServiceUrl(settings); service.RequesterCredentials = PayPalHelper.GetPaypalApiCredentials(settings); CreateRecurringPaymentsProfileResponseType response = service.CreateRecurringPaymentsProfile(req); string error = ""; bool success = PayPalHelper.CheckSuccess(Helper, response, out error); if (success) { result.NewPaymentStatus = PaymentStatus.Pending; if (response.CreateRecurringPaymentsProfileResponseDetails != null) { result.SubscriptionTransactionId = response.CreateRecurringPaymentsProfileResponseDetails.ProfileID; } } else { result.AddError(error); } } return(result); }