diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e687b2..fd0d180 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ CHANGELOG ========= -V0.6.0 (April 25, 2013) +V0.6.0 (April 26, 2013) ----------------------- * Adding support for dynamic configuration of SDK (Upgrading sdk-core-php dependency to V1.4.0) + * Changing resource class methods to take an ApiContext argument instead of a OauthTokenCredential argument. V0.5.0 (March 07, 2013) ----------------------- diff --git a/lib/PayPal/Api/CreditCard.php b/lib/PayPal/Api/CreditCard.php index 0a47b5a..982f51f 100644 --- a/lib/PayPal/Api/CreditCard.php +++ b/lib/PayPal/Api/CreditCard.php @@ -237,8 +237,8 @@ class CreditCard extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/vault/credit-card", "POST", $payLoad); $this->fromJson($json); @@ -259,8 +259,8 @@ class CreditCard extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/vault/credit-card/$creditcardid", "GET", $payLoad); $ret = new CreditCard(); diff --git a/lib/PayPal/Api/Payment.php b/lib/PayPal/Api/Payment.php index 3492d32..7ea69ba 100644 --- a/lib/PayPal/Api/Payment.php +++ b/lib/PayPal/Api/Payment.php @@ -185,8 +185,8 @@ class Payment extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/payment?" . http_build_query(array_intersect_key($params, $allowedParams)), "GET", $payLoad); $ret = new PaymentHistory(); @@ -206,8 +206,8 @@ class Payment extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/payment", "POST", $payLoad); $this->fromJson($json); @@ -228,8 +228,8 @@ class Payment extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/payment/$paymentid", "GET", $payLoad); $ret = new Payment(); @@ -255,8 +255,8 @@ class Payment extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/payment/{$this->getId()}/execute", "POST", $payLoad); $this->fromJson($json); diff --git a/lib/PayPal/Api/Refund.php b/lib/PayPal/Api/Refund.php index 89729ee..592fe4e 100644 --- a/lib/PayPal/Api/Refund.php +++ b/lib/PayPal/Api/Refund.php @@ -192,8 +192,8 @@ class Refund extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/refund/$refundid", "GET", $payLoad); $ret = new Refund(); diff --git a/lib/PayPal/Api/Sale.php b/lib/PayPal/Api/Sale.php index bcb0619..4b431f4 100644 --- a/lib/PayPal/Api/Sale.php +++ b/lib/PayPal/Api/Sale.php @@ -144,8 +144,8 @@ class Sale extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/sale/$saleid", "GET", $payLoad); $ret = new Sale(); @@ -171,8 +171,8 @@ class Sale extends Resource implements IResource { if($apiContext == null) { $apiContext = new ApiContext(self::$credential); } - $call = new \PPRestCall(); - $json = $call->execute($apiContext, array('PayPal\Rest\RestHandler'), + $call = new \PPRestCall($apiContext); + $json = $call->execute( array('PayPal\Rest\RestHandler'), "/v1/payments/sale/{$this->getId()}/refund", "POST", $payLoad); $this->fromJson($json); diff --git a/lib/PayPal/Auth/OAuthTokenCredential.php b/lib/PayPal/Auth/OAuthTokenCredential.php index 96e49c7..e5d375d 100644 --- a/lib/PayPal/Auth/OAuthTokenCredential.php +++ b/lib/PayPal/Auth/OAuthTokenCredential.php @@ -49,14 +49,15 @@ class OAuthTokenCredential { */ public function __construct($clientId, $clientSecret) { $this->clientId = $clientId; - $this->clientSecret = $clientSecret; - $this->logger = new \PPLoggingManager(__CLASS__); + $this->clientSecret = $clientSecret; } /** * @return the accessToken */ - public function getAccessToken($config) { + public function getAccessToken($config) { + + $this->logger = new \PPLoggingManager(__CLASS__, $config); // Check if Access Token is not null and has not expired. // The API returns expiry time as a relative time unit // We use a buffer time when checking for token expiry to account diff --git a/sample/bootstrap.php b/sample/bootstrap.php index 3d8f23c..5095a03 100644 --- a/sample/bootstrap.php +++ b/sample/bootstrap.php @@ -1,50 +1,57 @@ -::setCredential($cred) calls that -// you see in the samples. -$cred = new OAuthTokenCredential( - $configManager->get('acct1.ClientId'), - $configManager->get('acct1.ClientSecret')); - - -/** - * ### 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); -} +if(!file_exists(__DIR__ .'/vendor/autoload.php')) { + echo "The 'vendor' folder is missing. You must run 'composer update' to resolve application dependencies.\nPlease see the README for more information.\n"; + exit(1); +} +require __DIR__ . '/vendor/autoload.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' +)); +*/ + +/** + * ### 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); +} diff --git a/sample/payments/CreatePayment.php b/sample/payments/CreatePayment.php index 562a70d..258629f 100644 --- a/sample/payments/CreatePayment.php +++ b/sample/payments/CreatePayment.php @@ -1,111 +1,105 @@ -setLine1("3909 Witmer Road"); -$addr->setLine2("Niagara Falls"); -$addr->setCity("Niagara Falls"); -$addr->setState("NY"); -$addr->setPostal_code("14305"); -$addr->setCountry_code("US"); -$addr->setPhone("716-298-1822"); - -// ### CreditCard -// A resource representing a credit card that can be -// used to fund a payment. -$card = new CreditCard(); -$card->setType("visa"); -$card->setNumber("4417119669820331"); -$card->setExpire_month("11"); -$card->setExpire_year("2019"); -$card->setCvv2("012"); -$card->setFirst_name("Joe"); -$card->setLast_name("Shopper"); -$card->setBilling_address($addr); - -// ### FundingInstrument -// A resource representing a Payer's funding instrument. -// Use a Payer ID (A unique identifier of the payer generated -// and provided by the facilitator. This is required when -// creating or using a tokenized funding instrument) -// and the `CreditCardDetails` -$fi = new FundingInstrument(); -$fi->setCredit_card($card); - -// ### Payer -// A resource representing a Payer that funds a payment -// Use the List of `FundingInstrument` and the Payment Method -// as 'credit_card' -$payer = new Payer(); -$payer->setPayment_method("credit_card"); -$payer->setFunding_instruments(array($fi)); - -// ### Amount -// Let's you specify a payment amount. -$amount = new Amount(); -$amount->setCurrency("USD"); -$amount->setTotal("1.00"); - -// ### Transaction -// A transaction defines the contract of a -// payment - what is the payment for and who -// is fulfilling it. Transaction is created with -// a `Payee` and `Amount` types -$transaction = new Transaction(); -$transaction->setAmount($amount); -$transaction->setDescription("This is the payment description."); - -// ### Payment -// A Payment Resource; create one using -// the above types and intent as 'sale' -$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setTransactions(array($transaction)); - -// ### Api Context -// Pass in a `ApiContext` object to authenticate -// the call and to send a unique request id -// (that ensures idempotency). The SDK generates -// a request id if you do not pass one explicitly. -$apiContext = new ApiContext($cred, 'Request' . time()); - -// ### Create Payment -// Create a payment by posting to the APIService -// using a valid ApiContext -// The return object contains the status; -try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
- Created payment: - getId();?> -
-
toArray());?>
- Back - - +setLine1("3909 Witmer Road"); +$addr->setLine2("Niagara Falls"); +$addr->setCity("Niagara Falls"); +$addr->setState("NY"); +$addr->setPostal_code("14305"); +$addr->setCountry_code("US"); +$addr->setPhone("716-298-1822"); + +// ### CreditCard +// A resource representing a credit card that can be +// used to fund a payment. +$card = new CreditCard(); +$card->setType("visa"); +$card->setNumber("4417119669820331"); +$card->setExpire_month("11"); +$card->setExpire_year("2019"); +$card->setCvv2("012"); +$card->setFirst_name("Joe"); +$card->setLast_name("Shopper"); +$card->setBilling_address($addr); + +// ### FundingInstrument +// A resource representing a Payer's funding instrument. +// Use a Payer ID (A unique identifier of the payer generated +// and provided by the facilitator. This is required when +// creating or using a tokenized funding instrument) +// and the `CreditCardDetails` +$fi = new FundingInstrument(); +$fi->setCredit_card($card); + +// ### Payer +// A resource representing a Payer that funds a payment +// Use the List of `FundingInstrument` and the Payment Method +// as 'credit_card' +$payer = new Payer(); +$payer->setPayment_method("credit_card"); +$payer->setFunding_instruments(array($fi)); + +// ### Amount +// Let's you specify a payment amount. +$amount = new Amount(); +$amount->setCurrency("USD"); +$amount->setTotal("1.00"); + +// ### Transaction +// A transaction defines the contract of a +// payment - what is the payment for and who +// is fulfilling it. Transaction is created with +// a `Payee` and `Amount` types +$transaction = new Transaction(); +$transaction->setAmount($amount); +$transaction->setDescription("This is the payment description."); + +// ### Payment +// A Payment Resource; create one using +// the above types and intent as 'sale' +$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setTransactions(array($transaction)); + + + +// ### Create Payment +// Create a payment by posting to the APIService +// using a valid ApiContext (See bootstrap.php for more on `ApiContext`) +// The return object contains the status; +try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
+ Created payment: + getId();?> +
+
toArray());?>
+ Back + + diff --git a/sample/payments/CreatePaymentUsingPayPal.php b/sample/payments/CreatePaymentUsingPayPal.php index 8467c15..e4cf904 100644 --- a/sample/payments/CreatePaymentUsingPayPal.php +++ b/sample/payments/CreatePaymentUsingPayPal.php @@ -1,93 +1,86 @@ -setPayment_method("paypal"); - -// ### Amount -// Let's you specify a payment amount. -$amount = new Amount(); -$amount->setCurrency("USD"); -$amount->setTotal("1.00"); - -// ### Transaction -// A transaction defines the contract of a -// payment - what is the payment for and who -// is fulfilling it. Transaction is created with -// a `Payee` and `Amount` types -$transaction = new Transaction(); -$transaction->setAmount($amount); -$transaction->setDescription("This is the payment description."); - -// ### Redirect urls -// Set the urls that the buyer must be redirected to after -// payment approval/ cancellation. -$baseUrl = getBaseUrl(); -$redirectUrls = new RedirectUrls(); -$redirectUrls->setReturn_url("$baseUrl/ExecutePayment.php?success=true"); -$redirectUrls->setCancel_url("$baseUrl/ExecutePayment.php?success=false"); - -// ### Payment -// A Payment Resource; create one using -// the above types and intent as 'sale' -$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setRedirect_urls($redirectUrls); -$payment->setTransactions(array($transaction)); - -// ### Api Context -// Pass in a `ApiContext` object to authenticate -// the call and to send a unique request id -// (that ensures idempotency). The SDK generates -// a request id if you do not pass one explicitly. -$apiContext = new ApiContext($cred, 'Request' . time()); - -// ### Create Payment -// Create a payment by posting to the APIService -// using a valid apiContext -// The return object contains the status and the -// url to which the buyer must be redirected to -// for payment approval -try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} - -// ### Redirect buyer to paypal -// Retrieve buyer approval url from the `payment` object. -foreach($payment->getLinks() as $link) { - if($link->getRel() == 'approval_url') { - $redirectUrl = $link->getHref(); - } -} -// It is not really a great idea to store the payment id -// in the session. In a real world app, please store the -// payment id in a database. -$_SESSION['paymentId'] = $payment->getId(); -if(isset($redirectUrl)) { - header("Location: $redirectUrl"); - exit; -} +setPayment_method("paypal"); + +// ### Amount +// Let's you specify a payment amount. +$amount = new Amount(); +$amount->setCurrency("USD"); +$amount->setTotal("1.00"); + +// ### Transaction +// A transaction defines the contract of a +// payment - what is the payment for and who +// is fulfilling it. Transaction is created with +// a `Payee` and `Amount` types +$transaction = new Transaction(); +$transaction->setAmount($amount); +$transaction->setDescription("This is the payment description."); + +// ### Redirect urls +// Set the urls that the buyer must be redirected to after +// payment approval/ cancellation. +$baseUrl = getBaseUrl(); +$redirectUrls = new RedirectUrls(); +$redirectUrls->setReturn_url("$baseUrl/ExecutePayment.php?success=true"); +$redirectUrls->setCancel_url("$baseUrl/ExecutePayment.php?success=false"); + +// ### Payment +// A Payment Resource; create one using +// the above types and intent as 'sale' +$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setRedirect_urls($redirectUrls); +$payment->setTransactions(array($transaction)); + +// ### Create Payment +// Create a payment by posting to the APIService +// using a valid apiContext. +// (See bootstrap.php for more on `ApiContext`) +// The return object contains the status and the +// url to which the buyer must be redirected to +// for payment approval +try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} + +// ### Redirect buyer to paypal +// Retrieve buyer approval url from the `payment` object. +foreach($payment->getLinks() as $link) { + if($link->getRel() == 'approval_url') { + $redirectUrl = $link->getHref(); + } +} +// It is not really a great idea to store the payment id +// in the session. In a real world app, please store the +// payment id in a database. +$_SESSION['paymentId'] = $payment->getId(); +if(isset($redirectUrl)) { + header("Location: $redirectUrl"); + exit; +} diff --git a/sample/payments/CreatePaymentUsingSavedCard.php b/sample/payments/CreatePaymentUsingSavedCard.php index 5994c7c..40c9bf6 100644 --- a/sample/payments/CreatePaymentUsingSavedCard.php +++ b/sample/payments/CreatePaymentUsingSavedCard.php @@ -1,96 +1,88 @@ -setCredit_card_id($creditCardId); - -// ### FundingInstrument -// A resource representing a Payer's funding instrument. -// Use a Payer ID (A unique identifier of the payer generated -// and provided by the facilitator. This is required when -// creating or using a tokenized funding instrument) -// and the `CreditCardDetails` -$fi = new FundingInstrument(); -$fi->setCredit_card_token($creditCardToken); - -// ### Payer -// A resource representing a Payer that funds a payment -// Use the List of `FundingInstrument` and the Payment Method -// as 'credit_card' -$payer = new Payer(); -$payer->setPayment_method("credit_card"); -$payer->setFunding_instruments(array($fi)); - -// ### Amount -// Let's you specify a payment amount. -$amount = new Amount(); -$amount->setCurrency("USD"); -$amount->setTotal("1.00"); - -// ### Transaction -// A transaction defines the contract of a -// payment - what is the payment for and who -// is fulfilling it. Transaction is created with -// a `Payee` and `Amount` types -$transaction = new Transaction(); -$transaction->setAmount($amount); -$transaction->setDescription("This is the payment description."); - -// ### Payment -// A Payment Resource; create one using -// the above types and intent as 'sale' -$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setTransactions(array($transaction)); - -// ### Api Context -// Pass in a `ApiContext` object to authenticate -// the call and to send a unique request id -// (that ensures idempotency). The SDK generates -// a request id if you do not pass one explicitly. -$apiContext = new ApiContext($cred, 'Request' . time()); - -// ###Create Payment -// Create a payment by posting to the APIService -// using a valid apiContext -// The return object contains the status; -try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
- Created payment: - getId();?> -
-
toArray());?>
- Back - +setCredit_card_id($creditCardId); + +// ### FundingInstrument +// A resource representing a Payer's funding instrument. +// Use a Payer ID (A unique identifier of the payer generated +// and provided by the facilitator. This is required when +// creating or using a tokenized funding instrument) +// and the `CreditCardDetails` +$fi = new FundingInstrument(); +$fi->setCredit_card_token($creditCardToken); + +// ### Payer +// A resource representing a Payer that funds a payment +// Use the List of `FundingInstrument` and the Payment Method +// as 'credit_card' +$payer = new Payer(); +$payer->setPayment_method("credit_card"); +$payer->setFunding_instruments(array($fi)); + +// ### Amount +// Let's you specify a payment amount. +$amount = new Amount(); +$amount->setCurrency("USD"); +$amount->setTotal("1.00"); + +// ### Transaction +// A transaction defines the contract of a +// payment - what is the payment for and who +// is fulfilling it. Transaction is created with +// a `Payee` and `Amount` types +$transaction = new Transaction(); +$transaction->setAmount($amount); +$transaction->setDescription("This is the payment description."); + +// ### Payment +// A Payment Resource; create one using +// the above types and intent as 'sale' +$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setTransactions(array($transaction)); + +// ###Create Payment +// Create a payment by posting to the APIService +// (See bootstrap.php for more on `ApiContext`) +// The return object contains the status; +try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
+ Created payment: + getId();?> +
+
toArray());?>
+ Back + \ No newline at end of file diff --git a/sample/payments/ExecutePayment.php b/sample/payments/ExecutePayment.php index 45224fd..6827c99 100644 --- a/sample/payments/ExecutePayment.php +++ b/sample/payments/ExecutePayment.php @@ -1,47 +1,40 @@ -/execute'. - -require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\ExecutePayment; -use PayPal\Api\Payment; -use PayPal\Api\PaymentExecution; -use PayPal\Rest\ApiContext; -session_start(); - -if(isset($_GET['success']) && $_GET['success'] == 'true') { - // ### Api Context - // Pass in a `ApiContext` object to authenticate - // the call and to send a unique request id - // (that ensures idempotency). The SDK generates - // a request id if you do not pass one explicitly. - $apiContext = new ApiContext($cred); - - // Get the payment Object by passing paymentId - // payment id was previously stored in session in - // CreatePaymentUsingPayPal.php - $paymentId = $_SESSION['paymentId']; - $payment = Payment::get($paymentId); - - // PaymentExecution object includes information necessary - // to execute a PayPal account payment. - // The payer_id is added to the request query parameters - // when the user is redirected from paypal back to your site - $execution = new PaymentExecution(); - $execution->setPayer_id($_GET['PayerID']); - - //Execute the payment - $payment->execute($execution, $apiContext); - - echo "
";
-	var_dump($payment->toArray());
-	echo "
Back"; - -} else { - echo "User cancelled payment."; +/execute'. + +require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\ExecutePayment; +use PayPal\Api\Payment; +use PayPal\Api\PaymentExecution; +session_start(); +if(isset($_GET['success']) && $_GET['success'] == 'true') { + + // Get the payment Object by passing paymentId + // payment id was previously stored in session in + // CreatePaymentUsingPayPal.php + $paymentId = $_SESSION['paymentId']; + $payment = Payment::get($paymentId, $apiContext); + + // PaymentExecution object includes information necessary + // to execute a PayPal account payment. + // The payer_id is added to the request query parameters + // when the user is redirected from paypal back to your site + $execution = new PaymentExecution(); + $execution->setPayer_id($_GET['PayerID']); + + //Execute the payment + // (See bootstrap.php for more on `ApiContext`) + $payment->execute($execution, $apiContext); + + echo "
";
+	var_dump($payment->toArray());
+	echo "
Back"; + +} else { + echo "User cancelled payment."; } \ No newline at end of file diff --git a/sample/payments/GetPayment.php b/sample/payments/GetPayment.php index 97415ca..288d49e 100644 --- a/sample/payments/GetPayment.php +++ b/sample/payments/GetPayment.php @@ -1,39 +1,36 @@ -getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Retrieving Payment ID:
-
toArray());?>
- Back - +getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Retrieving Payment ID:
+
toArray());?>
+ Back + \ No newline at end of file diff --git a/sample/payments/ListPayments.php b/sample/payments/ListPayments.php index c11396c..4f950c4 100644 --- a/sample/payments/ListPayments.php +++ b/sample/payments/ListPayments.php @@ -1,42 +1,37 @@ - 10, 'start_index' => 5)); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Got getCount(); ?> matching payments
-
toArray());?>
- Back - - + 10, 'start_index' => 5), $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Got getCount(); ?> matching payments
+
toArray());?>
+ Back + + diff --git a/sample/sale/GetSale.php b/sample/sale/GetSale.php index 9ac1697..521f445 100644 --- a/sample/sale/GetSale.php +++ b/sample/sale/GetSale.php @@ -1,35 +1,30 @@ -getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Retrieving sale id:
-
- Back - +getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Retrieving sale id:
+
+ Back + \ No newline at end of file diff --git a/sample/sale/RefundSale.php b/sample/sale/RefundSale.php index 072af6d..bcf9df7 100644 --- a/sample/sale/RefundSale.php +++ b/sample/sale/RefundSale.php @@ -1,55 +1,49 @@ -setCurrency('USD'); -$amt->setTotal('0.01'); - -$refund = new Refund(); -$refund->setAmount($amt); - -$saleId = '3RM92092UW5126232'; - -// ###Sale -// A sale transaction. -// Create a Sale object with the -// given sale transaction id. -$sale = new Sale(); -$sale->setId($saleId); - -// ### Api Context -// Pass in a `ApiContext` object to authenticate -// the call and to send a unique request id -// (that ensures idempotency). The SDK generates -// a request id if you do not pass one explicitly. -$apiContext = new ApiContext($cred, 'Request' . time()); -try { - // Refund the sale - $sale->refund($refund, $apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Refunding sale id:
-
- Back - +setCurrency('USD'); +$amt->setTotal('0.01'); + +$refund = new Refund(); +$refund->setAmount($amt); + +$saleId = '3RM92092UW5126232'; + +// ###Sale +// A sale transaction. +// Create a Sale object with the +// given sale transaction id. +$sale = new Sale(); +$sale->setId($saleId); + +try { + // Refund the sale + // (See bootstrap.php for more on `ApiContext`) + $sale->refund($refund, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Refunding sale id:
+
+ Back + \ No newline at end of file diff --git a/sample/sdk_config.ini b/sample/sdk_config.ini index 69de99f..4906f19 100644 --- a/sample/sdk_config.ini +++ b/sample/sdk_config.ini @@ -1,35 +1,26 @@ -;Account credentials from developer portal -[Account] -acct1.ClientId = EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM -acct1.ClientSecret = EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM - - -;Connection Information -[Http] -http.ConnectionTimeOut = 30 -http.Retry = 1 -;http.Proxy=http://[username:password]@hostname[:port][/path] - - -;Service Configuration -[Service] -service.EndPoint="https://api.sandbox.paypal.com" -; Uncomment this line for integrating with the live endpoint -; service.EndPoint="https://api.paypal.com" - - -;Logging Information -[Log] - -log.LogEnabled=true - -# When using a relative path, the log file is created -# relative to the .php file that is the entry point -# for this request. You can also provide an absolute -# path here -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 +;Connection Information +[Http] +http.ConnectionTimeOut = 30 +http.Retry = 1 +;http.Proxy=http://[username:password]@hostname[:port][/path] + + +;Service Configuration +[Service] +mode=sandbox ; can be set to sandbox / live + +;Logging Information +[Log] + +log.LogEnabled=true + +# When using a relative path, the log file is created +# relative to the .php file that is the entry point +# for this request. You can also provide an absolute +# path here +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 \ No newline at end of file diff --git a/sample/source/CreateCreditCard.html b/sample/source/CreateCreditCard.html index 86eaa6f..23b4c92 100644 --- a/sample/source/CreateCreditCard.html +++ b/sample/source/CreateCreditCard.html @@ -7,42 +7,36 @@ a payment in the future. The following code demonstrates how can save a Credit Card on PayPal using the Vault API. -API used: POST /v1/vault/credit-card

