PayPal\Api\FuturePayment::create PHP Method

create() public method

Extends the Payment object to create future payments
public create ( null $apiContext = null, string | null $clientMetadataId = null )
$apiContext null
$clientMetadataId string | null
    public function create($apiContext = null, $clientMetadataId = null)
    {
        if ($apiContext == null) {
            $apiContext = new ApiContext(self::$credential);
        }
        $headers = array();
        if ($clientMetadataId != null) {
            $headers = array('PAYPAL-CLIENT-METADATA-ID' => $clientMetadataId);
        }
        $payLoad = $this->toJSON();
        $call = new PayPalRestCall($apiContext);
        $json = $call->execute(array('PayPal\\Handler\\RestHandler'), "/v1/payments/payment", "POST", $payLoad, $headers);
        $this->fromJson($json);
        return $this;
    }

Usage Example

// You need to get a permanent refresh token from the authorization code, retrieved from the mobile sdk.
// authorization code from mobile sdk
$authorizationCode = 'EJfRuAqXEE95pdVMmOym_mftTbeJD03RBX-Zjg9pLCAhdLqLeRR6YSKTNsrbQGX7lFoZ3SxwFyxADEZbBOxpn023W9SA0JzSQAy-9eLdON5eDPAyMyKlHyNVS2DqBR2iWVfQGfudbd9MDoRxMEjIZbY';
// Client Metadata id from mobile sdk
// For more information look for PayPal-Client-Metadata-Id in https://developer.paypal.com/docs/api/#authentication--headers
$clientMetadataId = '123123456';
try {
    // Exchange authorization_code for long living refresh token. You should store
    // it in a database for later use
    $refreshToken = FuturePayment::getRefreshToken($authorizationCode, $apiContext);
    // Update the access token in apiContext
    $payment->updateAccessToken($refreshToken, $apiContext);
    // For Sample Purposes Only.
    $request = clone $payment;
    // ### Create Future Payment
    // 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 state and the
    // url to which the buyer must be redirected to
    // for payment approval
    // Please note that currently future payments works only with PayPal as a funding instrument.
    $payment->create($apiContext, $clientMetadataId);
} catch (Exception $ex) {
    // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
    ResultPrinter::printError("Future Payment", "Payment", null, $request, $ex);
    exit(1);
}
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printResult("Future Payment", "Payment", $payment->getId(), $request, $payment);
return $payment;