PageRenderTime 3646ms CodeModel.GetById 42ms RepoModel.GetById 7ms app.codeStats 0ms

/Merchant/CreateRecurringPaymentsProfileSample.cs

https://gitlab.com/CORP-RESELLER/codesamples-dotnet
C# | 223 lines | 68 code | 26 blank | 129 comment | 3 complexity | de5a3d5e252ba94cbb31f4ad4b867a7e MD5 | raw file
  1. // # Namespaces
  2. using System;
  3. using System.Collections.Generic;
  4. // # NuGet Install
  5. // Visual Studio 2012 and 2010 Command:
  6. // Install-Package PayPalMerchantSDK
  7. // Visual Studio 2005 and 2008 (NuGet.exe) Command:
  8. // install PayPalMerchantSDK
  9. using PayPal.PayPalAPIInterfaceService;
  10. using PayPal.PayPalAPIInterfaceService.Model;
  11. using log4net;
  12. // # Sample for CreateRecurringPaymentsProfile API
  13. // The CreateRecurringPaymentsProfile API operation creates a recurring
  14. // payments profile.
  15. // You must invoke the CreateRecurringPaymentsProfile API operation for each
  16. // profile you want to create. The API operation creates a profile and an
  17. // associated billing agreement.
  18. // `Note:
  19. // There is a one-to-one correspondence between billing agreements and
  20. // recurring payments profiles. To associate a recurring payments profile
  21. // with its billing agreement, you must ensure that the description in the
  22. // recurring payments profile matches the description of a billing
  23. // agreement. For version 54.0 and later, use SetExpressCheckout to initiate
  24. // creation of a billing agreement.`
  25. // This sample code uses Merchant .NET SDK to make API call. You can
  26. // download the SDKs [here](https://github.com/paypal/sdk-packages/tree/gh-pages/merchant-sdk/dotnet)
  27. public class CreateRecurringPaymentsProfileSample
  28. {
  29. // # Static constructor for configuration setting
  30. static CreateRecurringPaymentsProfileSample()
  31. {
  32. // Load the log4net configuration settings from Web.config or App.config
  33. log4net.Config.XmlConfigurator.Configure();
  34. }
  35. // Logs output statements, errors, debug info to a text file
  36. private static ILog logger = LogManager.GetLogger(typeof(CreateRecurringPaymentsProfileSample));
  37. // # CreateRecurringPaymentsProfile API Operation
  38. // The CreateRecurringPaymentsProfile API operation creates a recurring payments profile.
  39. // You must invoke the CreateRecurringPaymentsProfile API operation for each profile you want to create.
  40. // The API operation creates a profile and an associated billing agreement.
  41. // Note:
  42. // There is a one-to-one correspondence between billing agreements and recurring payments profiles.
  43. // To associate a recurring payments profile with its billing agreement,
  44. // you must ensure that the description in the recurring payments profile matches the description of a billing agreement.
  45. // For version 54.0 and later, use SetExpressCheckout to initiate creation of a billing agreement.
  46. public CreateRecurringPaymentsProfileResponseType CreateRecurringPaymentsProfileAPIOperation()
  47. {
  48. // Create the CreateRecurringPaymentsProfileResponseType object
  49. CreateRecurringPaymentsProfileResponseType responseCreateRecurringPaymentsProfileResponseType = new CreateRecurringPaymentsProfileResponseType();
  50. try
  51. {
  52. // Create the CreateRecurringPaymentsProfileReq object
  53. CreateRecurringPaymentsProfileReq createRecurringPaymentsProfile = new CreateRecurringPaymentsProfileReq();
  54. // Create the CreateRecurringPaymentsProfileRequestType object
  55. CreateRecurringPaymentsProfileRequestType createRecurringPaymentsProfileRequest = new CreateRecurringPaymentsProfileRequestType();
  56. // You can include up to 10 recurring payments profiles per request. The
  57. // order of the profile details must match the order of the billing
  58. // agreement details specified in the SetExpressCheckout request which
  59. // takes mandatory argument:
  60. //
  61. // * `billing start date` - The date when billing for this profile begins.
  62. // `Note:
  63. // The profile may take up to 24 hours for activation.`
  64. RecurringPaymentsProfileDetailsType recurringPaymentsProfileDetails
  65. = new RecurringPaymentsProfileDetailsType("2013-12-31T13:01:19+00:00");
  66. // Billing amount for each billing cycle during this payment period.
  67. // This amount does not include shipping and tax amounts.
  68. // `Note:
  69. // All amounts in the CreateRecurringPaymentsProfile request must have
  70. // the same currency.`
  71. BasicAmountType billingAmount = new BasicAmountType(CurrencyCodeType.USD, "3.00");
  72. // Regular payment period for this schedule which takes mandatory
  73. // params:
  74. //
  75. // * `Billing Period` - Unit for billing during this subscription period. It is one of the
  76. // following values:
  77. // * Day
  78. // * Week
  79. // * SemiMonth
  80. // * Month
  81. // * Year
  82. // For SemiMonth, billing is done on the 1st and 15th of each month.
  83. // `Note:
  84. // The combination of BillingPeriod and BillingFrequency cannot exceed
  85. // one year.`
  86. // * `Billing Frequency` - Number of billing periods that make up one billing cycle.
  87. // The combination of billing frequency and billing period must be less
  88. // than or equal to one year. For example, if the billing cycle is
  89. // Month, the maximum value for billing frequency is 12. Similarly, if
  90. // the billing cycle is Week, the maximum value for billing frequency is
  91. // 52.
  92. // `Note:
  93. // If the billing period is SemiMonth, the billing frequency must be 1.`
  94. // * `Billing Amount`
  95. BillingPeriodDetailsType paymentPeriod = new BillingPeriodDetailsType(BillingPeriodType.DAY, Convert.ToInt32("5"), billingAmount);
  96. // Describes the recurring payments schedule, including the regular
  97. // payment period, whether there is a trial period, and the number of
  98. // payments that can fail before a profile is suspended which takes
  99. // mandatory params:
  100. //
  101. // * `Description` - Description of the recurring payment.
  102. // `Note:
  103. // You must ensure that this field matches the corresponding billing
  104. // agreement description included in the SetExpressCheckout request.`
  105. // * `Payment Period`
  106. ScheduleDetailsType scheduleDetails = new ScheduleDetailsType("description", paymentPeriod);
  107. // `CreateRecurringPaymentsProfileRequestDetailsType` which takes
  108. // mandatory params:
  109. //
  110. // * `Recurring Payments Profile Details`
  111. // * `Schedule Details`
  112. CreateRecurringPaymentsProfileRequestDetailsType createRecurringPaymentsProfileRequestDetails
  113. = new CreateRecurringPaymentsProfileRequestDetailsType(recurringPaymentsProfileDetails, scheduleDetails);
  114. // Either EC token or a credit card number is required.If you include
  115. // both token and credit card number, the token is used and credit card number is
  116. // ignored
  117. // In case of setting EC token,
  118. // `createRecurringPaymentsProfileRequestDetails.Token = "EC-5KH01765D1724703R";`
  119. // A timestamped token, the value of which was returned in the response
  120. // to the first call to SetExpressCheckout. Call
  121. // CreateRecurringPaymentsProfile once for each billing
  122. // agreement included in SetExpressCheckout request and use the same
  123. // token for each call. Each CreateRecurringPaymentsProfile request
  124. // creates a single recurring payments profile.
  125. // `Note:
  126. // Tokens expire after approximately 3 hours.`
  127. // Credit card information for recurring payments using direct payments.
  128. CreditCardDetailsType creditCard = new CreditCardDetailsType();
  129. // Type of credit card. For UK, only Maestro, MasterCard, Discover, and
  130. // Visa are allowable. For Canada, only MasterCard and Visa are
  131. // allowable and Interac debit cards are not supported. It is one of the
  132. // following values:
  133. //
  134. // * Visa
  135. // * MasterCard
  136. // * Discover
  137. // * Amex
  138. // * Solo
  139. // * Switch
  140. // * Maestro: See note.
  141. // `Note:
  142. // If the credit card type is Maestro, you must set currencyId to GBP.
  143. // In addition, you must specify either StartMonth and StartYear or
  144. // IssueNumber.`
  145. creditCard.CreditCardType = CreditCardTypeType.VISA;
  146. // Credit Card Number
  147. creditCard.CreditCardNumber = "4442662639546634";
  148. // Credit Card Expiration Month
  149. creditCard.ExpMonth = Convert.ToInt32("12");
  150. // Credit Card Expiration Year
  151. creditCard.ExpYear = Convert.ToInt32("2016");
  152. createRecurringPaymentsProfileRequestDetails.CreditCard = creditCard;
  153. createRecurringPaymentsProfileRequest.CreateRecurringPaymentsProfileRequestDetails
  154. = createRecurringPaymentsProfileRequestDetails;
  155. createRecurringPaymentsProfile.CreateRecurringPaymentsProfileRequest = createRecurringPaymentsProfileRequest;
  156. // # Create the service wrapper object to make the API call
  157. PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
  158. // # API call
  159. // Invoke the CreateRecurringPaymentsProfile method
  160. responseCreateRecurringPaymentsProfileResponseType
  161. = service.CreateRecurringPaymentsProfile(createRecurringPaymentsProfile);
  162. if (responseCreateRecurringPaymentsProfileResponseType != null)
  163. {
  164. // Response envelope acknowledgement
  165. string acknowledgement = "CreateRecurringPaymentsProfile API Operation - ";
  166. acknowledgement += responseCreateRecurringPaymentsProfileResponseType.Ack.ToString();
  167. logger.Info(acknowledgement + "\n");
  168. Console.WriteLine(acknowledgement + "\n");
  169. // # Success values
  170. if (responseCreateRecurringPaymentsProfileResponseType.Ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
  171. {
  172. // A unique identifier for future reference to the details of this recurring payment
  173. logger.Info("Profile ID : " + responseCreateRecurringPaymentsProfileResponseType.CreateRecurringPaymentsProfileResponseDetails.ProfileID + "\n");
  174. Console.WriteLine("Profile ID : " + responseCreateRecurringPaymentsProfileResponseType.CreateRecurringPaymentsProfileResponseDetails.ProfileID + "\n");
  175. }
  176. // # Error Values
  177. else
  178. {
  179. List<ErrorType> errorMessages = responseCreateRecurringPaymentsProfileResponseType.Errors;
  180. foreach (ErrorType error in errorMessages)
  181. {
  182. logger.Debug("API Error Message : " + error.LongMessage);
  183. Console.WriteLine("API Error Message : " + error.LongMessage + "\n");
  184. }
  185. }
  186. }
  187. }
  188. // # Exception log
  189. catch (System.Exception ex)
  190. {
  191. // Log the exception message
  192. logger.Debug("Error Message : " + ex.Message);
  193. Console.WriteLine("Error Message : " + ex.Message);
  194. }
  195. return responseCreateRecurringPaymentsProfileResponseType;
  196. }
  197. }