-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAddCardToCustomer.java
44 lines (31 loc) · 1.6 KB
/
AddCardToCustomer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.HashMap;
import java.util.Map;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Card;
import com.stripe.model.Customer;
import com.stripe.model.Token;
public class AddCardToCustomer {
public static void main(String[] args) throws StripeException {
// add stripe key
Stripe.apiKey = "*****";
//for updated code, check here : https://github.com/talenteddeveloper/Stripe-api/edit/master/AddCardToCustomerUpdatedCode.java
Customer customer = Customer.retrieve("cus_JPujXXXX"); //add customer id here : it will start with cus_
Map<String, Object> cardParam = new HashMap<String, Object>(); //add card details
cardParam.put("number", "4111111111111111");
cardParam.put("exp_month", "11");
cardParam.put("exp_year", "2026");
cardParam.put("cvc", "123");
Map<String, Object> tokenParam = new HashMap<String, Object>();
tokenParam.put("card", cardParam);
Token token = Token.create(tokenParam); // create a token
Map<String, Object> source = new HashMap<String, Object>();
source.put("source", token.getId()); //add token as source
Card card = (Card)customer.getSources().create(source); // add the customer details to which card is need to link
String cardDetails = card.toJson();
System.out.println("Card Details : " + cardDetails);
customer = Customer.retrieve("cus_JPujXXXX");//change the customer id or use to get customer by id
System.out.println("After adding card, customer details : " + customer);
// sample output in stripe dashboard : https://github.com/talenteddeveloper/Stripe-api/blob/master/Added%20Card.jpg
}
}