Chilkat Online Tools

Android / Salesforce Platform APIs / Create Payment Method

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();
    boolean success;

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "paymentGatewayId": "{{defaultPaymentGatewayId}}",
    //   "cardPaymentMethod": {
    //     "cardHolderName": "{{_userFullName}}",
    //     "expiryMonth": "11",
    //     "expiryYear": "2222",
    //     "cardNumber": "4111111111111111",
    //     "cvv": "111",
    //     "cardCategory": "CreditCard",
    //     "cardType": "VISA",
    //     "startYear": "2013",
    //     "startMonth": "7",
    //     "nickName": "Default Payment Method",
    //     "accountId": "{{defaultAccountId}}"
    //   },
    //   "phone": "4155551212",
    //   "email": "{{_userEmail}}",
    //   "additionalData": {
    //     "key1": "value1",
    //     "key2": "value2"
    //   }
    // }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("paymentGatewayId","{{defaultPaymentGatewayId}}");
    json.UpdateString("cardPaymentMethod.cardHolderName","{{_userFullName}}");
    json.UpdateString("cardPaymentMethod.expiryMonth","11");
    json.UpdateString("cardPaymentMethod.expiryYear","2222");
    json.UpdateString("cardPaymentMethod.cardNumber","4111111111111111");
    json.UpdateString("cardPaymentMethod.cvv","111");
    json.UpdateString("cardPaymentMethod.cardCategory","CreditCard");
    json.UpdateString("cardPaymentMethod.cardType","VISA");
    json.UpdateString("cardPaymentMethod.startYear","2013");
    json.UpdateString("cardPaymentMethod.startMonth","7");
    json.UpdateString("cardPaymentMethod.nickName","Default Payment Method");
    json.UpdateString("cardPaymentMethod.accountId","{{defaultAccountId}}");
    json.UpdateString("phone","4155551212");
    json.UpdateString("email","{{_userEmail}}");
    json.UpdateString("additionalData.key1","value1");
    json.UpdateString("additionalData.key2","value2");

    // Adds the "Authorization: Bearer <access_token>" header.
    http.put_AuthToken("<access_token>");

    CkHttpResponse resp = http.PostJson3("https://domain.com/services/data/v{{version}}/commerce/payments/payment-methods","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, String.valueOf(resp.get_StatusCode()));
    Log.i(TAG, resp.bodyStr());


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "paymentGatewayId" : "{{defaultPaymentGatewayId}}",
    "cardPaymentMethod": {
    "cardHolderName": "{{_userFullName}}",
    "expiryMonth": "11",
    "expiryYear": "2222",
    "cardNumber": "4111111111111111",
    "cvv": "111", 
    "cardCategory": "CreditCard",
    "cardType": "VISA",
    "startYear": "2013",
    "startMonth": "7",
    "nickName": "Default Payment Method",
    "accountId": "{{defaultAccountId}}"
    },
  "phone": "4155551212",
  "email": "{{_userEmail}}",
  "additionalData":{
      "key1":"value1",
      "key2":"value2"
    }
}'
https://domain.com/services/data/v{{version}}/commerce/payments/payment-methods

Postman Collection Item JSON

{
  "name": "Create Payment Method",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"paymentGatewayId\" : \"{{defaultPaymentGatewayId}}\",\n    \"cardPaymentMethod\": {\n    \"cardHolderName\": \"{{_userFullName}}\",\n    \"expiryMonth\": \"11\",\n    \"expiryYear\": \"2222\",\n    \"cardNumber\": \"4111111111111111\",\n    \"cvv\": \"111\", \n    \"cardCategory\": \"CreditCard\",\n    \"cardType\": \"VISA\",\n    \"startYear\": \"2013\",\n    \"startMonth\": \"7\",\n    \"nickName\": \"Default Payment Method\",\n    \"accountId\": \"{{defaultAccountId}}\"\n    },\n  \"phone\": \"4155551212\",\n  \"email\": \"{{_userEmail}}\",\n  \"additionalData\":{\n      \"key1\":\"value1\",\n      \"key2\":\"value2\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/commerce/payments/payment-methods",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "commerce",
        "payments",
        "payment-methods"
      ]
    }
  },
  "response": [
  ]
}