Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
Updates to Sample Code
Browse files Browse the repository at this point in the history
- Updated UI Presentation on samples
- Fixed Bugs
  • Loading branch information
japatel committed Nov 3, 2014
1 parent 4d481ad commit 3c02790
Show file tree
Hide file tree
Showing 97 changed files with 911 additions and 1,256 deletions.
2 changes: 1 addition & 1 deletion lib/PayPal/Api/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function getCart()
* A payment can have more than one transaction, with each transaction establishing a contract between the payer and a payee
*
*
* @param \PayPal\Api\Transaction $transactions
* @param \PayPal\Api\Transaction[] $transactions
*
* @return $this
*/
Expand Down
10 changes: 7 additions & 3 deletions lib/PayPal/Validation/JsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ class JsonValidator
* Helper method for validating if string provided is a valid json.
*
* @param string $string String representation of Json object
* @param bool $silent Flag to not throw \InvalidArgumentException
* @return bool
*/
public static function validate($string)
public static function validate($string, $silent = false)
{
json_decode($string);
if (json_last_error() != JSON_ERROR_NONE) {
//Throw an Exception for string or array
throw new \InvalidArgumentException("Invalid JSON String");
if ($silent == false) {
//Throw an Exception for string or array
throw new \InvalidArgumentException("Invalid JSON String");
}
return false;
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PayPal/Validation/ModelAccessorValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function validate(PPModel $class, $attributeName)
if (!method_exists($class, $methodName)) {
//Delegate the error based on the choice
$className = is_object($class) ? get_class($class) : (string)$class;
$errorMessage = "Missing Accessor: $className:$methodName. Please let us know by creating an issue at https://github.com/paypal/rest-api-sdk-php/issues";
$errorMessage = "Missing Accessor: $className:$methodName. Please let us know by creating an issue at https://github.com/paypal/PayPal-PHP-SDK/issues";
PPLoggingManager::getInstance(__CLASS__)->warning($errorMessage);
if ($mode == 'strict') {
trigger_error($errorMessage, E_USER_NOTICE);
Expand All @@ -41,4 +41,4 @@ public static function validate(PPModel $class, $attributeName)
}
return true;
}
}
}
5 changes: 2 additions & 3 deletions sample/billing/CreateBillingAgreementWithCreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@
// Please note that as the agreement has not yet activated, we wont be receiving the ID just yet.
$agreement = $agreement->create($apiContext);

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Created Billing Agreement.", "Agreement", $agreement->getId(), $request, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/CreateBillingAgreementWithPayPal.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@
}
}

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Created Billing Agreement.", "Agreement", null, $request, $ex);
exit(1);
}

