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,40 +1,70 @@
<?php
/*
* Sample bootstrap file.
*/
// Include the composer autoloader
// Include the composer autoloader
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";
exit(1);
}
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/common.php';
define("PP_CONFIG_PATH", __DIR__);
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
// ### Api Context
// 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'
));
*/
$apiContext = getApiContext();
/**
* 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));
// ### Create Payment
// Create a payment by posting to the APIService
// using a valid ApiContext (See bootstrap.php for more on `ApiContext`)
// Create a payment by calling the payment->create() method
// with a valid ApiContext (See bootstrap.php for more on `ApiContext`)
// The return object contains the state.
try {
$payment->create($apiContext);

View File

@@ -48,7 +48,7 @@ $refund->setAmount($amt);
try {
// Create a new apiContext object so we send a new
// PayPal-Request-Id (idempotency) header for this resource
$apiContext = new ApiContext($apiContext->getCredential());
$apiContext = getApiContext();
$captureRefund = $capture->refund($refund, $apiContext);
} 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
[Http]
http.ConnectionTimeOut = 30
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port][/path]
;http.Proxy=http://[username:password]@hostname[:port]
;Service Configuration
[Service]
@@ -23,4 +30,4 @@ log.FileName=../PayPal.log
# Logging level can be one of FINE, INFO, WARN or ERROR
# Logging is most verbose in the 'FINE' level and
# decreases as you proceed towards ERROR
log.LogLevel=FINE
log.LogLevel=FINE

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">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
using a valid ApiContext (See bootstrap.php for more on <code>ApiContext</code>)
<p>Create a payment by calling the payment->create() method
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>
<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>
@@ -82,4 +82,4 @@ The return object contains the state.</p></div></div><div class="code"><div clas
<span class="x"> &lt;pre&gt;</span><span class="cp">&lt;?php</span> <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">toArray</span><span class="p">());</span><span class="cp">?&gt;</span><span class="x">&lt;/pre&gt;</span>
<span class="x"> &lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;</span>
<span class="x">&lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>

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>
<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>
<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>
@@ -47,4 +47,4 @@ an 'id' that you can use to refer to it later.
<span class="x"> &lt;p&gt; Credit Card deleted Successfully&lt;/p&gt;</span>
<span class="x"> &lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;</span>
<span class="x">&lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>

View File

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