0
votes

I am currently implementing a credit card processing script, most as provided by the merchant gateway. The code calls functions within a class and returns a string based on the response. The end php code I am using (details removed of course) with example information is:

<?php
$gw = new gwapi;
$gw->setLogin("username", "password");
$gw->setBilling("John","Smith","Acme, Inc.","888","Suite 200", "Beverly Hills",
        "CA","77777","US","555-555-5555","555-555-5556","[email protected]",
        "www.example.com");
//        "CA","90210","US","[email protected]");
$gw->setOrder("1234","Big Order",1, 2, "PO1234","65.192.14.10");

$r = $gw->doSale("1.00","4111111111111111","1010");
print $gw->responses['responsetext'];

?>

where setlogin allows me to login, setbilling takes the sample consumer information, set order takes the order id and description, dosale takes the amount charged, cc number and exp date.

when all the variables are sent validated then sent off for processing, a string is returned in the following format:

response=1&responsetext=SUCCESS&authcode=123456&transactionid=23456&avsresponse=M&orderid=&type=sale&response_code=100

where:

  • response = transaction approved or declined
  • response text = textual response
  • authcode = transaction authorization code
  • transactionid = payment gateway tran id
  • avsresponse = avs response code
  • orderid = original order id passed in tran request
  • response_code = numeric mapping of processor response

I am trying to solve for the following:

  1. How do I take the data which is passed back and display it appropriately on the page - If the transaction failed or AVS code doesnt match my liking or something is wrong, an error is displayed to the consumer; if the transaction processed, they are taken to a completion page and the transaction id is sent in SESSION as output to the consumer
  2. If the response_code value matches a table of values, certain actions are taken, i.e. if code =100, take to success page, if code = 300 print specific error on original page to customer, etc.
2
I'm somewhat terrified that a programmer who can't figure out how to parse a string is handling credit card information.Matti Virkkunen
@Matti - thanks for the confidence. I work for an organization which I helped get to PCI compliance and worked in project management/product development for the last 3 years. We let some of our programming team go and I took the ball to help get one of our partner sites running. Perhaps instead of insulting me you could provide guidance to best practices or any previous experience because your comment does nothing for this questionJM4
And to that tune - one of the most popular questions on S.O. was why 'newbie' programmers shy away from advancing their techniques and its because of answers like that. Advanced programmers treat newcomers like they are stupid. If this forum were only for advanced levels of questions then they should have made it a criteria early on.JM4
That's not what I meant at all. Of course less advanced programmers should keep challenging themselves to improve their abilities. But not in production code.Matti Virkkunen

2 Answers

2
votes

Use parse_str() with the array argument (never use without) to get an array with key/value pairs out of the string. You can then easily access the separate values and implement your logic.

0
votes

I would use the explode function on the whole string, separating on the & sign, to get a first array. I would then then iterate over the result with another explode, separating on the =sign to get a key-value pair. From there you can work with it as you would any other array.