Cancel subscription

Cancel subscription

For this service the following endpoint must be consumed: 

  1. https://sandbox-merchant.greenpay.me/subscriptions/cancel

Below is the step by step to successfully unsubscribe in our API:


1. Create JSON object to be sent 

The JSON must be structured as the following: 

JSON to send in the request example
  1. {
  2.    "subscriptionId": "id returned when subscription was created",
  3.    "merchantId": "your merchantid",
  4.    "secret": "your secret",
  5.    "user": "Some user name",
  6.    "reason": "Some reason to cancel subscription"
  7. }
Parameters description
  1. subscriptionId: Identifier received in the subscription creation response.
  2. merchantId: merchant provided by Greenpay.
  3. secret: secret provided by Greenpay.
  4. user: user who's cancelling the subscription.
  5. reason: cancelation reason.

Recommendations
  1. “User” and “reason” parameters are to follow up the user who canceled the service.

2. Send cancellation request.

An HTTP POST request must be sent to the endpoint. This request must include the JSON object that contains the subscription data.

Once the subscription has been sent and has been successfully completed, a JSON object is obtained in response.

Upon receiving this response, the parameter that must be obtained from the JSON object is the “body”, this one contains the cancellation result. The following code shows the response received after sending a subscription request.

Successful cancellation response example

  1. {
        "code": 200,

  2.     "status": "SUCCESS",
  3.     "result": {
  4.         "id": "3",
  5.         "merchant_id": "143b28c9-32ad-4635-8ed8-d6abfb6863a0",
  6.         "status": "CANCELLED",
  7.         "user_id": "Aaron",
  8.         "user_type": 1,
  9.         "card_tokens": [
  10.             "57e3ea41-bdd4-4b51-b04b-cc3eeeb35ee2"
  11.         ],
  12.         "purchase_order": {
  13.             "secret": "QUY5MzBEMkRDMzVCMDFCRDc2NzEwRjZCQjE3NjhFNkE0NjhEQ0MxRjkxQzkzMjAwNzVDNUVGNkY5RTc0N0M0NzkzNUEyQUZFQjczMEYyODEyRjJEMDc5Q0ExNTk1NTA0NjdCMkNBODg1N0Q4MzY2MjI0NEREMUVGQjk4NTcwMzg=",
  14.             "currency": "CRC",
  15.             "optional": {
  16.                 "key": "value",
  17.                 "key1": "value1"
  18.             },
  19.             "terminal": "deKokoTest-eb29-BNCR-CRC",
  20.             "description": "subscription description",
  21.             "subscription": [{
  22.                 "amount": 10,
  23.                 "cadence": {
  24.                     "day": 15,
  25.                     "mode": "EVERY",
  26.                     "unit": "MONTH",
  27.                     "every": 1
  28.                 },
  29.                 "endDate": 1544853600000,
  30.                 "startDate": 1536991200000
  31.             }],
  32.             "initialPayment": {
  33.                 "amount": 300,
  34.                 "description": "some set up description"
  35.             }
  36.         },
  37.         "next_payment": null,
  38.         "enabled": true,
  39.         "inserted_at": "2018-12-21T20:55:06.299Z",
  40.         "updated_at": "2018-12-26T16:57:20.539Z",
  41.         "general_info": {
  42.             "user": "UserBot",
  43.             "reason": "I will no longer use the service"
  44.         }
  45.     },
  46.     "errors": []
  47. }
Below is a javascript example of the subscription endpoint request. This function also gets the JSON object “body” received as a response.

POST request code example

  1. var url = "https://sandbox-merchant.greenpay.me/subscriptions/cancel"
  2.  
  3. var data = {
  4.    "subscriptionId": "b69cd5773eac06bf25a702bac02e8079",
  5.    "secret": "QUY5MzBEMkRDMzVCMDFCRDc2NzEwRjZCQjE3NjhFNkE0NjhEQ0MxRjkxQzkzMjAwNzVDNUVGNkY5RTc0N0M0NzkzNUEyQUZFQjczMEYyODEyRjJEMDc5Q0ExNTk1NTA0NjdCMkNBODg1N0Q4MzY2MjI0NEREMUVGQjk4NTcwMzg=",
  6.    "merchantId": "143b28c9-32ad-4635-8ed8-d6abfb6863a0",
  7.    "user": "UserBot",
  8.    "reason": "I will no longer use the service"
  9. }
  10.  
  11. var unirest = require("unirest");
  12.  
  13. var req = unirest("POST", url);
  14.  
  15. req.headers({
  16.    "cache-control": "no-cache",
  17.    "Content-Type": "application/json"
  18. });
  19.  
  20. req.type("json");
  21. req.send(data);
  22.  
  23. req.end(function (res) {
  24.    if (res.error) throw new Error(res.error);
  25.    console.log(res.body);
  26. });
3. Possible errors to receive

  1. Invalid credentials

If you try to cancel an inactive or non-existent subscription, the response will de the following.



Response example -  invalid credentials.
  1. {
  2.    "code": 500,
  3.    "status": "FAIL",
  4.    "result": [],
  5.    "errors": [
  6.        "Invalid credentials"
  7.    ]
  8. }

    • Related Articles

    • Subscription service - General information

      This service allow merchants to subscribe their customers to automatic collections in a set time window. This functionality only allows subscription monthly payments. It will be deducted from the customer’s card every month, while the subscription is ...
    • Create subscription

      For this service, the following endpoint must be consumed:  https://sandbox-merchant.greenpay.me/subscriptions Step by step to successfully create a subscription in our API : 1. Create the JSON object to be sent It must contain the following ...
    • Void or cancel transaction

      Aspects to consider If you want to use this service, you would have consider following aspects:: Use the service on the endpoint:  Sandbox: https://sandbox-merchant.greenpay.me/transactions/void ...
    • Get responses on a webhook

      1. Description If the merchant requires receiving the payments responses or card tokenization response in a backend, either to update orders, information or only store the response in the database as a backup, then they have to enable a webhook. A ...
    • Update amount

      For this service, the following endpoint must be consumed:  https://sandbox-merchant.greenpay.me/subscriptions/update Step by step to update a subscritpion amount in our API 1. Create a JSON object  JSON structure example {   "subscriptionId": "id ...