forked from LiveCarta/PayPal-PHP-SDK
162 lines
6.1 KiB
PHP
162 lines
6.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
Common functions used across samples
|
|
*/
|
|
|
|
use PayPal\Api\Address;
|
|
use PayPal\Api\CreditCard;
|
|
use PayPal\Api\Amount;
|
|
use PayPal\Api\Payer;
|
|
use PayPal\Api\Payment;
|
|
use PayPal\Api\Transaction;
|
|
use PayPal\Api\FundingInstrument;
|
|
|
|
/**
|
|
* Helper Class for Printing Results
|
|
*
|
|
* Class ResultPrinter
|
|
*/
|
|
class ResultPrinter
|
|
{
|
|
|
|
private static $printResultCounter = 0;
|
|
|
|
/**
|
|
* Prints HTML Output to web page.
|
|
*
|
|
* @param string $title
|
|
* @param string $objectName
|
|
* @param string $objectId
|
|
* @param mixed $request
|
|
* @param mixed $response
|
|
* @param string $error
|
|
*/
|
|
public static function printOutput($title, $objectName, $objectId = null, $request = null, $response = null, $errorMessage = null)
|
|
{
|
|
if (self::$printResultCounter == 0) {
|
|
include "header.html";
|
|
echo '<br />
|
|
<div class="row"><div class="col-md-2 pull-left"><a href="../index.html"><h4>❮❮ Back to Samples</h4></a><br /><br /></div>
|
|
<div class="col-md-1 pull-right"><img src="../images/pp_v_rgb.png" height="70" /></div> </div><div class="clearfix visible-xs-block"></div>';
|
|
echo '<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">';
|
|
}
|
|
self::$printResultCounter++;
|
|
echo '
|
|
<div class="panel panel-default">
|
|
<div class="panel-heading '. ($errorMessage ? 'error' : '') .'" role="tab" id="heading-'.self::$printResultCounter.'">
|
|
<h4 class="panel-title">
|
|
<a data-toggle="collapse" data-parent="#accordion" href="#step-'. self::$printResultCounter .'" aria-expanded="false" aria-controls="step-'.self::$printResultCounter.'">
|
|
'. self::$printResultCounter .'. '. $title . ($errorMessage ? ' (Failed)' : '') . '</a>
|
|
</h4>
|
|
</div>
|
|
<div id="step-'.self::$printResultCounter.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-'. self::$printResultCounter . '">
|
|
<div class="panel-body">
|
|
';
|
|
|
|
if ($objectId) {
|
|
echo "<div>" . ($objectName ? $objectName : "Object") . " with ID: $objectId </div>";
|
|
}
|
|
|
|
echo '<div class="row hidden-xs hidden-sm hidden-md"><div class="col-md-6"><h4>Request Object</h4>';
|
|
self::printObject($request);
|
|
echo '</div><div class="col-md-6"><h4 class="'. ($errorMessage ? 'error' : '') .'">Response Object</h4>';
|
|
self::printObject($response, $errorMessage);
|
|
echo '</div></div>';
|
|
|
|
echo '<div class="hidden-lg"><ul class="nav nav-tabs" role="tablist">
|
|
<li role="presentation" ><a href="#step-'.self::$printResultCounter .'-request" role="tab" data-toggle="tab">Request</a></li>
|
|
<li role="presentation" class="active"><a href="#step-'.self::$printResultCounter .'-response" role="tab" data-toggle="tab">Response</a></li>
|
|
</ul>
|
|
<div class="tab-content">
|
|
<div role="tabpanel" class="tab-pane" id="step-'.self::$printResultCounter .'-request"><h4>Request Object</h4>';
|
|
self::printObject($request) ;
|
|
echo '</div><div role="tabpanel" class="tab-pane active" id="step-'.self::$printResultCounter .'-response"><h4>Response Object</h4>';
|
|
self::printObject($response, $errorMessage);
|
|
echo '</div></div></div></div>
|
|
</div>
|
|
</div>';
|
|
|
|
flush();
|
|
}
|
|
|
|
/**
|
|
* Prints success response HTML Output to web page.
|
|
*
|
|
* @param string $title
|
|
* @param string $objectName
|
|
* @param string $objectId
|
|
* @param mixed $request
|
|
* @param mixed $response
|
|
*/
|
|
public static function printResult($title, $objectName, $objectId = null, $request = null, $response = null)
|
|
{
|
|
self::printOutput($title, $objectName, $objectId, $request, $response, false);
|
|
}
|
|
|
|
/**
|
|
* Prints Error
|
|
*
|
|
* @param $title
|
|
* @param $objectName
|
|
* @param null $objectId
|
|
* @param null $request
|
|
* @param \Exception $exception
|
|
*/
|
|
public static function printError($title, $objectName, $objectId = null, $request = null, $exception = null)
|
|
{
|
|
$data = null;
|
|
if ($exception instanceof \PayPal\Exception\PPConnectionException) {
|
|
$data = $exception->getData();
|
|
}
|
|
self::printOutput($title, $objectName, $objectId, $request, $data, $exception->getMessage());
|
|
}
|
|
|
|
protected static function printObject($object, $error = null)
|
|
{
|
|
if ($error) {
|
|
echo '<pre class="error">'. $error . '</pre>';
|
|
}
|
|
if ($object) {
|
|
if (is_a($object, 'PayPal\Common\PPModel')) {
|
|
/** @var $object \PayPal\Common\PPModel */
|
|
echo '<pre class="prettyprint '. ($error ? 'error' : '') .'">' . $object->toJSON(128) . "</pre>";
|
|
} elseif (\PayPal\Validation\JsonValidator::validate($object, true)) {
|
|
echo '<pre class="prettyprint '. ($error ? 'error' : '') .'">'. str_replace('\\/', '/', json_encode(json_decode($object), 128)) . "</pre>";
|
|
} elseif (is_string($object)) {
|
|
echo '<pre class="prettyprint '. ($error ? 'error' : '') .'">' . $object . '</pre>';
|
|
} else {
|
|
echo "<pre>";
|
|
print_r($object);
|
|
echo "</pre>";
|
|
}
|
|
} else {
|
|
echo "<span>No Data</span>";
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ### getBaseUrl function
|
|
* // utility function that returns base url for
|
|
* // determining return/cancel urls
|
|
*
|
|
* @return string
|
|
*/
|
|
function getBaseUrl()
|
|
{
|
|
|
|
$protocol = 'http';
|
|
if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on')) {
|
|
$protocol .= 's';
|
|
$protocol_port = $_SERVER['SERVER_PORT'];
|
|
} else {
|
|
$protocol_port = 80;
|
|
}
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
$port = $_SERVER['SERVER_PORT'];
|
|
$request = $_SERVER['PHP_SELF'];
|
|
return dirname($protocol . '://' . $host . ($port == $protocol_port ? '' : ':' . $port) . $request);
|
|
}
|