The JSON must be structured as the following:
JSON to send in the request example
- {
- "subscriptionId": "id returned when subscription was created",
- "merchantId": "your merchantid",
- "secret": "your secret",
- "user": "Some user name",
- "reason": "Some reason to cancel subscription"
- }
Parameters description
- subscriptionId: Identifier received in the subscription creation response.
- merchantId: merchant provided by Greenpay.
- secret: secret provided by Greenpay.
- user: user who's cancelling the subscription.
- reason: cancelation reason.
Recommendations
- “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
- {
"code": 200,
- "status": "SUCCESS",
- "result": {
- "id": "3",
- "merchant_id": "143b28c9-32ad-4635-8ed8-d6abfb6863a0",
- "status": "CANCELLED",
- "user_id": "Aaron",
- "user_type": 1,
- "card_tokens": [
- "57e3ea41-bdd4-4b51-b04b-cc3eeeb35ee2"
- ],
- "purchase_order": {
- "secret": "QUY5MzBEMkRDMzVCMDFCRDc2NzEwRjZCQjE3NjhFNkE0NjhEQ0MxRjkxQzkzMjAwNzVDNUVGNkY5RTc0N0M0NzkzNUEyQUZFQjczMEYyODEyRjJEMDc5Q0ExNTk1NTA0NjdCMkNBODg1N0Q4MzY2MjI0NEREMUVGQjk4NTcwMzg=",
- "currency": "CRC",
- "optional": {
- "key": "value",
- "key1": "value1"
- },
- "terminal": "deKokoTest-eb29-BNCR-CRC",
- "description": "subscription description",
- "subscription": [{
- "amount": 10,
- "cadence": {
- "day": 15,
- "mode": "EVERY",
- "unit": "MONTH",
- "every": 1
- },
- "endDate": 1544853600000,
- "startDate": 1536991200000
- }],
- "initialPayment": {
- "amount": 300,
- "description": "some set up description"
- }
- },
- "next_payment": null,
- "enabled": true,
- "inserted_at": "2018-12-21T20:55:06.299Z",
- "updated_at": "2018-12-26T16:57:20.539Z",
- "general_info": {
- "user": "UserBot",
- "reason": "I will no longer use the service"
- }
- },
- "errors": []
- }
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
- var url = "https://sandbox-merchant.greenpay.me/subscriptions/cancel"
-
- var data = {
- "subscriptionId": "b69cd5773eac06bf25a702bac02e8079",
- "secret": "QUY5MzBEMkRDMzVCMDFCRDc2NzEwRjZCQjE3NjhFNkE0NjhEQ0MxRjkxQzkzMjAwNzVDNUVGNkY5RTc0N0M0NzkzNUEyQUZFQjczMEYyODEyRjJEMDc5Q0ExNTk1NTA0NjdCMkNBODg1N0Q4MzY2MjI0NEREMUVGQjk4NTcwMzg=",
- "merchantId": "143b28c9-32ad-4635-8ed8-d6abfb6863a0",
- "user": "UserBot",
- "reason": "I will no longer use the service"
- }
-
- var unirest = require("unirest");
-
- var req = unirest("POST", url);
-
- req.headers({
- "cache-control": "no-cache",
- "Content-Type": "application/json"
- });
-
- req.type("json");
- req.send(data);
-
- req.end(function (res) {
- if (res.error) throw new Error(res.error);
- console.log(res.body);
- });
3. Possible errors to receive
- Invalid credentials
If you try to cancel an inactive or non-existent subscription, the response will de the following.
Response example - invalid credentials.
- {
- "code": 500,
- "status": "FAIL",
- "result": [],
- "errors": [
- "Invalid credentials"
- ]
- }