use PayPal\Rest\ApiContext; - -require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\CreditCard; +API used: POST /v1/vault/credit-card

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\CreditCard; use PayPal\Api\Address;

CreditCard

A resource representing a credit card that can be -used to fund a payment.

$card = new CreditCard(); -$card->setType("visa"); -$card->setNumber("4417119669820331"); -$card->setExpire_month("11"); -$card->setExpire_year("2019"); -$card->setCvv2("012"); -$card->setFirst_name("Joe"); -$card->setLast_name("Shopper");

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred, 'Request' . time());

Save card

+used to fund a payment.

$card = new CreditCard(); +$card->setType("visa"); +$card->setNumber("4417119669820331"); +$card->setExpire_month("11"); +$card->setExpire_year("2019"); +$card->setCvv2("012"); +$card->setFirst_name("Joe"); +$card->setLast_name("Shopper");

Save card

Creates the credit card as a resource in the PayPal vault. The response contains an 'id' that you can use to refer to it -in the future payments.

try { - $card->create(); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Saved a new credit card with id: <?php echo $card->getId();?></div> - <pre><?php var_dump($card);?></pre> - <a href='../index.html'>Back</a> -</body> +in the future payments. +(See bootstrap.php for more on ApiContext)

try { + $card->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Saved a new credit card with id: <?php echo $card->getId();?></div> + <pre><?php var_dump($card);?></pre> + <a href='../index.html'>Back</a> +</body> </html>
Back
\ No newline at end of file diff --git a/sample/source/CreatePayment.html b/sample/source/CreatePayment.html index 284662e..11c231e 100644 --- a/sample/source/CreatePayment.html +++ b/sample/source/CreatePayment.html @@ -2,89 +2,83 @@

This sample code demonstrate how you can process a payment with a credit card. -API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Address; -use PayPal\Api\Amount; -use PayPal\Api\CreditCard; -use PayPal\Api\Payer; -use PayPal\Api\Payment; -use PayPal\Api\FundingInstrument; -use PayPal\Api\Transaction; -use PayPal\Rest\ApiContext;

Address

+API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Address; +use PayPal\Api\Amount; +use PayPal\Api\CreditCard; +use PayPal\Api\Payer; +use PayPal\Api\Payment; +use PayPal\Api\FundingInstrument; +use PayPal\Api\Transaction;

Address

Base Address object used as shipping or billing -address in a payment. [Optional]

$addr = new Address(); -$addr->setLine1("3909 Witmer Road"); -$addr->setLine2("Niagara Falls"); -$addr->setCity("Niagara Falls"); -$addr->setState("NY"); -$addr->setPostal_code("14305"); -$addr->setCountry_code("US"); +address in a payment. [Optional]

$addr = new Address(); +$addr->setLine1("3909 Witmer Road"); +$addr->setLine2("Niagara Falls"); +$addr->setCity("Niagara Falls"); +$addr->setState("NY"); +$addr->setPostal_code("14305"); +$addr->setCountry_code("US"); $addr->setPhone("716-298-1822");

CreditCard

A resource representing a credit card that can be -used to fund a payment.

$card = new CreditCard(); -$card->setType("visa"); -$card->setNumber("4417119669820331"); -$card->setExpire_month("11"); -$card->setExpire_year("2019"); -$card->setCvv2("012"); -$card->setFirst_name("Joe"); -$card->setLast_name("Shopper"); +used to fund a payment.

$card = new CreditCard(); +$card->setType("visa"); +$card->setNumber("4417119669820331"); +$card->setExpire_month("11"); +$card->setExpire_year("2019"); +$card->setCvv2("012"); +$card->setFirst_name("Joe"); +$card->setLast_name("Shopper"); $card->setBilling_address($addr);

FundingInstrument

A resource representing a Payer's funding instrument. Use a Payer ID (A unique identifier of the payer generated and provided by the facilitator. This is required when creating or using a tokenized funding instrument) -and the CreditCardDetails

$fi = new FundingInstrument(); +and the CreditCardDetails

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

Payer

A resource representing a Payer that funds a payment Use the List of FundingInstrument and the Payment Method -as 'credit_card'

$payer = new Payer(); -$payer->setPayment_method("credit_card"); +as 'credit_card'

$payer = new Payer(); +$payer->setPayment_method("credit_card"); $payer->setFunding_instruments(array($fi));

Amount

-

Let's you specify a payment amount.

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

Let's you specify a payment amount.

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

Transaction

A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. Transaction is created with -a Payee and Amount types

$transaction = new Transaction(); -$transaction->setAmount($amount); +a Payee and Amount types

$transaction = new Transaction(); +$transaction->setAmount($amount); $transaction->setDescription("This is the payment description.");

Payment

A Payment Resource; create one using -the above types and intent as 'sale'

$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setTransactions(array($transaction));

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred, 'Request' . time());

Create Payment

+the above types and intent as 'sale'

$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setTransactions(array($transaction));

Create Payment

Create a payment by posting to the APIService -using a valid ApiContext -The return object contains the status;

try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - <pre><?php var_dump($card);?></pre> - exit(1); -} -?> -<html> -<body> - <div> - Created payment: - <?php echo $payment->getId();?> - </div> - <pre><?php var_dump($payment->toArray());?></pre> - <a href='../index.html'>Back</a> -</body> -</html>
Back
+using a valid ApiContext (See bootstrap.php for more on ApiContext) +The return object contains the status;

