Merge pull request #28 from paypal/sample-dyna-config

Sample update to showcase dynamic configuration
This commit is contained in:
Latha
2013-09-02 00:40:47 -07:00
7 changed files with 73 additions and 36 deletions

View File

@@ -1,4 +1,5 @@
<?php <?php
/* /*
* Sample bootstrap file. * Sample bootstrap file.
*/ */
@@ -8,33 +9,62 @@ if(!file_exists(__DIR__ .'/vendor/autoload.php')) {
echo "The 'vendor' folder is missing. You must run 'composer update --no-dev' to resolve application dependencies.\nPlease see the README for more information.\n"; echo "The 'vendor' folder is missing. You must run 'composer update --no-dev' to resolve application dependencies.\nPlease see the README for more information.\n";
exit(1); exit(1);
} }
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/common.php'; require __DIR__ . '/common.php';
define("PP_CONFIG_PATH", __DIR__);
use PayPal\Rest\ApiContext; use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential; use PayPal\Auth\OAuthTokenCredential;
// ### Api Context $apiContext = getApiContext();
// Pass in a `PayPal\Rest\ApiContext` object to authenticate
// the call. You can also send a unique request id
// (that ensures idempotency). The SDK generates
// a request id if you do not pass one explicitly.
$apiContext = new ApiContext(new OAuthTokenCredential(
'EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM',
'EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM'));
// Uncomment this step if you want to use per request
// dynamic configuration instead of using sdk_config.ini
/*
$apiContext->setConfig(array(
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => '../PayPal.log',
'log.LogLevel' => 'FINE'
));
*/
/**
* Helper method for getting an APIContext for all calls
*
* @return PayPal\Rest\ApiContext
*/
function getApiContext() {
// ### Api context
// Use an ApiContext object to authenticate
// API calls. The clientId and clientSecret for the
// OAuthTokenCredential class can be retrieved from
// developer.paypal.com
$apiContext = new ApiContext(
new OAuthTokenCredential(
'EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM',
'EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM'
)
);
// #### SDK configuration
// Comment this line out and uncomment the PP_CONFIG_PATH
// 'define' block if you want to use static file
// based configuration
$apiContext->setConfig(
array(
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => '../PayPal.log',
'log.LogLevel' => 'FINE'
)
);
/*
// Register the sdk_config.ini file in current directory
// as the configuration source.
if(!defined("PP_CONFIG_PATH")) {
define("PP_CONFIG_PATH", __DIR__);
}
*/
return $apiContext;
}

View File

@@ -81,8 +81,8 @@ $payment->setIntent("sale")
->setTransactions(array($transaction)); ->setTransactions(array($transaction));
// ### Create Payment // ### Create Payment
// Create a payment by posting to the APIService // Create a payment by calling the payment->create() method
// using a valid ApiContext (See bootstrap.php for more on `ApiContext`) // with a valid ApiContext (See bootstrap.php for more on `ApiContext`)
// The return object contains the state. // The return object contains the state.
try { try {
$payment->create($apiContext); $payment->create($apiContext);

View File

@@ -48,7 +48,7 @@ $refund->setAmount($amt);
try { try {
// Create a new apiContext object so we send a new // Create a new apiContext object so we send a new
// PayPal-Request-Id (idempotency) header for this resource // PayPal-Request-Id (idempotency) header for this resource
$apiContext = new ApiContext($apiContext->getCredential()); $apiContext = getApiContext();
$captureRefund = $capture->refund($refund, $apiContext); $captureRefund = $capture->refund($refund, $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) { } catch (PayPal\Exception\PPConnectionException $ex) {

View File

@@ -1,9 +1,16 @@
## This is an example configuration file for the SDK.
## The sample scripts configure the SDK dynamically
## but you can choose to go for file based configuration
## in simpler apps (See bootstrap.php for more).
;Connection Information ;Connection Information
[Http] [Http]
http.ConnectionTimeOut = 30 http.ConnectionTimeOut = 30
http.Retry = 1 http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port][/path] ;http.Proxy=http://[username:password]@hostname[:port]
;Service Configuration ;Service Configuration
[Service] [Service]

View File

@@ -60,8 +60,8 @@ the above types and intent set to sale 'sale'</p></div></div><div class="code"><
<span class="o">-&gt;</span><span class="na">setPayer</span><span class="p">(</span><span class="nv">$payer</span><span class="p">)</span> <span class="o">-&gt;</span><span class="na">setPayer</span><span class="p">(</span><span class="nv">$payer</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">setTransactions</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">));</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="create-payment">Create Payment</h3> <span class="o">-&gt;</span><span class="na">setTransactions</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">));</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="create-payment">Create Payment</h3>
<p>Create a payment by posting to the APIService <p>Create a payment by calling the payment->create() method
using a valid ApiContext (See bootstrap.php for more on <code>ApiContext</code>) with a valid ApiContext (See bootstrap.php for more on <code>ApiContext</code>)
The return object contains the state.</p></div></div><div class="code"><div class="wrapper"><span class="k">try</span> <span class="p">{</span> The return object contains the state.</p></div></div><div class="code"><div class="wrapper"><span class="k">try</span> <span class="p">{</span>
<span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">create</span><span class="p">(</span><span class="nv">$apiContext</span><span class="p">);</span> <span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">create</span><span class="p">(</span><span class="nv">$apiContext</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span>

View File

@@ -31,7 +31,7 @@ an 'id' that you can use to refer to it later.
<span class="k">try</span> <span class="p">{</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="delete-card">Delete Card</h3> <span class="k">try</span> <span class="p">{</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h3 id="delete-card">Delete Card</h3>
<p>deletes saved credit card <p>Lookup and delete a saved credit card.
(See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper"> <span class="nv">$creditCard</span> <span class="o">=</span> <span class="nx">CreditCard</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$card</span><span class="o">-&gt;</span><span class="na">getId</span><span class="p">(),</span> <span class="nv">$apiContext</span><span class="p">);</span> (See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper"> <span class="nv">$creditCard</span> <span class="o">=</span> <span class="nx">CreditCard</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$card</span><span class="o">-&gt;</span><span class="na">getId</span><span class="p">(),</span> <span class="nv">$apiContext</span><span class="p">);</span>
<span class="nv">$creditCard</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$apiContext</span><span class="p">);</span> <span class="nv">$creditCard</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$apiContext</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span>

View File

@@ -36,7 +36,7 @@ try {
try { try {
// ### Delete Card // ### Delete Card
// deletes saved credit card // Lookup and delete a saved credit card.
// (See bootstrap.php for more on `ApiContext`) // (See bootstrap.php for more on `ApiContext`)
$creditCard = CreditCard::get($card->getId(), $apiContext); $creditCard = CreditCard::get($card->getId(), $apiContext);