

<?php

	//THIS EXAMPLE POINTS TO THE PRODUCTION SANDBOX
	//I'm only using the xmp tags as a quick and dirty way to spit out the response xml.  This is obviously a depricated tag, but it works for this example.

	require_once("paymentlibrary.php");

	//Fill in these variables with the appropriate values
	$p12filename = '';
	$password = '';
	$sandboxClientId = '';

	echo '<html><head></head><body>';

	//Purchase

	echo 'PURCHASE<br /><br />';

	$purchaseBody = '<?xml version="1.0" encoding="utf-8"?><PurchaseRequest><MerchantIdentity><CompanyId>47586</CompanyId><CompanyPassword>YtNDA1NS05YmJjLWZmMzc1NjJkZmViZg</CompanyPassword></MerchantIdentity><Reference><MessageId>12TG65H987BBQJ56742</MessageId><SettlementId>12345WXYZ6789</SettlementId></Reference><Amount><Currency>USD</Currency><Value>55555</Value></Amount><FundingCard><CardholderName>LEE M CARDHOLDER</CardholderName><AccountNumber>5466160123456789</AccountNumber><ExpiryMonth>10</ExpiryMonth><ExpiryYear>13</ExpiryYear><SecurityCode>699</SecurityCode></FundingCard><Authentication><EcommerceIndicator>05</EcommerceIndicator><TransactionId>12345678</TransactionId><Enrolled>Y</Enrolled><Status>Y</Status><Verification><Status>Y</Status><SecurityLevel>05</SecurityLevel><Token>8skd828s</Token></Verification></Authentication></PurchaseRequest>';

	$purchase = new MCPurchase($p12filename, $password, $sandboxClientId, $purchaseBody);

	echo '<xmp>' . $purchase->getPurchaseXML() . '</xmp>';

	//Authorization

	echo '<br /><br />AUTHORIZATION<br /><br />';

	$authorizationBody = '<?xml version="1.0" encoding="utf-8"?><AuthorizationRequest><MerchantIdentity><CompanyId>47586</CompanyId><CompanyPassword>YtNDA1NS05YmJjLWZmMzc1NjJkZmViZg</CompanyPassword></MerchantIdentity><Reference><MessageId>12TG65H987BBQJ56742</MessageId><SettlementId>12345WXYZ6789</SettlementId></Reference><Amount><Currency>USD</Currency><Value>44444</Value></Amount><FundingCard><CardholderName>LEE M CARDHOLDER</CardholderName><AccountNumber>5466160123456789</AccountNumber><ExpiryMonth>10</ExpiryMonth><ExpiryYear>13</ExpiryYear><SecurityCode>699</SecurityCode></FundingCard><Authentication><EcommerceIndicator>05</EcommerceIndicator><TransactionId>12345678</TransactionId><Enrolled>Y</Enrolled><Status>Y</Status><Verification><Status>Y</Status>			<SecurityLevel>05</SecurityLevel><Token>8skd828s</Token></Verification></Authentication></AuthorizationRequest>';

	$authorization = new MCAuthorization($p12filename, $password, $sandboxClientId, $authorizationBody);

	echo '<xmp>' . $authorization->getAuthorizationXML() . '</xmp>';

	//Capture

	echo '<br /><br />CAPTURE<br /><br />';

	$captureBody = '<?xml version="1.0" encoding="utf-8"?><CaptureRequest><MerchantIdentity><CompanyId>47586</CompanyId><CompanyPassword>YtNDA1NS05YmJjLWZmMzc1NjJkZmViZg</CompanyPassword></MerchantIdentity><Amount><Currency>USD</Currency><Value>33333</Value>  </Amount><Reference><MessageId>12TG65H987BBQJ56742</MessageId><SettlementId>12345WXYZ6789</SettlementId><OrderId>12345WXYZ67892321</OrderId></Reference></CaptureRequest>';

	$capture = new MCCapture($p12filename, $password, $sandboxClientId, $captureBody);

	echo '<xmp>' . $capture->getCaptureXML() . '</xmp>';

	//Refund

	echo '<br /><br />REFUND<br /><br />';

	$refundBody = '<?xml version="1.0" encoding="utf-8"?><RefundRequest><MerchantIdentity><CompanyId>47586</CompanyId><CompanyPassword>YtNDA1NS05YmJjLWZmMzc1NjJkZmViZg</CompanyPassword></MerchantIdentity><Amount><Currency>USD</Currency><Value>22222</Value>    </Amount><Reference><MessageId>12TG65H987BBQJ56742</MessageId><SettlementId>12345WXYZ6789</SettlementId><OrderId>12345WXYZ67892321</OrderId></Reference></RefundRequest>';

	$refund = new MCRefund($p12filename, $password, $sandboxClientId, $refundBody);

	echo '<xmp>' . $refund->getRefundXML() . '</xmp>';

	//Void

	echo '<br /><br />VOID<br /><br />';

	$voidBody = '<?xml version="1.0" encoding="utf-8"?><VoidRequest><MerchantIdentity><CompanyId>685123</CompanyId><CompanyPassword>vRWoQu7Cw8sN3KbbLbEajQ==</CompanyPassword></MerchantIdentity><Reference><MessageId>12TG65H987BBQJ56742</MessageId>		<SettlementId>12345WXYZ6789</SettlementId><OrderId>12345WXYZ67892321</OrderId></Reference></VoidRequest>';

	$void = new MCVoid($p12filename, $password, $sandboxClientId, $voidBody);

	echo '<xmp>' . $void->getVoidXML() . '</xmp>';



	echo '</body></html>';