try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div> + Created payment: + <?php echo $payment->getId();?> + </div> + <pre><?php var_dump($payment->toArray());?></pre> + <a href='../index.html'>Back</a> +</body> +</html>
Back
\ No newline at end of file diff --git a/sample/source/CreatePaymentUsingPayPal.html b/sample/source/CreatePaymentUsingPayPal.html index 7a03153..3282114 100644 --- a/sample/source/CreatePaymentUsingPayPal.html +++ b/sample/source/CreatePaymentUsingPayPal.html @@ -2,71 +2,66 @@

This sample code demonstrates how you can process a PayPal Account based Payment. -API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Address; -use PayPal\Api\Amount; -use PayPal\Api\Payer; -use PayPal\Api\Payment; -use PayPal\Api\FundingInstrument; -use PayPal\Api\RedirectUrls; -use PayPal\Api\Transaction; -use PayPal\Rest\ApiContext; +API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Address; +use PayPal\Api\Amount; +use PayPal\Api\Payer; +use PayPal\Api\Payment; +use PayPal\Api\FundingInstrument; +use PayPal\Api\RedirectUrls; +use PayPal\Api\Transaction; session_start();

Payer

A resource representing a Payer that funds a payment Use the List of FundingInstrument and the Payment Method -as 'credit_card'