Expand Down
7 changes: 3 additions & 4 deletions sample/billing/CreatePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
$baseUrl = getBaseUrl();
$merchantPreferences->setReturnUrl("$baseUrl/ExecuteAgreement.php?success=true")
->setCancelUrl("$baseUrl/ExecuteAgreement.php?success=false")
->setAutoBillAmount("YES")
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new Currency(['value' => '1', 'currency' => 'USD']));
Expand All @@ -61,9 +61,8 @@
// ### Create Plan
try {
$output = $plan->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Created Plan", "Plan", null, $request, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/ExecuteAgreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

try {
$agreement->execute($token, $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $ex);
exit(1);
}
ResultPrinter::printResult("Executed an Agreement", "Agreement", $agreement->getId(), $_GET['token'], $agreement);
Expand Down
5 changes: 2 additions & 3 deletions sample/billing/GetBillingAgreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

try {
$agreement = Agreement::get($createdAgreement->getId(), $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Retrieved an Agreement", "Agreement", $agreement->getId(), $createdAgreement->getId(), $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/GetPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

try {
$plan = Plan::get($createdPlan->getId(), $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Retrieved a Plan", "Plan", $plan->getId(), null, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/ListPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
// at https://developer.paypal.com/webapps/developer/docs/api/#list-plans
$params = array('page_size' => '2');
$planList = Plan::all($params, $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("List of Plans", "Plan", null, $params, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/ReactivateBillingAgreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
// Lets get the updated Agreement Object
$agreement = Agreement::get($suspendedAgreement->getId(), $apiContext);

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printResult("Reactivate the Agreement", "Agreement", $agreement->getId(), $suspendedAgreement, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/SuspendBillingAgreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
// Lets get the updated Agreement Object
$agreement = Agreement::get($createdAgreement->getId(), $apiContext);

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Suspended the Agreement", "Agreement", null, $agreementStateDescriptor, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/UpdateBillingAgreement.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
// Lets get the updated Agreement Object
$agreement = Agreement::get($createdAgreement->getId(), $apiContext);

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Updated the Agreement with new Description and Updated Shipping Address", "Agreement", null, $patchRequest, $ex);
exit(1);
}

Expand Down
5 changes: 2 additions & 3 deletions sample/billing/UpdatePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

$plan = Plan::get($createdPlan->getId(), $apiContext);

} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
} catch (Exception $ex) {
ResultPrinter::printError("Updated the Plan to Active State", "Plan", null, $patchRequest, $ex);
exit(1);
}

Expand Down
126 changes: 59 additions & 67 deletions sample/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@
*
* Class ResultPrinter
*/
class ResultPrinter {
class ResultPrinter
{

private static $printResultCounter = 0;

public static function printResult($title, $objectName, $objectId = null, $request = null, $response = null)
/**
* 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";
Expand All @@ -33,24 +44,24 @@ public static function printResult($title, $objectName, $objectId = null, $reque
self::$printResultCounter++;
echo '
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-'.self::$printResultCounter.'">
<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 .'</a>
'. 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>" . ($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>Response Object</h4>';
self::printObject($response);
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">
Expand All @@ -61,22 +72,59 @@ public static function printResult($title, $objectName, $objectId = null, $reque
<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);
self::printObject($response, $errorMessage);
echo '</div></div></div></div>
</div>
</div>';

flush();
}

protected static function printObject($object)
/**
* 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">' . $object->toJSON(128) . "</pre>";
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>$object</pre>";
echo '<pre class="prettyprint '. ($error ? 'error' : '') .'">' . $object . '</pre>';
} else {
echo "<pre>";
print_r($object);
Expand Down Expand Up @@ -111,59 +159,3 @@ function getBaseUrl()
$request = $_SERVER['PHP_SELF'];
return dirname($protocol . '://' . $host . ($port == $protocol_port ? '' : ':' . $port) . $request);
}

/**
* Creates a new mock 'payment authorization'
*
* @param PayPal\Api\ApiContext apiContext
* @return PayPal\Api\Authorization
*/
function createAuthorization($apiContext)
{
$addr = new Address();
$addr->setLine1("3909 Witmer Road")
->setLine2("Niagara Falls")
->setCity("Niagara Falls")
->setState("NY")
->setPostalCode("14305")
->setCountryCode("US")
->setPhone("716-298-1822");

$card = new CreditCard();
$card->setType("visa")
->setNumber("4417119669820331")
->setExpireMonth("11")
->setExpireYear("2019")
->setCvv2("012")
->setFirstName("Joe")
->setLastName("Shopper")
->setBillingAddress($addr);

$fi = new FundingInstrument();
$fi->setCreditCard($card);

$payer = new Payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));

$amount = new Amount();
$amount->setCurrency("USD")
->setTotal("1.00");

$transaction = new Transaction();
$transaction->setAmount($amount)
->setDescription("Payment description.");

$payment = new Payment();

// Setting intent to authorize creates a payment
// authorization. Setting it to sale creates actual payment
$payment->setIntent("authorize")
->setPayer($payer)
->setTransactions(array($transaction));

$paymnt = $payment->create($apiContext);
$resArray = $paymnt->toArray();

return $authId = $resArray['transactions'][0]['related_resources'][0]['authorization']['id'];
}
Loading

0 comments on commit 3c02790

Please sign in to comment.