diff --git a/sample/common.php b/sample/common.php index 2ab665f..a75d79c 100644 --- a/sample/common.php +++ b/sample/common.php @@ -36,7 +36,7 @@ function getBaseUrl() { } /** - * Creates a new payment authorization + * Creates a new mock 'payment authorization' * * @param PayPal\Api\ApiContext apiContext * @return PayPal\Api\Authorization @@ -74,12 +74,12 @@ function createAuthorization($apiContext) { $transaction = new Transaction(); $transaction->setAmount($amount) - ->setDescription("This is the payment description."); + ->setDescription("Payment description."); $payment = new Payment(); // Setting intent to authorize creates a payment - // authorization. Setting it to sale makes an actual payment + // authorization. Setting it to sale creates actual payment $payment->setIntent("authorize") ->setPayer($payer) ->setTransactions(array($transaction)); diff --git a/sample/index.html b/sample/index.html index b90b55d..c191506 100644 --- a/sample/index.html +++ b/sample/index.html @@ -31,15 +31,19 @@ Payments - Payment with a credit card + Direct credit card payments Execute Source - Payment with a PayPal Account + PayPal account payments Execute Source + + Stored credit card payments + Execute + Source Get payment details Execute diff --git a/sample/payments/AuthorizationCapture.php b/sample/payments/AuthorizationCapture.php index df5ed7a..0bfce9e 100644 --- a/sample/payments/AuthorizationCapture.php +++ b/sample/payments/AuthorizationCapture.php @@ -1,6 +1,7 @@ capture method // with a valid ApiContext (See bootstrap.php for more on `ApiContext`) try { - // create payment to get authorization Id + // Create a new authorization to get authorization Id // createAuthorization defined in common.php $authId = createAuthorization($apiContext); @@ -27,9 +28,10 @@ try { $capture->setId($authId) ->setAmount($amt); - // Get the authorization + // Lookup the authorization. $authorization = Authorization::get($authId, $apiContext); + // Perform a capture $getCapture = $authorization->capture($capture, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; @@ -38,14 +40,15 @@ try { } ?> + + Capturing an authorization +
Captured payment getParentPayment(); ?>. Capture Id: getId();?>
-
-		toArray());?>
-	
+
toArray());?>
Back diff --git a/sample/payments/CreatePayment.php b/sample/payments/CreatePayment.php index 3e45a36..ccbfa76 100644 --- a/sample/payments/CreatePayment.php +++ b/sample/payments/CreatePayment.php @@ -1,8 +1,11 @@ setLine1("3909 Witmer Road") ->setLine2("Niagara Falls") @@ -41,23 +43,23 @@ $card->setType("visa") // ### 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` +// For direct credit card payments, set the CreditCard +// field on this object. $fi = new FundingInstrument(); $fi->setCreditCard($card); // ### Payer // A resource representing a Payer that funds a payment -// Use the List of `FundingInstrument` and the Payment Method -// as 'credit_card' +// For direct credit card payments, set payment method +// to 'credit_card' and add an array of funding instruments. $payer = new Payer(); $payer->setPaymentMethod("credit_card") ->setFundingInstruments(array($fi)); // ### Amount -// Let's you specify a payment amount. +// Lets you specify a payment amount. +// You can also specify additional details +// such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") ->setTotal("1.00"); @@ -65,15 +67,14 @@ $amount->setCurrency("USD") // ### 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 +// is fulfilling it. $transaction = new Transaction(); $transaction->setAmount($amount) - ->setDescription("This is the payment description."); + ->setDescription("Payment description"); // ### Payment // A Payment Resource; create one using -// the above types and intent as 'sale' +// the above types and intent set to sale 'sale' $payment = new Payment(); $payment->setIntent("sale") ->setPayer($payer) @@ -82,7 +83,7 @@ $payment->setIntent("sale") // ### 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; +// The return object contains the state. try { $payment->create($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { @@ -92,6 +93,9 @@ try { } ?> + + Direct Credit card payments +
Created payment: diff --git a/sample/payments/CreatePaymentUsingPayPal.php b/sample/payments/CreatePaymentUsingPayPal.php index d228111..4de5273 100644 --- a/sample/payments/CreatePaymentUsingPayPal.php +++ b/sample/payments/CreatePaymentUsingPayPal.php @@ -6,24 +6,24 @@ // 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' +// For paypal account payments, set payment method +// to 'paypal'. $payer = new Payer(); $payer->setPaymentMethod("paypal"); // ### Amount -// Let's you specify a payment amount. +// Lets you specify a payment amount. +// You can also specify additional details +// such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") ->setTotal("1.00"); @@ -31,11 +31,10 @@ $amount->setCurrency("USD") // ### 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 +// is fulfilling it. $transaction = new Transaction(); $transaction->setAmount($amount) - ->setDescription("This is the payment description."); + ->setDescription("Payment description"); // ### Redirect urls // Set the urls that the buyer must be redirected to after @@ -47,7 +46,7 @@ $redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true") // ### Payment // A Payment Resource; create one using -// the above types and intent as 'sale' +// the above types and intent set to 'sale' $payment = new Payment(); $payment->setIntent("sale") ->setPayer($payer) @@ -55,10 +54,10 @@ $payment->setIntent("sale") ->setTransactions(array($transaction)); // ### Create Payment -// Create a payment by posting to the APIService -// using a valid apiContext. +// Create a payment by calling the 'create' method +// passing it a valid apiContext. // (See bootstrap.php for more on `ApiContext`) -// The return object contains the status and the +// The return object contains the state and the // url to which the buyer must be redirected to // for payment approval try { @@ -69,16 +68,25 @@ try { exit(1); } -// ### Redirect buyer to paypal -// Retrieve buyer approval url from the `payment` object. +// ### Get redirect url +// The API response provides the url that you must redirect +// the buyer to. Retrieve the url from the $payment->getLinks() +// method foreach($payment->getLinks() as $link) { if($link->getRel() == 'approval_url') { $redirectUrl = $link->getHref(); + break; } } + +// ### Redirect buyer to PayPal website +// Save payment id so that you can 'complete' the payment +// once the buyer approves the payment and is redirected +// bacl to your website. +// // 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. +// in the session. In a real world app, you may want to +// store the payment id in a database. $_SESSION['paymentId'] = $payment->getId(); if(isset($redirectUrl)) { header("Location: $redirectUrl"); diff --git a/sample/payments/CreatePaymentUsingSavedCard.php b/sample/payments/CreatePaymentUsingSavedCard.php index e74cee3..ed40229 100644 --- a/sample/payments/CreatePaymentUsingSavedCard.php +++ b/sample/payments/CreatePaymentUsingSavedCard.php @@ -2,47 +2,42 @@ // # Create payment using a saved credit card // This sample code demonstrates how you can process a -// Payment using a previously saved credit card. +// Payment using a previously stored 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(); -$creditCardToken->setCreditCardId($creditCardId); +$creditCardToken->setCreditCardId('CARD-29H07236G1554552FKINPBHQ'); // ### 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` +// For stored credit card payments, set the CreditCardToken +// field on this object. $fi = new FundingInstrument(); $fi->setCreditCardToken($creditCardToken); // ### Payer // A resource representing a Payer that funds a payment -// Use the List of `FundingInstrument` and the Payment Method -// as 'credit_card' +// For stored credit card payments, set payment method +// to 'credit_card'. $payer = new Payer(); $payer->setPaymentMethod("credit_card") ->setFundingInstruments(array($fi)); // ### Amount -// Let's you specify a payment amount. +// Lets you specify a payment amount. +// You can also specify additional details +// such as shipping, tax. $amount = new Amount(); $amount->setCurrency("USD") ->setTotal("1.00"); @@ -50,24 +45,24 @@ $amount->setCurrency("USD") // ### 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 +// is fulfilling it. $transaction = new Transaction(); $transaction->setAmount($amount) - ->setDescription("This is the payment description."); + ->setDescription("Payment description"); // ### Payment // A Payment Resource; create one using -// the above types and intent as 'sale' +// the above types and intent set to 'sale' $payment = new Payment(); $payment->setIntent("sale") ->setPayer($payer) ->setTransactions(array($transaction)); // ###Create Payment -// Create a payment by posting to the APIService +// Create a payment by calling the 'create' method +// passing it a valid apiContext. // (See bootstrap.php for more on `ApiContext`) -// The return object contains the status; +// The return object contains the state. try { $payment->create($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { @@ -77,6 +72,9 @@ try { } ?> + + Saved Credit card payments +
Created payment: diff --git a/sample/payments/GetAuthorization.php b/sample/payments/GetAuthorization.php index db3400c..2caeb8b 100644 --- a/sample/payments/GetAuthorization.php +++ b/sample/payments/GetAuthorization.php @@ -1,6 +1,7 @@ require __DIR__ . '/../bootstrap.php'; @@ -28,6 +29,9 @@ try { } ?> + + Lookup an authorization +
Retrieved Authorization: diff --git a/sample/payments/GetCapture.php b/sample/payments/GetCapture.php index 5fbe148..288dda6 100644 --- a/sample/payments/GetCapture.php +++ b/sample/payments/GetCapture.php @@ -1,6 +1,7 @@ require __DIR__ . '/../bootstrap.php'; @@ -12,22 +13,25 @@ use PayPal\Api\Authorization; // ### Create a mock Capture try { - // create payment to get authorization Id + // create a mock authorization to get authorization Id + // createAuthorization is defined in common.php $authId = createAuthorization($apiContext); + + // Lookup the authorization + $authorization = Authorization::get($authId, $apiContext); + + ### Capture + $amt = new Amount(); $amt->setCurrency("USD") ->setTotal("1.00"); - - ### Capture - $captur = new Capture(); - $captur->setId($authId) + + // Create a capture + $captureInfo = new Capture(); + $captureInfo->setId($authId) ->setAmount($amt); - // get the authorization - $authorization = Authorization::get($authId, $apiContext); - - // Create a capture - $capt = $authorization->capture($captur, $apiContext); + $capture = $authorization->capture($captureInfo, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); @@ -38,7 +42,7 @@ try { // You can look up a capture by invoking the Capture::get method // with a valid ApiContext (See bootstrap.php for more on `ApiContext`) try { - $capture = Capture::get($capt->getId(), $apiContext); + $capture = Capture::get($capture->getId(), $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); @@ -46,6 +50,9 @@ try { } ?> + + Lookup a capture +
Capture Id: diff --git a/sample/payments/GetPayment.php b/sample/payments/GetPayment.php index 2863af9..91d90c0 100644 --- a/sample/payments/GetPayment.php +++ b/sample/payments/GetPayment.php @@ -28,6 +28,9 @@ try { } ?> + + Lookup a payment +
Retrieving Payment ID:
toArray());?>
diff --git a/sample/payments/ListPayments.php b/sample/payments/ListPayments.php index ce60abd..6b19b3e 100644 --- a/sample/payments/ListPayments.php +++ b/sample/payments/ListPayments.php @@ -29,6 +29,9 @@ try { } ?> + + Lookup payment history +
Got getCount(); ?> matching payments
toArray());?>
diff --git a/sample/payments/Reauthorization.php b/sample/payments/Reauthorization.php index e4c42b2..7fc500c 100644 --- a/sample/payments/Reauthorization.php +++ b/sample/payments/Reauthorization.php @@ -1,6 +1,7 @@ + + Reauthorize a payment +
Reauthorization Id: diff --git a/sample/payments/RefundCapture.php b/sample/payments/RefundCapture.php index 7a9bfd3..7c234a3 100644 --- a/sample/payments/RefundCapture.php +++ b/sample/payments/RefundCapture.php @@ -1,9 +1,9 @@ }/refund + require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Authorization; @@ -13,24 +13,25 @@ use PayPal\Api\Amount; use PayPal\Rest\ApiContext; -// ### Create a mock capture try { - // create payment to get authorization Id + // Create a mock authorization to get authorization Id $authId = createAuthorization($apiContext); - $amt = new Amount(); - $amt->setCurrency("USD") - ->setTotal("1.00"); - - ### Capture - $captur = new Capture(); - $captur->setAmount($amt); - // Get the authorization $authorization = Authorization::get($authId, $apiContext); + + // ### Capture + + $amt = new Amount(); + $amt->setCurrency("USD") + ->setTotal("1.00"); + // Create a capture - $capt = $authorization->capture($captur, $apiContext); + $captureInfo = new Capture(); + $captureInfo->setAmount($amt); + + $capture = $authorization->capture($captureInfo, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); @@ -48,7 +49,8 @@ try { // Create a new apiContext object so we send a new // PayPal-Request-Id (idempotency) header for this resource $apiContext = new ApiContext($apiContext->getCredential()); - $captureRefund = $capt->refund($refund, $apiContext); + + $captureRefund = $capture->refund($refund, $apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; var_dump($ex->getData()); @@ -57,6 +59,9 @@ try { ?> + + Refund a captured payment +
Refund Capture:
diff --git a/sample/payments/VoidAuthorization.php b/sample/payments/VoidAuthorization.php index bc9dc7d..e1c3f74 100644 --- a/sample/payments/VoidAuthorization.php +++ b/sample/payments/VoidAuthorization.php @@ -1,6 +1,7 @@ /void" require __DIR__ . '/../bootstrap.php'; @@ -13,11 +14,14 @@ use PayPal\Api\Authorization; // by invoking the $authorization->void method // with a valid ApiContext (See bootstrap.php for more on `ApiContext`) try { - // create payment to get authorization Id + // create an authorization to get authorization Id // createAuthorization is defined in common.php $authId = createAuthorization($apiContext); - + + // Lookup the authorization $authorization = Authorization::get($authId, $apiContext); + + // Void the authorization $voidedAuth = $authorization->void($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; @@ -26,6 +30,9 @@ try { } ?> + + Void an authorization +
Voided authorization diff --git a/sample/sale/GetSale.php b/sample/sale/GetSale.php index 14da4bb..ad64f70 100644 --- a/sample/sale/GetSale.php +++ b/sample/sale/GetSale.php @@ -1,6 +1,7 @@ + + Lookup a sale +
Retrieving sale id:
diff --git a/sample/sale/RefundSale.php b/sample/sale/RefundSale.php index fa34854..01d7292 100644 --- a/sample/sale/RefundSale.php +++ b/sample/sale/RefundSale.php @@ -11,13 +11,15 @@ use PayPal\Api\Amount; use PayPal\Api\Refund; use PayPal\Api\Sale; -// ### Refund -// Create a refund object indicating -// refund amount +// ### Refund amount +// Includes both the refunded amount (to Payer) +// and refunded fee (to Payee). Use the $amt->details +// field to mention fees refund details. $amt = new Amount(); $amt->setCurrency('USD') ->setTotal('0.01'); +// ### Refund object $refund = new Refund(); $refund->setAmount($amt); @@ -29,7 +31,6 @@ $saleId = '3RM92092UW5126232'; // given sale transaction id. $sale = new Sale(); $sale->setId($saleId); - try { // Refund the sale // (See bootstrap.php for more on `ApiContext`) @@ -41,6 +42,9 @@ try { } ?> + + Refund a sale +
Refunding sale id:
diff --git a/sample/vault/CreateCreditCard.php b/sample/vault/CreateCreditCard.php index 9cc6bce..4953550 100644 --- a/sample/vault/CreateCreditCard.php +++ b/sample/vault/CreateCreditCard.php @@ -1,23 +1,18 @@ setType("visa") ->setNumber("4417119669820331") @@ -42,6 +37,9 @@ try { } ?> + + Save a credit card +
Saved a new credit card with id: getId();?>
diff --git a/sample/vault/DeleteCreditCard.php b/sample/vault/DeleteCreditCard.php index 32020bd..0de5bba 100644 --- a/sample/vault/DeleteCreditCard.php +++ b/sample/vault/DeleteCreditCard.php @@ -1,15 +1,14 @@ } // NOTE: HTTP method used here is DELETE + require __DIR__ . '/../bootstrap.php'; use PayPal\Api\CreditCard; -use PayPal\Api\Address; -// save card for demo +// Store a mock card that can be deleted later. // ### CreditCard // A resource representing a credit card that can be // used to fund a payment. @@ -25,8 +24,7 @@ $card->setType("visa") // ### 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. +// an 'id' that you can use to refer to it later. // (See bootstrap.php for more on `ApiContext`) try { $card = $card->create($apiContext); @@ -36,19 +34,22 @@ try { exit(1); } -$creditCard = CreditCard::get($card->getId(), $apiContext); try { // ### Delete Card // deletes saved credit card // (See bootstrap.php for more on `ApiContext`) + + $creditCard = CreditCard::get($card->getId(), $apiContext); $creditCard->delete($apiContext); } catch (PayPal\Exception\PPConnectionException $ex) { echo "Exception: " . $ex->getMessage() . PHP_EOL; exit(1); } ?> - + + Delete a saved credit card +

Credit Card deleted Successfully

Back diff --git a/sample/vault/GetCreditCard.php b/sample/vault/GetCreditCard.php index 6489485..c33e333 100644 --- a/sample/vault/GetCreditCard.php +++ b/sample/vault/GetCreditCard.php @@ -2,9 +2,8 @@ // # Get Credit Card Sample // The CreditCard resource allows you to -// retrieve previously saved CreditCards, -// by sending a GET request to the URI -// '/v1/vault/credit-card' +// retrieve previously saved CreditCards. +// API called: '/v1/vault/credit-card' // The following code takes you through // the process of retrieving a saved CreditCard require __DIR__ . '/../bootstrap.php'; @@ -25,8 +24,11 @@ try { } ?> + + Lookup a saved credit card + -
Retrieving credit card:
+
Retrieving saved credit card:
Back