0

I'm building paypal subscription system and i'm using web-hooks for notifying system on subscription created, active, etc.

However, it's required that user must return to success url and site execute $agreement->execute($token, $apiContext)); to make it work.

Let's say for some reason user never get back to return url then you will never execute payment and user will never get their subscription.

I've looked around on paypal documentation and couldn't find any solution.

Here's my code:

Subscribe.php:

$agreement = new Agreement();
$agreement->setName('Basic Plan')
->setDescription('Some info')
->setStartDate($date);

$plan = new Plan();
$plan->setId('PLAN_ID');

$agreement->setPlan($plan);

// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);

// Add Shipping Address
$shippingAddress = new ShippingAddress();
$shippingAddress->setLine1('111 First Street')
    ->setCity('Saratoga')
    ->setState('CA')
    ->setPostalCode('95070')
    ->setCountryCode('US');
$agreement->setShippingAddress($shippingAddress);

// ### Create Agreement
try {
    $agreement = $agreement->create($apiContext);
    $agreement->getApprovalLink()
    // method
    $approvalUrl = $agreement->getApprovalLink();
    redirect($approvalUrl);
} catch (Exception $ex) {
    print_r($ex->getData());
}

index.php

if (isset($_GET['status']) && $_GET['status'] == 'success') {
    $token = $_GET['token'];
    $agreement = new \PayPal\Api\Agreement();
    try {
        // ## Execute Agreement
        // Execute the agreement by passing in the token
        echo "<pre>";
        print_r($agreement->execute($token, $apiContext));
    } catch (Exception $ex) {

        exit(1);
    }
} else {
    echo "User Cancelled the Approval";
}
Markian
  • 39
  • 5
  • Check the documentation for events you would like to hook in to. Possible events may be [Sales](https://developer.paypal.com/docs/integration/direct/webhooks/event-names/#sales) or [Payment Orders](https://developer.paypal.com/docs/integration/direct/webhooks/event-names/#payment-orders) – Jamie_D Nov 09 '19 at 10:33
  • I already have the required events set up. The problem is they only trigger if the user hits the return url. If for some reasons they don't return to that url, no webhook would trigger even if the transaction completes. – Markian Nov 09 '19 at 13:51
  • All your processing code seems to be executed after a success status. You need hook into a pre-processing event which would require an order flow in your code. [See](https://developer.paypal.com/docs/api/orders/v2/) – Jamie_D Nov 09 '19 at 14:22
  • Is it avail for subscription api? Can you give some example code? – Markian Nov 09 '19 at 16:52

0 Answers0