Web developer and entrepreneur

Easy DPS Payment Express interface for PHP

Filed Under DPS, PHP, Security - October 27th, 2006 11:28am

DPS are New Zealand’s biggest player in the payments gateway business. They’ve been around for as long as I’ve been building E-Commerce sites which must be at least 6 or 7 years now.

Their main service for handling card processing on sites is Payment Express, or PX. It’s been through a number of different incarnations over time, from requiring a proxy daemon (running on the hosting server itself) in the old days to now having a clean web service interface.

DPS have always provided excellent documentation, but their example code for PHP only covered the most basic situations and wasn’t easily reusable. I had always intended to publish some interface code ‘once it was good enough’ and since this class is being used on a dozen or so sites now, it must be the right time.

There are still some features to come like handling refunds and Auth/Completion transactions, and sometime soon I’ll remove the dependency on PEAR::HTTP_Request. I might even backport it to PHP4 if anyone needs it.

Billing a Credit Card

In it’s simplest form, billing a credit card requires very little code:

$dps = new DPSPXPost(DPS_USERNAME, DPS_PASSWORD);
$dps->chargeCard($name, $number, $exp_month, $exp_year, $amount);

You’ll certainly want to check that the transaction was a success though, and record the DPS transaction ID somewhere:

try {
  $txn_ref = $dps->chargeCard($name, $number, $exp_month, $exp_year, $amount);
  // Success!
  printf('Transaction OK: DPS Reference %s', $txn_ref);
} catch (Exception $e) {
  printf('Transaction Failed: %s', $e->getMessage());
}

That was easy and probably covers the vast majority of E-Commerce implementations. How about if we want to get a bit more clever and store our user’s card details (securely) with DPS, for billing again later? Maybe we’re checking their card is valid first, or planning to rebill at the end of each month.

Rebilling a Loaded Card

First we load the card details into the DPS system. Be aware that you’ll need tipping to be enabled on your merchant facility, and activated by DPS for your account.

try {
  $id = $dps->loadCard($name, $number, $exp_month, $exp_year);
} catch (Exception $e) {
  printf("Couldn't load card: %s", $e->getMessage());
}

Now that we’ve sent the credit card details to DPS, we can remove all trace of them from our system. That means there’s no security risk to your customers if anything should happen to your server.

We can then initiate a charge to the card any time by providing the Billing ID we were given when we loaded the card. Again this is just a single line of code, along with some error checking:

try {
  $txn_ref = $dps->chargeLoadedCard($id, $amount);
} catch (Exception $e) {
  printf('Transaction Failed: %s', $e->getMessage());
}

Show me the Code

There are a lot of options for the interface that I haven’t covered here, not least of which is a debugging log. Browse through the API docs at your leisure to see what else you can do.

Hopefully this will be useful to other developers - feel free to send me questions / comments / bug fixes / improvements / beer.

» Download DPSPXPost.zip

Comments

5 Responses to “Easy DPS Payment Express interface for PHP”

  1. Tobz on May 4th, 2007 3:01 pm

    Hi, I’m wondering if you have class to handle the pxpay service (hosted) as well?
    dps has no sample php for this service.

    any help would be appreciated.

  2. james on May 4th, 2007 5:03 pm

    Hi Tobz,

    DPS do have sample PHP code for PXPay, but you have to ask them for a developer account to be able to login and download it.

    Let me know if you aren’t able to get it and I’ll see if I have an archive around here anywhere.

  3. Matt on June 21st, 2007 1:40 pm

    Hi,

    I’m also looking for php4 code to use with PXPay.

    I’ve emailed DPS but haven’t received a response yet.

  4. Tim on November 6th, 2007 1:25 am

    Hi,
    I am new to payment gateway integration, and i’ve got a development account with DPS. Are you able to do me a huge favour and put up a sample integration of PX Post (PHP)?

  5. James on November 6th, 2007 11:46 am

    Hi Tim,
    The code above and in the download is for PX Post with PHP :)

Leave a Reply