# How to call Cards API source: https://developer.mastercard.com/donations/tutorial/donation-api-details/cardsapinew/index.md ### Store card-on-file to make donations easy {#store-card-on-file-to-make-donations-easy} The `/donations/donors/cards` endpoint allows you to add payment cards-on-file in the Mastercard Donate Service to allow donors to donate without having to type in the card details each time. It also allows you to retrieve, update, and delete payment cards details. ## POST /donors/cards {#post-donorscards} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Add a Card](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/add-a-card/index.md) * [Parameters to Add a Card](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/add-card-param/index.md) ## Test Cases {#test-cases} 1. To add a card on file for a registered donor, send a POST request. 2. To create a POST request, provide the `donorId` as the endpoint path parameter and the card details in the request body. ```py POST https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards ``` ```JSON { "cardholderName" : "Robert Doe", "cardAlias" : "Card Alias Text", "primaryAccountNumber" : "71930787711008431", "primaryAccountNumberSource" : "CARD_ON_FILE", "expiryInfo" : { "month" : "01", "year" : "2023" }, "billingAddress" : { "line1" : "2200", "line2" : "Mastercard", "city" : "O' Fallon", "postalCode" : "63368", "state" : "MO", "countryCode" : "IN" } } ``` 3. A successful request returns the `HTTP 200 Created` status code and a JSON response body that includes the unique `card_id` and the **last four digits** of the Primary Account Number (PAN). 4. Save the card identifier. ```JSON { "cardId" : "777a3120-f73a-4c5e-9e63-490d1fc5fc1a", "primaryAccountNumberSuffix" : "8431", } ``` If a client is associated with more than one program, it is mandatory to pass a `programID` in the endpoint header to be able to add a card associated with that specific program ID: 1. To add a card on file for a registered donor, send a POST request. ```py POST https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards ``` 2. Provide the program ID in the request header: **Program ID**: 82e73e5c-f618-409a-8ca2-a212a0f649e3 3. Provide the card details in the request body. ```JSON { "cardholderName" : "Robert Doe", "cardAlias" : "Card Alias Text", "primaryAccountNumber" : "71930787711008426", "primaryAccountNumberSource" : "CARD_ON_FILE", "expiryInfo" : { "month" : "01", "year" : "2023" }, "billingAddress" : { "line1" : "2200", "line2" : "Mastercard", "city" : "O' Fallon", "postalCode" : "63368", "state" : "MO", "countryCode" : "IN" } } ``` 4. A successful request returns the `HTTP 200 Created` status code and a JSON response body that includes the unique `card_id` and the **last four digits** of the Primary Account Number (PAN). 5. Save the card identifier. ```JSON { "cardId" : "777a3120-f73a-4c5e-9e63-490d1fc5fe2a", "primaryAccountNumberSuffix" : "8426", } ``` This section discusses some of the failed scenarios and the error messages expected. If a client is mapped to many programs, the Add Card endpoint will throw an error if a `program ID` is not provided. 1. Send a POST request with only the `donorId` in the path parameter. ```py POST https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards ``` 2. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "PROGRAM_ID_NOT_FOUND", "Description": "Program id is required as this client is mapped to multiple programs.", "Recoverable": false, "Details": null } ] } } ``` If you try adding a card on file with the same PAN, the endpoint throws an error. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "PAN_ALREADY_EXISTS_FOR_USER", "Description": "The provided card already exists for this user.", "Recoverable": false, "Details": [ "CardId: 327ebf98-f3cc-4733-a041-c60e7d743b04" ] } ] } } ``` ## POST /donors/{donor_id}/bulk-cards {#post-donorsdonor_idbulk-cards} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Add Multiple Cards](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/add-bulk-cards/index.md) * [Add Multiple Cards Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/add-bulk-cards-param/index.md) ## Test Cases {#test-cases} 1. To add a multiple cards-on-file for a registered donor, send a POST request. 2. To create a POST request, provide the `donorId` as the endpoint path parameter and all the card details in the request body. ```py POST https://sandbox.api.mastercard.com/donations/donors/9d2101aa-b316-4b41-bb4f-fc71b8f151f9/bulk-cards ``` ```json { "cardDetails": [ { "billingAddress": { "city": "Pune", "countryCode": "IND", "line1": "2/2", "line2": "Range Hills", "line3": "Snake Hill Ave", "postalCode": 411012, "state": "MH" }, "cardAlias": "CardA", "cardholderName": "John Doe", "expiryInfo": { "month": 10, "year": 2025 }, "primaryAccountNumber": 5450985888403538, "primaryAccountNumberSource": "CARD_ON_FILE" }, { "billingAddress": { "city": "Pune", "countryCode": "IND", "line1": "2/2", "line2": "Range Hills", "line3": "Snake Hill Ave", "postalCode": 411012, "state": "MH" }, "cardAlias": "CardB", "cardholderName": "John Doe", "expiryInfo": { "month": 10, "year": 2025 }, "primaryAccountNumber": 4508750015741019, "primaryAccountNumberSource": "CARD_ON_FILE" } ] } ``` 3. A successful request returns the `HTTP 200 Created` status code and a JSON response body that includes two lists: > * `addedCards`: This list contains the cards that were successfully added along with their masked card numbers and system-generated unique card ID for each card. > * `failedCards`: This list contains the cards that could not be added, along with their masked card numbers and reason codes explaining the failure. 4. Save the card identifiers. ```json { "addedCards": [ { "maskedCard": "545098******3538","cardId": "dab4beae-7fba-4acc-8f08-76e4d70cfe8a" } ], "failedCards": [ { "maskedCard": "450875******1019","reasonCode": "PAN_ALREADY_EXISTS_FOR_USER" } ] } ``` 1. Create a POST request having the same PAN for two records in the request. 2. To create a POST request, provide the `donorId` as the endpoint path parameter and all the card details in the request body. ```py POST https://sandbox.api.mastercard.com/donations/donors/9d2101aa-b316-4b41-bb4f-fc71b8f151f9/bulk-cards ``` ```json { "cardDetails": [ { "billingAddress": { "city": "Pune", "countryCode": "IND", "line1": "2/2", "line2": "Range Hills", "line3": "Snake Hill Ave", "postalCode": 411012, "state": "MH" }, "cardAlias": "CardA", "cardholderName": "John Doe", "expiryInfo": { "month": 10, "year": 2025 }, "primaryAccountNumber": 2223003122003222, "primaryAccountNumberSource": "CARD_ON_FILE" }, { "billingAddress": { "city": "Pune", "countryCode": "IND", "line1": "2/2", "line2": "Range Hills", "line3": "Snake Hill Ave", "postalCode": 411012, "state": "MH" }, "cardAlias": "CardB", "cardholderName": "John Doe", "expiryInfo": { "month": 10, "year": 2025 }, "primaryAccountNumber": 2223003122001615, "primaryAccountNumberSource": "CARD_ON_FILE" } { "billingAddress": { "city": "Pune", "countryCode": "IND", "line1": "2/2", "line2": "Range Hills", "line3": "Snake Hill Ave", "postalCode": 411012, "state": "MH" }, "cardAlias": "CardB", "cardholderName": "John Doe", "expiryInfo": { "month": 10, "year": 2025 }, "primaryAccountNumber": 2223003122003222, "primaryAccountNumberSource": "CARD_ON_FILE" } ] } ``` 3. The second record having the same PAN is not added. The details of such a card with duplicate PAN appears in the `failedCards` list. ```json { "addedCards": [ { "maskedCard": "222300******3222","cardId": "dab4beae-7fba-4acc-8f08-76e4d70zxefa" }, { "maskedCard": "222300******1615","cardId": "dab4beae-7fba-4acc-8f08-76e4d70cbwme" } ], "failedCards": [ { "maskedCard": "222300******3222","reasonCode": "DUPLICATE_PRIMARY_ACCOUNT_NUMBER_IN_REQUEST" } ] } ``` 4. Save the card identifiers. This section discusses some of the failed scenarios and the error messages expected. If you try to add more than 10 cards in a single request, the request terminates with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE SERVICES", "ReasonCode": "DONATE_CARD_REQUEST_SIZE_EXCEEDED", "Description": "Request body contains more than 10 cards exceeding the limit.", "Recoverable": false, "Details": null } ] } } ``` ## GET /donors/{donor_id}/cards/{card_id} {#get-donorsdonor_idcardscard_id} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Retrieve a Card](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/retrieve-a-card/index.md) * [Retrieve a Card Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/retrieve-card-param/index.md) ## Test Cases {#test-cases} 1. To fetch information about a card, send a GET request. 2. To create a GET request, provide the `donorId` and `cardId` of the donor as a path parameter. ```py GET https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards/777a3120-f73a-4c5e-9e63-490d1fc5fe2a ``` 3. A successful request returns the `HTTP 200 Success` status code and a JSON response body with the card details. ```JSON { "oaepPaddingDigestAlgorithm": "SHA256", "billingAddress" : { "line1" : "2200", "line2" : "Mastercard", "city" : "O' Fallon", "postalCode" : "63368", "state" : "MO", "countryCode" : "IN" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Robert Doe", "primaryAccountNumber": "************8426", "cardId": "7777a3120-f73a-4c5e-9e63-490d1fc5fe2a", "primaryAccountNumberSuffix": "8426", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2023" }, "status": "ACTIVE", "cardAlias": "Card Alias Text", "tokenizedMerchants": null } ``` If a client is associated with more than one program, it is mandatory to pass a `programID` in the header to be able to retrieve donor details associated with that specific program ID: 1. To fetch information about a card, send a GET request. 2. Provide the `donorId` and `cardId` of the donor as a path parameter when fetching information about a particular card on file. ```py GET https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards/777a3120-f73a-4c5e-9e63-490d1fc5fe2a ``` 3. Provide the program ID in the request header: **Program ID**: 82e73e5c-f618-409a-8ca2-a212a0f649e3 4. A successful request returns the `HTTP 200 Success` status code and a JSON response body with the card details. ```JSON { "oaepPaddingDigestAlgorithm": "SHA256", "billingAddress" : { "line1" : "2200", "line2" : "Mastercard", "city" : "O' Fallon", "postalCode" : "63368", "state" : "MO", "countryCode" : "IN" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Robert Doe", "primaryAccountNumber": "************8426", "cardId": "7777a3120-f73a-4c5e-9e63-490d1fc5fe2a", "primaryAccountNumberSuffix": "8426", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2023" }, "status": "ACTIVE", "cardAlias": "Card Alias Text", "tokenizedMerchants": null } ``` This section discusses some of the failed scenarios and the error messages expected. Assuming that the client is associated with multiple programs, and no program ID is provided in the header when trying to retrieve specific card details: 1. Send a GET request with the `donorId` and `cardId` in the path parameter. ```py GET https://sandbox.api.mastercard.com/donations/donors/da9b61ef-dcad-4d2a-a09f-989ea6783bf3/cards/7b6b2c27-9c6d-47a7-85a5-bf9a421962d1 ``` 2. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "PROGRAM_ID_NOT_FOUND", "Description": "Program id is required as this client is mapped to multiple programs.", "Recoverable": false, "Details": null } ] } } ``` 1. Send a GET request with the `donorId` and the `cardId` of a deleted card in the path parameter. ```py GET https://sandbox.api.mastercard.com/donations/donors/da9b61ef-dcad-4d2a-a09f-989ea6783bf3/cards/7b6b2c27-9c6d-47a7-85a5-bf9a421 ``` 2. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "CARD_RECORD_NOT_FOUND", "Description": "No card record found.", "Recoverable": false, "Details": null } ] } } ``` ## GET /donors/{donor_id}/cards {#get-donorsdonor_idcards} ## Prerequisites {#prerequisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Retrieve all Cards](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/retrieve-all-cards/index.md) * [Retrieve all Card Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/retrieve-all-cards-param/index.md) ## Test Cases {#test-cases} 1. To fetch information about a card, send a GET request. 2. Provide the `donorId` of the donor as a path parameter when fetching the details of all cards registered for a donor on file. ```py GET https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards ``` 3. A successful request returns the `HTTP 200 Success` status code and a JSON response body with the details of all the cards-on-file registered for the donor. ```json { "oaepPaddingDigestAlgorithm": "SHA256", "offset": "0", "limit": "0", "totalItems": "2", "items": [ { "billingAddress": { "line1": "line1", "line2": "line2", "line3": "line3", "city": "PKL", "postalCode": "411027", "state": "CHD", "countryCode": "IND" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Renu", "primaryAccountNumber": "************0008", "cardId": "35161be7-1528-4678-83ea-0ba12991231f", "primaryAccountNumberSuffix": "0008", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2039" }, "status": "ACTIVE", "cardAlias": "MC", "tokenizedMerchants": null }, { "billingAddress": { "line1": "line1", "line2": "line2", "line3": "line3", "city": "PKL", "postalCode": "411027", "state": "CHD", "countryCode": "IND" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "RenuKapoor", "primaryAccountNumber": "************1019", "cardId": "402fa58a-ba8e-47e3-bbe6-c3e96338c7a1", "primaryAccountNumberSuffix": "1019", "paymentNetworkName": "visa", "expiryInfo": { "month": "08", "year": "2040" }, "status": "ACTIVE", "cardAlias": "VISA", "tokenizedMerchants": null } ] } ``` This section discusses some of the failed scenarios and the error messages expected. Assuming that the donor whose card details you want to retrieve is not associated with the client ID provided in the header: 1. To fetch information about a card, send a GET request. 2. Provide the `donorId` of the donor as a path parameter when fetching the details of all cards registered for a donor on file. 3. Change the client ID in the header. ```py GET https://sandbox.api.mastercard.com/donations/donors/c3111db9-fb41-4039-b454-2171eb4080c3/cards ``` 4. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "DONOR_ID_NOT_EXIST", "Description": "Donor does not exist for the client id 'L99sR0H2UAGqwtzMWyDE-nfCQN1NLYQUuQd0OoSu5b9302ac'.", "Recoverable": false, "Details": null } ] } } ``` Assuming that the client is not associated with the program with which the donor is registered, execute these steps: 1. To fetch information about a card, send a GET request. 2. Provide the `donorId` of the donor as a path parameter when fetching the details of all cards registered for a donor on file. 3. Change the program ID in the header. ```py GET https://sandbox.api.mastercard.com/donations/donors/c3111db9-fb41-4039-b454-2171eb4080c3/cards ``` 4. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "CLIENT_PROGRAM_NOT_ASSOCIATED", "Description": "The client with clientId 'L99sR0H2UAGqwtzMWyDE-nfCQN1NLYQUuQd0OoSu5b9302ac' is not associated with the program with programId '80380ef2-5037-4e57-86bf-1bdd1efb0ae2'.", "Recoverable": false, "Details": null } ] } } ``` ## POST /donors/{donor_id}/cards/searches {#post-donorsdonor_idcardssearches} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Search a Card](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/search-a-card/index.md) * [Search a Card Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/search-a-card-param/index.md) ## Test Cases {#test-cases} 1. To search information about a card, send a POST request. 2. Provide the `donorId` of the donor as a path parameter when searching information about a particular card on file. ```py POST https://sandbox.api.mastercard.com/donations/donors/9d2101aa-b316-4b41-bb4f-fc71b8f151f9/cards/searches ``` 3. Provide the PAN in the request body. ```json { "primaryAccountNumber": "2223000000000007" } ``` 4. A successful request returns the `HTTP 200 Success` status code and a JSON response body with the card details. ```json { "oaepPaddingDigestAlgorithm": "SHA256", "billingAddress": { "line1": "line1", "line2": "line2", "line3": "line3", "city": "PKL", "postalCode": "411027", "state": "CHD", "countryCode": "IND" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Kapoor", "primaryAccountNumber": "************0007", "cardId": "10df2c36-0200-4ab5-99db-c94eafd59be6", "primaryAccountNumberSuffix": "0007", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2034" }, "status": "ACTIVE", "cardAlias": "Mastercard", "tokenizedMerchants": null } ``` This section discusses some of the failed scenarios and the error messages expected. If you try to search for card details using a PAN that was not registered for the particular donor, you will get the following error code `400 Bad Request` as the response: ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "CARD_NOT_FOUND_FOR_PAN", "Description": "No card found for this PAN.", "Recoverable": false, "Details": null } ] } } ``` ## PUT /donors/{donor_id}/cards/{card_id} {#put-donorsdonor_idcardscard_id} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Update a Card](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/update-a-card/index.md) * [Update Card Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/update-a-card-param/index.md) ## Test Cases {#test-cases} 1. To update a card-on-file for an existing donor, send a PUT request. 2. To create a PUT request, provide the `donorId` and `cardId` of the donor as a path parameter. ```py PUT https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards/777a3120-f73a-4c5e-9e63-490d1fc5fe2a ``` 3. Provide a different address for the card as shown in this request. ```json { "billingAddress": { "line1": "Citadel Enclave, Sicilia", "line2": "22/4", "line3": "22nd Floor", "city": "Pune", "postalCode": "411012", "state": "MH", "countryCode": "IND" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Kapoor", "primaryAccountNumber": "71930787711008431", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2034" }, "cardAlias": "Mastercard", } ``` 4. A successful request returns the `HTTP 200 Created` status code and a JSON response body that includes a unique card identifier and last 4 digits of card. ```JSON { "cardId" : "777a3120-f73a-4c5e-9e63-490d1fc5fc1a", "primaryAccountNumberSuffix" : "8431", } ``` This section discusses a failed scenario and the error message expected. 1. Create a PUT request with the following parameters: ```py PUT https://sandbox.api.mastercard.com/donations/donors/44e15e022bb10c4ba334f7e823d4ae76dc672720/cards/777a3120-f73a-4c5e-9e63-490d1fc5fe2a ``` 2. Provide an backdated expiry date. ```json { "billingAddress": { "line1": "Citadel Enclave, Sicilia", "line2": "22/4", "line3": "22nd Floor", "city": "Pune", "postalCode": "411012", "state": "MH", "countryCode": "IND" }, "primaryAccountNumberSource": "CARD_ON_FILE", "cardholderName": "Kapoor", "primaryAccountNumber": "71930787711008431", "paymentNetworkName": "mastercard", "expiryInfo": { "month": "01", "year": "2024" }, "cardAlias": "Mastercard", } ``` 4. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "CARD_EXPIRED", "Description": "Card is expired.", "Recoverable": false, "Details": null } ] } } ``` ## DELETE /donors/{donor_id}/cards/{card_id} {#delete-donorsdonor_idcardscard_id} ## Pre-requisites {#pre-requisites} Before executing the test cases, acquaint yourself with the endpoint use case and parameters: * [Delete a Card](https://developer.mastercard.com/donations/documentation/use-cases/manage-cards/delete-a-card/index.md) * [Delete Card Parameters](https://developer.mastercard.com/donations/documentation/parameters/manage-cards-param/delete-a-card-param/index.md) ## Test Cases {#test-cases} 1. Before deleting a card-on-file, verify that there are no donation setups active on the card. 2. To delete a card-on-file for an existing donor, send a DELETE request. 3. To create a DELETE request, provide the `donorId` and `cardId` of the donor as a path parameter. ```py DELETE https://sandbox.api.mastercard.com/donations/donors/da9b61ef-dcad-4d2a-a09f-989ea6783bf3/cards/7b6b2c27-9c6d-47a7-85a5-bf9a421962d1 ``` 4. A successful request returns the with message `HTTP 204` status code with the message '**This card with this id 7b6b2c27-9c6d-47a7-85a5-bf9a421962d1 is deleted**'. This section discusses a failed scenario and the error message expected. 1. Send a DELETE request with a `donor_id` and a `card_id` of a card with active donation setups. ```py DELETE https://sandbox.api.mastercard.com/donations/donors/40ec82af-6be7-4d83-be6a-5665b122aebf/cards/ee86e31f-06e2-42ea-ab55-1ff65bf0b36b ``` 2. This request fails with the error code `400 Bad Request` as the response. ```json { "Errors": { "Error": [ { "Source": "DONATE_SERVICE", "ReasonCode": "SETUP_DONATION_STATUS_ACTIVE", "Description": "Donation setup status is active for donor id '40ec82af-6be7-4d83-be6a-5665b122aebf', card id 'ee86e31f-06e2-42ea-ab55-1ff65bf0b36b' and program id '82e73e5c-f618-409a-8ca2-a212a0f649e3'.", "Recoverable": false, "Details": null } ] } } ```