Friday, January 16, 2009

Paypal IPN and mc_fee

I encountered an interesting problem today with accepting payments for an online registration solution I wrote for a non-profit organization. The application accepts payments using Paypal's Instant Payment Notification feature. IPN works as follows:
  1. You put a form (hidden or with bits visible) that POSTs data to Paypal
  2. This brings the user to Paypal where they can pay using either a credit card or their Paypal account
  3. As part of processing the user's payment, Paypal takes key information about the user's payment e.g. transaction id, amount, and other sundry details and POSTs them to a custom instant payment notification URL (as it happens to be called) on your site
  4. This URL is hopefully hosts a script or other form of code that can pick up all the POSTed IPN variables and POSTs back the information with one extra variable to validate the information
  5. Once this POST back to Paypal returns with a success status the application can record the user's payment and credit them appropriately.
The problem I'm facing is that I received an IPN POST to my application where the mc_fee variable is apparently null or undefined. This is a column that's specified as NOT NULL in my database schema as per many other examples from Paypal and others. I'm not sure what's going on. It's too late to mount a full investigation tonight but tomorrow is a new day.

3 comments:

  1. Well... what was the trouble with mc_fee? :)

    ReplyDelete
  2. Haha it's been a while since I've looked back at it. I don't recall any more except that perhaps the POSTs where mc_fee is null aren't the final resolved transactions.

    My solution to the problem was to make the mc_fee field not null with a default of 0.00. Your comment made me look at the data I've collected so far in that application. Here's what I see as a result of the following query:

    select distinct payment_status, mc_fee from paypal_detail where mc_fee = 0.00

    payment_status | mc_fee
    ----------------+--------
    Failed | 0.00
    Pending | 0.00
    Reversed | 0.00

    It looks like mc_fee was null for all transactions where the payment_status was not "Completed" or "Refunded"

    The output of:

    select distinct payment_status from paypal_detail

    payment_status
    ----------------
    Completed
    Pending
    Reversed
    Refunded
    Failed

    ReplyDelete
  3. Hi Shahbaz,
    Quite recently I started a simple webshop, using a similar technicque as descriped above. But for me the mc_fee was a surprice, do you know if this is required and if its possible to manupulate this? Because on a order of 7 dollars they charge almost 1 dollar... :( there go's my profit? Any ideas?

    Kind Regards

    ReplyDelete