Saturday, January 12, 2013

php cybersource payment gateway integration

This PHP class can be used to process credit card payments via Cybersource.

It can send HTTP requests to Cybersource SOAP Web services API server to  perform several types
of operations to process credit card payments.

Currently it can request a payment authorization, capture the result of a  payment request and
request the reversal of a previous payment request.

This class would require php_soap and php_openssl extensions.

Code Example: Download Example

index.php
<?php
/**
 * Test script to show how the EPS_CYBERSOURCE class can be used.
 */
 
 require 'class.eps_cybersource.php';
 
/**
 * These should go in a config file somewhere on the box.
 */
 $trans_key = 'your SOAP transaction key';
 $merchant_id = 'your merchant id';
 $url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.wsdl';
 
/**
 * These are sent from some GUI and assembled into the applicable arrays.
 */
 $bill_array = array('firstName'=>'John','lastName'=>'Doe','street1'=>'1295 Charleston Road',
                     'city'=>'Mountain View','state'=>'CA','postalCode' => '94043','country'=>'US',
                     'email'=> 'null@cybersource.com','ipaddress'=>'10.7.111.111');
 $card_array = array('accountNumber'=>'4111111111111111','expirationMonth'=>'12',
                     'expirationYear'=>'2020','cvNumber'=>'123');
 $item_array = array(
 array('unitPrice'=>'.50','quantity'=>2,'productName'=>'product one'),
 array('unitPrice'=>'2.5','quantity'=>1,'productName'=>'product two'));
 $custom_array = array('one','two','three','four');
 
/**
 * Authorize a transaction.
 */
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setMerchantDefinedData($custom_array);
 $soap->setReferenceCode(array('CSTEST','YYYY','J','-','RNDM'));
 $soap->setCCRequest($bill_array,$card_array,$item_array);
 $soap->ccAuthorize();
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 
 print_r($soap->reply);
 
/**
 * Capture the successful authorization.
 * A single ccCapture() could have been done instead of a ccAuthorize() followed by a ccCapture().
 */
 if ($soap->success) $soap->ccCapture();
 
/**
 * These return values would be stored locally.
 */
 $tok = $soap->reply->requestToken;
 $id = $soap->reply->requestID;
 $rc = $soap->reply->merchantReferenceCode;
 $amount = $soap->reply->amount;
 $currency = $soap->reply->currency;
 
 print_r($soap->reply);
 
 $trans_array = array('requestToken'=>$tok,
 'requestID'=>$id,
 'referenceCode'=>$rc,
 'amount'=>$amount,
 'currency'=>$currency);
 
/**
 * Reverse the capture or authorization.
 */
 if ($soap->success)
 {
 unset($soap);
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setCCReversalRequest($tok,$id,$rc,$amount);
 $soap->ccReverse('c');
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 }
 
 print_r($soap->reply);   
 
/**
 * Credit the account.
 */
 if ($soap->success)
 {
 unset($soap);
 try
 {
 $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
 $soap->setCCCreditRequest($bill_array,$card_array);
 $soap->setReferenceCode(array('CR','YYYY','J','-',array('RNDM',5,5)));
 $soap->ccCredit('2.53');
 }
 catch (SoapFault $e)
 {
 exit($e->getMessage());
 }
 }
 
 print_r($soap->reply);   
 
/**
 * Get some help on the XML schema.
 */
 print_r($soap->getHelp());
 
 echo "current version: " . $soap->getHelpVersion() . "\n";
 
 print_r($soap->getHelp('item'));
 
 unset($soap);
 
?>

KissMetrics tracking integration with php

API Specifications

To be unobtrusive to your end-users, we’ll respond with a 200 OK status, even if some of these parameters are “incorrect”. The response will be a 1x1 GIF image (to power our ”Beacon API”).

Recording an Event

Method URL:
  • http://trk.kissmetrics.com/e
  • https://trk.kissmetrics.com/e
Parameters (GET)
ParametersFormatNecessary?Description
_kstringYesYour API key
_p255 char stringYesPerson doing the event
_nURL-encoded stringYesName of the event
_tintegeroptionalTimestamp in seconds after UTC Unix epoch
_d0 or 1optionalSet to 1 if you’re manually passing us the timestamp. It’s used when logging events that occurred in the past.
(Anything)URL-encoded stringoptionalSet an arbitrary value to an arbitrary user property
Example
http://trk.kissmetrics.com/e?_k=api-key&_p=bob&_n=Signed+Up&gender=male&_t=1262304000&_d=1
This records that the user bob did the event Signed Up and his gender was male and this all happened on midnight of January 1, 2010 UTC.
Please be aware of how our processing servers detect duplicate events.

Setting Properties

Method URL:
  • http://trk.kissmetrics.com/s
  • https://trk.kissmetrics.com/s
Parameters (GET)
ParametersFormatNecessary?Description
_kstringYesYour API key
_p255 char stringYesPerson doing the event
(Anything)URL-encoded stringoptionalSet an arbitrary value to an arbitrary user property
_tintegeroptionalTimestamp in seconds after UTC Unix epoch
_d0 or 1optionalSet to 1 if you’re manually passing us the timestamp. It’s used when logging events that occurred in the past.
Example
http://trk.kissmetrics.com/s?_k=api-key&_p=bob&gender=male&_t=1262304000&_d=1
This records that the user bob got the property gender with the value set to male and this happened on midnight of January 1, 2010 UTC.
Please be aware of how our processing servers detect duplicate properties.

Aliasing Users

Method URL:
  • http://trk.kissmetrics.com/a
  • https://trk.kissmetrics.com/a
Parameters (GET)
ParametersFormatNecessary?Description
_kstringYesYour API key
_p255 char stringYesOne of the person’s identities
_n255 char stringYesAnother of the person’s identities
Example
http://trk.kissmetrics.com/a?_k=api-key&_p=bob&_n=bob%40bob.com
This tells us to treat bob and bob@bob.com as the same person. If you log events or properties to either ID, they all refer back to the same one person. (bob@bob.com is passed as bob%40bob.com because the @ needs to be URL-encoded.)
You most frequently use this to connect a person’s ID when they are anonymous with the same person’s ID when they are known (that is, to connect a randomly generated ID with their username, an email address, Facebook ID, whatever).
It’s OK to call this more than once with the same pair of identities. It’s also OK if a person has more than one alias.

Filed under APIs