?>

Comments (19)  

    Hide Comments Collapse All 

    Nov 11, 2010
    Matthew Hopkins says: A few more items to note about using PHP... If you have the PECL OAuth extensi...

    A few more items to note about using PHP...
        If you have the PECL OAuth extensions installed on your server, then you will run into an issue where the OAuthException class is defined twice.  This can be avoided by either uninstalling the PECL OAuth extensions or by renaming the OAuthException class.
        I had multiple issues when working with the body of a payments request.  First, it is important that there be absolutely no space between any of the tags is the request XML.  It is possible, though I have not tried it, to use the nowdoc syntax <<EOD ... EOD;
        Another body issue was ensuring the that the body is url encoded before the SHA1 hashing and base 64 encoding in performed.
        Permalink

    Jan 30, 2012
    Andreas Geier says: do you by any chance also have a php class developed for MATCH?

    do you by any chance also have a php class developed for MATCH?
        Permalink

        Jan 30, 2012
        Dan Martin says: Andreas, No sorry, we don't. However, altering this code to access MATCH shoul...

        Andreas,

        No sorry, we don't. However, altering this code to access MATCH should be very simple. The only change would be the endpoint URL, and the payload. Both Payments and MATCH require a POST body, and the resulting oauth-body-hash.
            Permalink

    Jan 31, 2012
    Andreas Geier says: the connection seems to be established now, but we get an error JAXBException o...

    the connection seems to be established now, but we get an error

    JAXBException occurred : Content is not allowed in prolog.. Content is not allowed in prolog..

    Here's the code:

    $p12filename = '(generated) MCOpenAPI.p12 ';
    $password = '(given password for generated p12)';
    $sandboxClientId = '(Sandbox Consumer Key)';


    // Sample data
    $acquirerId = "1996";
    $merchantName = "Terminated Merchant";
    $addressLine1 = "1234 State Street";
    $addressCity = "St. Louis";
    $addressState = "MO";
    $addressPostalCode = "55555";
    $addressCountry = "USA";
    $principalFirstName = "John";
    $principalLastName = "Smith";
    $principalAddressCountry = "USA";

    $matchRequest =
    '<?xml version="1.0" encoding="utf-8"?>' .
    '<ns2:TerminationInquiryRequest' + 'xmlns:ns2="http://mastercard.com/termination">' .
    '<AcquirerId>'. $acquirerId . '</AcquirerId>' .
    '<Merchant>' .
    '<Name>' . $merchantName . '</Name>' .
    '<Address>' .
    '<Line1>' . $addressLine1 . '</Line1>' .
    '<City>' . $addressCity . '</City>' .
    '<CountrySubdivision>' . $addressState . '</CountrySubdivision>' .
    '<PostalCode>' . $addressPostalCode . '</PostalCode>' .
    '<Country>' . $addressCountry . '</Country>' .
    '</Address>' .
    '<Principal>' .
    '<FirstName>' . $principalFirstName . '</FirstName>' .
    '<LastName>' . $principalLastName . '</LastName>' .
    '<Address>' .
    '<Country>' . $principalAddressCountry . '</Country>' .
    '</Address>' .
    '</Principal>' .
    '</Merchant>' .
    '</ns2:TerminationInquiryRequest>';


    //MATCH
    $match = new MCMatch($p12filename, $password, $sandboxClientId, $matchRequest);
    echo '<xmp>' . $match->getMatchXML() . '</xmp>';

    CLASS DEFINITION:

    class MCMatch
    {

       protected $url = "https://sandbox.api.mastercard.com/fraud/merchant/v1/termination-inquiry?Format=XML&PageOffset=0&PageLength=10";

       protected $matchBody;
       protected $matchHeader;

       function __construct($p12file, $pass, $publicKey, $matchBody)
       {
          $consumer = new OAuthConsumer($publicKey,NULL);

          $params = array("oauth_consumer_key"=>$publicKey,
             "oauth_nonce"=>time() . rand(1000, 9999),
             "oauth_timestamp"=>time(),
             "oauth_version"=>"1.0",
             "oauth_body_hash"=>base64_encode(sha1(utf8_encode($matchBody),true)),
             "oauth_signature_method"=>"RSA-SHA1")
             ;
          $request = new OAuthRequest('POST',$this->url,$params);
          $signatureMethod = new TestOAuthSignatureMethod_RSA_SHA1($p12file, $pass);
          $request->sign_request($signatureMethod,$consumer,NULL);
          $matchHeader[0] = $request->to_header();
          $matchHeader[1] = "content-type: application/xml";
          $matchHeader[2] = "content-length: ".strlen($matchBody);
          $this->matchBody = $matchBody;
          $this->matchHeader = $matchHeader;
       }

       function getMatchXML()
       {
          $curl_handle = curl_init();
          curl_setopt($curl_handle,CURLOPT_URL,$this->url);
          curl_setopt($curl_handle,CURLOPT_POST,1);
          curl_setopt($curl_handle,CURLOPT_POSTFIELDS,$this->matchBody);
          curl_setopt($curl_handle,CURLOPT_HTTPHEADER,$this->matchHeader);
          curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,TRUE);
          curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER,FALSE);
          $response = curl_exec($curl_handle);
          curl_close($curl_handle);
          return $response;
       }
    }

