Formatting Paypal dates

10 Jun 2009, by Michael Cindrić

We have been doing a some work on PayPal integration for one of our clients for their soon to be released application and l needed to reformat the date PayPal was sending me. Now l know that Active Merchant could help with this but it didn't fit our application.

So any ways PayPal sends you a date like so "03:25:17 Apr 26, 2007 PDT" and l need it in a clean format so here is what l came up with, its a first run and l am sure l will change it when the time comes to refactor but for now it works.

  def self.date_from_paypal(datetime_string)
    datetime_string =~ /(d+):(d+):(d+) (.+) (d+), (d+)/
    parseable_string = "#$6-#$4-#$5T#$1:#$2:#$3"
    DateTime.parse(parseable_string)
  end

  %w(payment_date).each do |attrib|
    module_eval %{
      def #{attrib}=(datetime_string)
        write_paypal_date :#{attrib}, datetime_string
      end
    }
  end

  private

  def write_paypal_date(attribute, datetime_string)
    write_attribute attribute, PaypalTransaction.date_from_paypal(datetime_string)
  end

So what it does is when you go to set the "payment_date" field it runs the "write_paypal_date" method which formats the date for you a little cleaner. You can add other dates to this very simply by just adding them to the string array in the method above.


Cookies help us deliver our services. By using our services, you agree to our use of cookies.