$payer = new Payer(); +as 'credit_card'

$payer = new Payer(); $payer->setPayment_method("paypal");

Amount

-

Let's you specify a payment amount.

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

Let's you specify a payment amount.

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

Transaction

A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. Transaction is created with -a Payee and Amount types

$transaction = new Transaction(); -$transaction->setAmount($amount); +a Payee and Amount types

$transaction = new Transaction(); +$transaction->setAmount($amount); $transaction->setDescription("This is the payment description.");

Redirect urls

Set the urls that the buyer must be redirected to after -payment approval/ cancellation.

$baseUrl = getBaseUrl(); -$redirectUrls = new RedirectUrls(); -$redirectUrls->setReturn_url("$baseUrl/ExecutePayment.php?success=true"); +payment approval/ cancellation.

$baseUrl = getBaseUrl(); +$redirectUrls = new RedirectUrls(); +$redirectUrls->setReturn_url("$baseUrl/ExecutePayment.php?success=true"); $redirectUrls->setCancel_url("$baseUrl/ExecutePayment.php?success=false");

Payment

A Payment Resource; create one using -the above types and intent as 'sale'

$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setRedirect_urls($redirectUrls); -$payment->setTransactions(array($transaction));

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred, 'Request' . time());

Create Payment

+the above types and intent as 'sale'

