Switching PayPal / SandBox accounts

If you occasionally test your website targeting PayPal Sandbox and change the target to PayPal live frequently, then, you might have faced the pain of managing code. Moreover, switching the PayPal account target can cause error if you miss any appropriate step. For example, when you change the target account, you need to remember to change the PDT Authentication token, Encrypted Payment Settings etc. If your preference is to use Encrypted Payment for another account, then, you need to remember to use Business Email Address instead of Merchant ID. Anyway, in order to facilitate you, this control has a built in solution for this scenario. This control offers an Interface named 'IAccountConfig'. You can create as many classes implementing this interface where the classes will be the holder of your account configuration.


Keep this file in the App_Code folder. So, now, you know you have all of your account information organized in a common place. Now, let us consider a scenario where you have configured your PayPal Controls for targeting PayPal live site. Occasionally you want to switch to PayPal sandbox but you do not want to touch the ASPX markup of your pages as that can be unmaintainable.

  1. OK, you just need to create a class, name it SandBoxConfig implementing the interface IAccountConfig as shown in the above snippet. You can name the class anything as long as your class implements the interface IAccountConfig. For the scenario you are talking about, you just need only one config class.
  2. Now, place your class in another class named PayPalAccountConfigs. Place a constant Boolean field named TURN_SANDBOX_ON . This is the field you will occasionally change to target your Sandbox account.
  3. Now, In the Page_Load event handler method of your Page (where you used PayPal Control), use the following snippet: using_implementation
  4. One very important matter to note : If you want to follow this pattern, you must set the property

    PayPalIPN.EnablePageLoadEventInIPNSession = true
    Explanation: When IPN_Notified event is fired, Page_Load event is suppressed by default unless you turn that option on by the snippet shown above. As Sandbox IPN validation URL is different than the PayPal Live IPN validation URL, the control needs to know that the target account is Sandbox at the beginning of the IPN_Notified event fire. By enabling Page_Load event, your code if (PayPalAccountConfigs.TURN_SANDBOX_ON) ConfigApplier.ApplyAccountParams(this, new PayPalAccountConfigs.SandBoxConfig()); will get executed prior to IPN_Notified event. But, when you turn PayPalIPN.EnablePageLoadEventInIPNSession = true, you need to be careful, because, some code in Page_Load event maybe harmful for IPN_Notified event and so, you may get multiple times IPN_Notified event get fired. Thats why, we used the code

    if (
    PaymentButton.IsPostPaymentEvent) return; In the Page_Load event. This statement will check if the Page is Loaded in IPN session or PayPal_Returned session. If true then, bypass your other statements in Page_Load event handler method.

We have just demonstrated the usage of this pattern for one scenario where you occasionally switch to Sandbox. You can use this pattern to target multiple PayPal accounts.

Last updated on 1 November 2014, Saturday, 11:52:23 PM