3
votes

I used paypal php sdk with this :

https://github.com/paypal/merchant-sdk-php/blob/master/samples/RecurringPayments/CreateRecurringPaymentsProfile.php

Express checkout is works well, but use recurring payments have problem : token is invalid. Line 152 in the sdk, it's said

A timestamped token, the value of which was returned in the response to the first call to SetExpressCheckout. Call CreateRecurringPaymentsProfile once for each billing agreement included in SetExpressCheckout request and use the same token for each call. Each CreateRecurringPaymentsProfile request creates a single recurring payments profile.

But i don't understand how to "Call CreateRecurringPaymentsProfile once in SetExpressCheckout", there is my code:

public function createPayToken($returnUrl, $cancelUrl, $payModeData) {
    $itemName = $payModeData['name'];
    $order    = $payModeData['fee'];

    // $category = 'Digital';
    $category = 'Physical';

    $currencyCode = "USD";

    $paymentDetails = new PaymentDetailsType();

    $itemAmount = new BasicAmountType($currencyCode, $order);

    $itemDetails = new PaymentDetailsItemType();
    $itemDetails->Name = $itemName;
    $itemDetails->Amount = $itemAmount;
    $itemDetails->Quantity = 1;
    $itemDetails->ItemCategory = $category;

    $paymentDetails->OrderTotal = new BasicAmountType($currencyCode, $order);
    $paymentDetails->PaymentAction = 'Sale';
    $paymentDetails->PaymentDetailsItem[] = $itemDetails;

    $setECReqDetails = new SetExpressCheckoutRequestDetailsType();
    $setECReqDetails->PaymentDetails[] = $paymentDetails;
    $setECReqDetails->ReqConfirmShipping = 0;
    $setECReqDetails->NoShipping = 1;
    $setECReqDetails->AddressOverride = 0;
    $setECReqDetails->CancelURL = $cancelUrl;
    $setECReqDetails->ReturnURL = $returnUrl;

    $setECReqType = new SetExpressCheckoutRequestType();
    $setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails;

    $setECReq = new SetExpressCheckoutReq();
    $setECReq->SetExpressCheckoutRequest = $setECReqType;

    $paypalService = new PayPalAPIInterfaceServiceService();


    try {
        $setECResponse = $paypalService->SetExpressCheckout($setECReq);
        exit;
    } catch (Exception $ex) {
        echo $ex;
        exit;
    }



    if(isset($setECResponse)) {
        if($setECResponse->Ack =='Success') {
            $token = $setECResponse->Token;
            return $token;
        }
        var_dump($setECResponse);
        exit;
    }

    return false;
}

Thank you.

1

1 Answers

4
votes

You just need to make sure you're including the billing agreement information in your SetExpressCheckout request. Take a look at this sample of set of API calls to complete a recurring payments profile using Express Checkout.

You'll notice the SEC request includes parameters for L_BILLINGTYPE0 AND L_BILLINGAGREEMENTDESCRIPTION0. You need to make sure you include those, otherwise the token that you get back won't be valid for CreateRecurringPaymentsProfile.