$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setRedirect_urls($redirectUrls); +$payment->setTransactions(array($transaction));

Create Payment

Create a payment by posting to the APIService -using a valid apiContext +using a valid apiContext. +(See bootstrap.php for more on ApiContext) The return object contains the status and the url to which the buyer must be redirected to -for payment approval

try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -}

Redirect buyer to paypal

+for payment approval

try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +}

Redirect buyer to paypal

-

Retrieve buyer approval url from the payment object.

foreach($payment->getLinks() as $link) { - if($link->getRel() == 'approval_url') { - $redirectUrl = $link->getHref(); - } -}

It is not really a great idea to store the payment id +

Retrieve buyer approval url from the payment object.

foreach($payment->getLinks() as $link) { + if($link->getRel() == 'approval_url') { + $redirectUrl = $link->getHref(); + } +}

It is not really a great idea to store the payment id in the session. In a real world app, please store the -payment id in a database.

$_SESSION['paymentId'] = $payment->getId(); -if(isset($redirectUrl)) { - header("Location: $redirectUrl"); - exit; +payment id in a database.

$_SESSION['paymentId'] = $payment->getId(); +if(isset($redirectUrl)) { + header("Location: $redirectUrl"); + exit; }
Back
\ No newline at end of file diff --git a/sample/source/CreatePaymentUsingSavedCard.html b/sample/source/CreatePaymentUsingSavedCard.html index b4d90d0..9e070fa 100644 --- a/sample/source/CreatePaymentUsingSavedCard.html +++ b/sample/source/CreatePaymentUsingSavedCard.html @@ -2,76 +2,70 @@

This sample code demonstrates how you can process a Payment using a previously saved credit card. -API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Address; -use PayPal\Api\Amount; -use PayPal\Api\CreditCard; -use PayPal\Api\CreditCardToken; -use PayPal\Api\Payer; -use PayPal\Api\Payment; -use PayPal\Api\FundingInstrument; -use PayPal\Api\RedirectUrls; -use PayPal\Api\Transaction; -use PayPal\Auth\OAuthTokenCredential; -use PayPal\Rest\ApiContext;

Credit card token

+API used: /v1/payments/payment

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Address; +use PayPal\Api\Amount; +use PayPal\Api\CreditCard; +use PayPal\Api\CreditCardToken; +use PayPal\Api\Payer; +use PayPal\Api\Payment; +use PayPal\Api\FundingInstrument; +use PayPal\Api\RedirectUrls; +use PayPal\Api\Transaction; +use PayPal\Auth\OAuthTokenCredential;

Credit card token

Saved credit card id from a previous call to -CreateCreditCard.php

$creditCardId = 'CARD-5BT058015C739554AKE2GCEI'; -$creditCardToken = new CreditCardToken(); +CreateCreditCard.php

$creditCardId = 'CARD-5BT058015C739554AKE2GCEI'; +$creditCardToken = new CreditCardToken(); $creditCardToken->setCredit_card_id($creditCardId);

FundingInstrument

A resource representing a Payer's funding instrument. Use a Payer ID (A unique identifier of the payer generated and provided by the facilitator. This is required when creating or using a tokenized funding instrument) -and the CreditCardDetails

$fi = new FundingInstrument(); +and the CreditCardDetails

$fi = new FundingInstrument(); $fi->setCredit_card_token($creditCardToken);

Payer

A resource representing a Payer that funds a payment Use the List of FundingInstrument and the Payment Method -as 'credit_card'

$payer = new Payer(); -$payer->setPayment_method("credit_card"); +as 'credit_card'

$payer = new Payer(); +$payer->setPayment_method("credit_card"); $payer->setFunding_instruments(array($fi));

Amount

-

Let's you specify a payment amount.

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

Let's you specify a payment amount.

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

Transaction

A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. Transaction is created with -a Payee and Amount types

$transaction = new Transaction(); -$transaction->setAmount($amount); +a Payee and Amount types

$transaction = new Transaction(); +$transaction->setAmount($amount); $transaction->setDescription("This is the payment description.");

Payment

A Payment Resource; create one using -the above types and intent as 'sale'

$payment = new Payment(); -$payment->setIntent("sale"); -$payment->setPayer($payer); -$payment->setTransactions(array($transaction));

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred, 'Request' . time());

