1
votes

I am trying to wrap my head around the flow of Paypals subscriptions.

The flow we have is:

1.) Create Billing Plan
2.) Activate Billing Plan
3.) Create Billing Agreement (using the activated plan, send customer to paypal url)
4.) Customer approves, returns back to our URL, Execute with the Token provided

The problem I am running into is I can't seem to reference any id's returned in Step 4 to anything provided in the first 3 steps.

I must be missing something, but I am completely stumped. I can't find any way to relate the executed agreement anything in the previous steps.

3

3 Answers

2
votes

As mentioned by PayPal_Orcun, the issue is still pending, and is getting resolved internally in paypal, however, in the meantime, there is something you could do here.

  1. On step 3, once you create an agreement, you get an approval link that looks like this:

    https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8DL20958WD398123E

  2. Here, copy the token, and store it in the database. Think of it as a unique Id.

  3. Execute the payment. When it comes back, paypal will redirect back with a url like this:

    http://localhost/paypal/PayPal-PHP-SDK/sample/billing/ExecuteAgreement.php?success=true&token=EC-8DL20958WD398123E

As you can see, you could do a $_GET['token'] to retrieve the token here, and match with the one created. You can then use ID returned back on execute success, and replace the token field with new ID field.

I agree, this is not something you would expect, but it would definitely allow you to connect the agreement after create and execute calls.

Hope this helped.

0
votes

Here's how REST API billing plans and billing agreements work (including curl call samples):

First of all, please make sure that your REST app is toggled for subscriptions (You can check your REST app settings on https://developer.paypal.com/webapps/developer/applications/myapps )

1-) Create Billing Plan

curl -k -v -H "Content-Type:application/json" -H "Authorization: Bearer ACCESS_TOKEN" -d '{"name":"Test REST Club Plan","description":"Template creation.","type":"fixed","payment_definitions":[{"name":"Regular Payments","type":"REGULAR","frequency":"MONTH","frequency_interval":"2","amount":{"value":"100","currency":"USD"},"cycles":"12","charge_models":[{"type":"SHIPPING","amount":{"value":"10","currency":"USD"}},{"type":"TAX","amount":{"value":"12","currency":"USD"}}]}],"merchant_preferences":{"setup_fee":{"value":"1","currency":"USD"},"return_url":"http://returnurl","cancel_url":"http://cancelurl","auto_bill_amount":"YES","initial_fail_amount_action":"CONTINUE","max_fail_attempts":"0"}}' https://api.sandbox.paypal.com/v1/payments/billing-plans

PayPal will return a billing plan ID starting with "P-"

2-) Activate Billing Plan

curl -k -v -H "Content-Type:application/json" -H "Authorization: Bearer ACCESS_TOKEN" -d '[{"path":"/","value":{"state":"ACTIVE"},"op":"replace"}]' https://api.sandbox.paypal.com/v1/payments/billing-plans/

3-) Create Billing Agreement

curl -k -v -H "Content-Type:application/json" -H "Authorization: Bearer ACCESS_TOKEN" -d '{"name":"Test REST Club Plan","description":"Template creation.","start_date":"2015-03-20T07:49:27-07:00","plan":{"id":"PLAN_ID_HERE"},"payer":{"payment_method":"paypal"},"shipping_address":{"line1":"111 First Street","city":"Saratoga","state":"CA","postal_code":"95070","country_code":"US"}}' https://api.sandbox.paypal.com/v1/payments/billing-agreements

PayPal will return an Express Checkout re-direct URL with a EC token. E.g. https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-xxxxxxxxxxxxxxxxx

You need to re-direct your customer to PayPal via that URL. Once the billing agreement is authorized, customer is returned to your site (return_url in step 1).

4-) Execute Billing Agreement

curl -k -v -H "Content-Type:application/json" -H "Authorization: Bearer ACCESS_TOKEN" -d '{}' https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-TOKEN_HERE/agreement-execute

0
votes

The solution I used for this same problem was to set override_merchant_preferences on the billing agreement, with the return_url containing the user id in the query, like www.domain.com/api/handler.php?uid=42&action=return.