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

Passing Data

Sergei Matros edited this page Jan 27, 2018 · 2 revisions

In PayPal SDK, each object representation of JSON in Rest API, is represented as a PayPalModel Object. All these classes lives inside \PayPal\Api namespace. To send/receive any data from PayPal, you need to create an instance of respective PayPalModel, and use functions provided by the object, to make function calls like create, delete, update, execute, list, etc.

As we saw in Making-First-Call, we created an instance of Payment and use set* methods to insert data into $payment object. The other ways to inject similar data is as follows:

JSON String

You could directly pass the JSON string as PayPalModel constructor parameter, and PayPal SDK will fill the information accordingly. You could use get* methods to retrieve specific information about the object immediately after that. Here is the example.

$payment = new \PayPal\Api\Payment(
  '{
	"intent": "sale",
	"redirect_urls": {
		"return_url": "https://example.com/your_redirect_url.html",
		"cancel_url": "https://example.com/your_cancel_url.html"
	},
	"payer": {
		"payment_method": "paypal"
	},
	"transactions": [{
		"amount": {
			"total": "1.00",
			"currency": "USD"
		}
	}]
  }'
);
// This will print the intent
echo $payment->getIntent();

Array Object

Similarly, array could be passed too as a constructor parameter to PayPalModel Object, as shown below:

$payment = new \PayPal\Api\Payment(
    array(
        "intent" => "sale",
        "redirect_urls" => array(
            "return_url" => "https://example.com/your_redirect_url.html",
            "cancel_url" => "https://example.com/your_cancel_url.html"
        ),
        "payer" => array(
            "payment_method" => "paypal"
        ),
        "transactions" => array(
            array(
                "amount" => array(
                    "total" => "1.00",
                    "currency" => "USD"
                )
            )
        )
    )
);
// This will print the intent
echo $payment->getIntent();

This ability to accept multiple data formats allow developers to create instance of PayPalModels with minimal interference.

Next Step