Create Payment

+the above types and intent as 'sale'

$payment = new Payment(); +$payment->setIntent("sale"); +$payment->setPayer($payer); +$payment->setTransactions(array($transaction));

Create Payment

Create a payment by posting to the APIService -using a valid apiContext -The return object contains the status;

try { - $payment->create($apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div> - Created payment: - <?php echo $payment->getId();?> - </div> - <pre><?php var_dump($payment->toArray());?></pre> - <a href='../index.html'>Back</a> -</body> +(See bootstrap.php for more on ApiContext) +The return object contains the status;

try { + $payment->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div> + Created payment: + <?php echo $payment->getId();?> + </div> + <pre><?php var_dump($payment->toArray());?></pre> + <a href='../index.html'>Back</a> +</body> </html>
Back
\ No newline at end of file diff --git a/sample/source/ExecutePayment.html b/sample/source/ExecutePayment.html index 5f007b7..ab77e3d 100644 --- a/sample/source/ExecutePayment.html +++ b/sample/source/ExecutePayment.html @@ -5,36 +5,28 @@ a payment that has been approved by the buyer by logging into paypal site. You can optionally update transaction information by passing in one or more transactions. -API used: POST '/v1/payments/payment//execute'.

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\ExecutePayment; -use PayPal\Api\Payment; -use PayPal\Api\PaymentExecution; -use PayPal\Rest\ApiContext; -session_start(); - -if(isset($_GET['success']) && $_GET['success'] == 'true') {

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred); -

Get the payment Object by passing paymentId +API used: POST '/v1/payments/payment//execute'.

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\ExecutePayment; +use PayPal\Api\Payment; +use PayPal\Api\PaymentExecution; +session_start(); +if(isset($_GET['success']) && $_GET['success'] == 'true') { +

Get the payment Object by passing paymentId payment id was previously stored in session in -CreatePaymentUsingPayPal.php

$paymentId = $_SESSION['paymentId']; - $payment = Payment::get($paymentId); -

PaymentExecution object includes information necessary +CreatePaymentUsingPayPal.php

$paymentId = $_SESSION['paymentId']; + $payment = Payment::get($paymentId, $apiContext); +

PaymentExecution object includes information necessary to execute a PayPal account payment. The payer_id is added to the request query parameters -when the user is redirected from paypal back to your site

$execution = new PaymentExecution(); - $execution->setPayer_id($_GET['PayerID']); - - //Execute the payment - $payment->execute($execution, $apiContext); - - echo "<html><body><pre>"; - var_dump($payment->toArray()); - echo "</pre><a href='../index.html'>Back</a></body></html>"; - -} else { - echo "User cancelled payment."; +when the user is redirected from paypal back to your site

$execution = new PaymentExecution(); + $execution->setPayer_id($_GET['PayerID']); + + //Execute the payment

(See bootstrap.php for more on ApiContext)

$payment->execute($execution, $apiContext); + + echo "<html><body><pre>"; + var_dump($payment->toArray()); + echo "</pre><a href='../index.html'>Back</a></body></html>"; + +} else { + echo "User cancelled payment."; }
\ No newline at end of file diff --git a/sample/source/GetCreditCard.html b/sample/source/GetCreditCard.html index b22c75d..a46eaae 100644 --- a/sample/source/GetCreditCard.html +++ b/sample/source/GetCreditCard.html @@ -5,27 +5,22 @@ retrieve previously saved CreditCards, by sending a GET request to the URI '/v1/vault/credit-card' The following code takes you through -the process of retrieving a saved CreditCard

require __DIR__ . '/../bootstrap.php'; +the process of retrieving a saved CreditCard

require __DIR__ . '/../bootstrap.php'; use PayPal\Api\CreditCard;

The cardId can be obtained from a previous save credit -card operation. Use $card->getId()

$cardId = "CARD-5BT058015C739554AKE2GCEI";

Authentication

- -

Pass in a OAuthTokenCredential object -explicilty to authenticate the call. -If you skip this step, the client id/secret -set in the config file will be used.

CreditCard::setCredential($cred); -/// ### Retrieve card -try { - $card = CreditCard::get($cardId); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Retrieving credit card: <?php echo $cardId;?></div> - <pre><?php var_dump($card);?></pre> - <a href='../index.html'>Back</a> -</body> +card operation. Use $card->getId()

$cardId = "CARD-5BT058015C739554AKE2GCEI"; + +/// ### Retrieve card

(See bootstrap.php for more on ApiContext)

try { + $card = CreditCard::get($cardId, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Retrieving credit card: <?php echo $cardId;?></div> + <pre><?php var_dump($card);?></pre> + <a href='../index.html'>Back</a> +</body> </html>
\ No newline at end of file diff --git a/sample/source/GetPayment.html b/sample/source/GetPayment.html index 4805cee..0f451bf 100644 --- a/sample/source/GetPayment.html +++ b/sample/source/GetPayment.html @@ -6,29 +6,27 @@ you've created using the Payments API. Note various query parameters that you can use to filter, and paginate through the payments list. -API used: GET /v1/payments/payments

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Payment; - -$paymentId = "PAY-0XL713371A312273YKE2GCNI";

Authentication

- -

Pass in a OAuthTokenCredential object -explicilty to authenticate the call.

Payment::setCredential($cred);

Retrieve payment

+API used: GET /v1/payments/payments

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Payment; + +$paymentId = "PAY-0XL713371A312273YKE2GCNI";

Retrieve payment

Retrieve the payment object by calling the static get method on the Payment class by passing a valid -Payment ID

try { - $payment = Payment::get($paymentId); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Retrieving Payment ID: <?php echo $paymentId;?></div> - <pre><?php var_dump($payment->toArray());?></pre> - <a href='../index.html'>Back</a> -</body> +Payment ID +(See bootstrap.php for more on ApiContext)

try { + $payment = Payment::get($paymentId, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Retrieving Payment ID: <?php echo $paymentId;?></div> + <pre><?php var_dump($payment->toArray());?></pre> + <a href='../index.html'>Back</a> +</body> </html>
\ No newline at end of file diff --git a/sample/source/GetSale.html b/sample/source/GetSale.html index 0c02368..fd2c9ff 100644 --- a/sample/source/GetSale.html +++ b/sample/source/GetSale.html @@ -2,29 +2,25 @@

This sample code demonstrates how you can retrieve details of completed Sale Transaction. -API used: /v1/payments/sale/{sale-id}

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Sale; - -$saleId = '3RM92092UW5126232';

Authentication

- -

Pass in a OAuthTokenCredential object -explicilty to authenticate the call. -If you skip this step, the client id/secret -set in the config file will be used.

Sale::setCredential($cred); -try {

Retrieve the sale object

+API used: /v1/payments/sale/{sale-id}

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Sale; + +$saleId = '3RM92092UW5126232'; + +try {

Retrieve the sale object

Pass the ID of the sale -transaction from your payment resource.

$sale = Sale::get($saleId); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Retrieving sale id: <?php echo $saleId;?></div> - <pre><?php var_dump($sale);?></pre> - <a href='../index.html'>Back</a> -</body> +transaction from your payment resource.

$sale = Sale::get($saleId, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Retrieving sale id: <?php echo $saleId;?></div> + <pre><?php var_dump($sale);?></pre> + <a href='../index.html'>Back</a> +</body> </html>
\ No newline at end of file diff --git a/sample/source/ListPayments.html b/sample/source/ListPayments.html index d6ac87c..c86444d 100644 --- a/sample/source/ListPayments.html +++ b/sample/source/ListPayments.html @@ -6,30 +6,26 @@ you've created using the Payments API. Note various query parameters that you can use to filter, and paginate through the payments list. -API used: GET /v1/payments/payments

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Payment;

Authentication

- -

Pass in a OAuthTokenCredential object -explicilty to authenticate the call. -If you skip this step, the client id/secret -set in the config file will be used.

Payment::setCredential($cred);

Retrieve payment

+API used: GET /v1/payments/payments

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Payment;

Retrieve payment

Retrieve the PaymentHistory object by calling the static get method on the Payment class, and pass a Map object that contains query parameters for paginations and filtering. -Refer the method doc for valid values for keys

try { - $payments = Payment::all(array('count' => 10, 'start_index' => 5)); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Got <?php echo $payments->getCount(); ?> matching payments </div> - <pre><?php var_dump($payments->toArray());?></pre> - <a href='../index.html'>Back</a> -</body> +Refer the method doc for valid values for keys +(See bootstrap.php for more on ApiContext)

try { + $payments = Payment::all(array('count' => 10, 'start_index' => 5), $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Got <?php echo $payments->getCount(); ?> matching payments </div> + <pre><?php var_dump($payments->toArray());?></pre> + <a href='../index.html'>Back</a> +</body> </html>
\ No newline at end of file diff --git a/sample/source/RefundSale.html b/sample/source/RefundSale.html index 87c2455..3b8375d 100644 --- a/sample/source/RefundSale.html +++ b/sample/source/RefundSale.html @@ -3,42 +3,38 @@

This sample code demonstrate how you can process a refund on a sale transaction created using the Payments API. -API used: /v1/payments/sale/{sale-id}/refund

require __DIR__ . '/../bootstrap.php'; -use PayPal\Api\Amount; -use PayPal\Api\Refund; -use PayPal\Api\Sale; -use PayPal\Rest\ApiContext;

Refund

+API used: /v1/payments/sale/{sale-id}/refund

require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\Amount; +use PayPal\Api\Refund; +use PayPal\Api\Sale;

Refund

Create a refund object indicating -refund amount

$amt = new Amount(); -$amt->setCurrency('USD'); -$amt->setTotal('0.01'); - -$refund = new Refund(); -$refund->setAmount($amt); - +refund amount

$amt = new Amount(); +$amt->setCurrency('USD'); +$amt->setTotal('0.01'); + +$refund = new Refund(); +$refund->setAmount($amt); + $saleId = '3RM92092UW5126232';

Sale

A sale transaction. Create a Sale object with the -given sale transaction id.

$sale = new Sale(); -$sale->setId($saleId);

Api Context

- -

Pass in a ApiContext object to authenticate -the call and to send a unique request id -(that ensures idempotency). The SDK generates -a request id if you do not pass one explicitly.

$apiContext = new ApiContext($cred, 'Request' . time()); -try {

Refund the sale

$sale->refund($refund, $apiContext); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> -<html> -<body> - <div>Refunding sale id: <?php echo $saleId;?></div> - <pre><?php var_dump($sale);?></pre> - <a href='../index.html'>Back</a> -</body> +given sale transaction id.

$sale = new Sale(); +$sale->setId($saleId); + +try {

Refund the sale +(See bootstrap.php for more on ApiContext)

$sale->refund($refund, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> +<html> +<body> + <div>Refunding sale id: <?php echo $saleId;?></div> + <pre><?php var_dump($sale);?></pre> + <a href='../index.html'>Back</a> +</body> </html>
\ No newline at end of file diff --git a/sample/vault/CreateCreditCard.php b/sample/vault/CreateCreditCard.php index 1bcf1ce..bf50d4c 100644 --- a/sample/vault/CreateCreditCard.php +++ b/sample/vault/CreateCreditCard.php @@ -1,57 +1,50 @@ -setType("visa"); -$card->setNumber("4417119669820331"); -$card->setExpire_month("11"); -$card->setExpire_year("2019"); -$card->setCvv2("012"); -$card->setFirst_name("Joe"); -$card->setLast_name("Shopper"); - -// ### Api Context -// Pass in a `ApiContext` object to authenticate -// the call and to send a unique request id -// (that ensures idempotency). The SDK generates -// a request id if you do not pass one explicitly. -$apiContext = new ApiContext($cred, 'Request' . time()); - -// ### Save card -// Creates the credit card as a resource -// in the PayPal vault. The response contains -// an 'id' that you can use to refer to it -// in the future payments. -try { - $card->create(); -} catch (\PPConnectionException $ex) { - echo "Exception:" . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Saved a new credit card with id: getId();?>
-
- Back - + +require __DIR__ . '/../bootstrap.php'; +use PayPal\Api\CreditCard; +use PayPal\Api\Address; + +// ### CreditCard +// A resource representing a credit card that can be +// used to fund a payment. +$card = new CreditCard(); +$card->setType("visa"); +$card->setNumber("4417119669820331"); +$card->setExpire_month("11"); +$card->setExpire_year("2019"); +$card->setCvv2("012"); +$card->setFirst_name("Joe"); +$card->setLast_name("Shopper"); + +// ### Save card +// Creates the credit card as a resource +// in the PayPal vault. The response contains +// an 'id' that you can use to refer to it +// in the future payments. +// (See bootstrap.php for more on `ApiContext`) +try { + $card->create($apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception:" . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Saved a new credit card with id: getId();?>
+
+ Back + \ No newline at end of file diff --git a/sample/vault/GetCreditCard.php b/sample/vault/GetCreditCard.php index 4246b17..815bfaf 100644 --- a/sample/vault/GetCreditCard.php +++ b/sample/vault/GetCreditCard.php @@ -1,38 +1,33 @@ -getId() -$cardId = "CARD-5BT058015C739554AKE2GCEI"; - -// ### Authentication -// Pass in a `OAuthTokenCredential` object -// explicilty to authenticate the call. -// If you skip this step, the client id/secret -// set in the config file will be used. -CreditCard::setCredential($cred); -/// ### Retrieve card -try { - $card = CreditCard::get($cardId); -} catch (\PPConnectionException $ex) { - echo "Exception: " . $ex->getMessage() . PHP_EOL; - var_dump($ex->getData()); - exit(1); -} -?> - - -
Retrieving credit card:
-
- Back - +getId() +$cardId = "CARD-5BT058015C739554AKE2GCEI"; + +/// ### Retrieve card +// (See bootstrap.php for more on `ApiContext`) +try { + $card = CreditCard::get($cardId, $apiContext); +} catch (\PPConnectionException $ex) { + echo "Exception: " . $ex->getMessage() . PHP_EOL; + var_dump($ex->getData()); + exit(1); +} +?> + + +
Retrieving credit card:
+
+ Back + \ No newline at end of file