Search code examples
phppaypalpaypal-sandboxpaypal-rest-sdkpaypal-subscriptions

How to link PayPal webhook and user?


I think I'm over looking something incredibly simple here.

When I create a new billing agreement through the PayPal PHP SDK I'm returned to my store by the following URL..

/paypal-success?success=true&token=EC-3K353628LH3197445  

I have created a WebHook for the Subscription Created event that posts JSON as follows

{
    "id": "WH-5UR49681MC703253M-3S161814MB664705V",
    "event_version": "1.0",
    "create_time": "2018-03-17T17:21:47.120Z",
    "resource_type": "Agreement",
    "event_type": "BILLING.SUBSCRIPTION.CREATED",
    "summary": "A billing subscription was created",
    "resource": {
        "agreement_details": {
            "outstanding_balance": {
                "value": "0.00"
            },
            "num_cycles_remaining": "0",
            "num_cycles_completed": "0",
            "next_billing_date": "2018-03-17T10:00:00Z",
            "last_payment_date": "2018-03-17T17:21:44Z",
            "last_payment_amount": {
                "value": "1.00"
            },
            "final_payment_due_date": "1970-01-01T00:00:00Z",
            "failed_payment_count": "0"
        },
        "description": "x",
        "links": [{
            "href": "api.sandbox.paypal.com/v1/payments/billing-agreements/I-55KA5N9GPPJW",
            "rel": "self",
            "method": "GET"
        }],
        "shipping_address": {
            "recipient_name": "test buyer",
            "line1": "Spitalfields Arts Market, 112 Brick Lane,",
            "city": "London",
            "state": "London",
            "postal_code": "E1 6RL",
            "country_code": "GB"
        },
        "id": "I-55KA5N9GPPJW",
        "state": "Active",
        "payer": {
            "payment_method": "paypal",
            "status": "verified",
            "payer_info": {
                "email": "jamie-buyer@x.com",
                "first_name": "test",
                "last_name": "buyer",
                "payer_id": "Q3TZFN4F7NHJ6",
                "shipping_address": {
                    "recipient_name": "test buyer",
                    "line1": "Spitalfields Arts Market, 112 Brick Lane,",
                    "city": "London",
                    "state": "London",
                    "postal_code": "E1 6RL",
                    "country_code": "GB"
                }
            }
        },
        "plan": {
            "curr_code": "USD",
            "links": [],
            "payment_definitions": [{
                "type": "REGULAR",
                "frequency": "Month",
                "frequency_interval": "1",
                "amount": {
                    "value": "49.99"
                },
                "cycles": "0",
                "charge_models": [{
                    "type": "TAX",
                    "amount": {
                        "value": "0.00"
                    }
                },
                {
                    "type": "SHIPPING",
                    "amount": {
                        "value": "0.00"
                    }
                }]
            }],
            "merchant_preferences": {
                "setup_fee": {
                    "value": "1.00"
                },
                "auto_bill_amount": "YES",
                "max_fail_attempts": "0"
            }
        },
        "start_date": "2018-03-17T00:00:00Z"
    },
    "links": [{
        "href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V",
        "rel": "self",
        "method": "GET"
    },
    {
        "href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V/resend",
        "rel": "resend",
        "method": "POST"
    }]
}

That token generated when the user is redirected back to my store does not appear to be in the WebHooks JSON.

How do you link your PayPal users once they are redirected back to your store to the incoming WebHooks?


Solution

  • The token returned in your success url is used to activate the agreement after the user accepts the agreement on PayPal. Once you activate the agreement you can retrieve the agreement id. That id is in your web hook under resources/id. "id": "I-55KA5N9GPPJW". There may be a better way to deal with this but I created a subscription table that associates the agreement id to the user id and a transactions table that has the agreement id and the associated webhook information.