Save Credit Card for later

matt-james
Contributor
Contributor

Is it possible to save a credit card for use later (much later) with the REST APIs and get a token back that I can use for future purchases.  We're building a platform of a service based organization and are trying to use PayPal as the payment processor and my previous attempts didn't work.  I was trying to use saved tokens in the vault (https://developer.paypal.com/docs/checkout/save-payment-methods/purchase-later/payment-tokens-api/ca...), but after some of the cards I'd stored stopping working, I'm being told that the tokens have a very short life span (though I can't find how long or any documentation to that effect).  Any other ideas?  I have two use cases that I'm trying to cover.  First is just saving the payment method for later, to make paying the next invoice easier.  The second is allowing automatic payment of invoices.  Invoices aren't generated on any kind of schedule or even for the same amount, so subscriptions really aren't going to get me there.  I'm trying to do all of this within my own UI, so I'd rather not have to kick the user over to a PayPal interface to pay.  And, even if I did that for the manual payment option, I don't think that would allow for the automatic option if the user chooses to do so.

 

Thoughts or options?  API calls that I (apparently) haven't found?

 

Thanks - Matt

Login to Me Too
1 REPLY 1

mhelmntrs
Contributor
Contributor
For securely saving credit card information and facilitating future payments, PayPal offers the Vault feature. The Payment Tokens API is part of the Vault and is suitable for your use case. Here's a general outline of how you can use the Payment Tokens API: 1. **Store a Credit Card in the Vault:** - Use the `vault/payment-tokens` API to store the credit card information securely in the Vault. - This will return a payment token that you can associate with your customer or save for future use. 2. **Use the Token for Future Payments:** - When your customer returns to make a payment, retrieve the stored payment token. - Use the token in the `purchase_units` section of your API call to create an order or capture payment. 3. **Payment Token Lifespan:** - While there isn't a specific documentation mentioning the lifespan of payment tokens, they are generally intended for long-term use. It's more likely that the issue you faced might be related to the cards themselves rather than the tokens. 4. **Automatic Payments for Invoices:** - If you want to facilitate automatic payments for invoices, you can schedule your system to initiate payments using the stored payment token associated with the customer. 5. **Additional Considerations:** - Ensure that you comply with PCI DSS requirements when handling credit card information. - Consider tokenizing the credit card information on your server before sending it to PayPal to minimize security risks. Here's a basic example using the PayPal REST API in Javascript: ```javascript // 1. Store Credit Card in the Vault const createToken = async () => { const response = await fetch('https://api.sandbox.paypal.com/v2/vault/payment-tokens', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer YOUR_ACCESS_TOKEN`, }, body: JSON.stringify({ card: { number: '4111111111111111', expiration_date[Removed. Phone #s not permitted], }, }), }); const { id } = await response.json(); // 'id' is the payment token, associate it with your customer or save it for future use console.log(id); }; // 2. Use Token for Future Payments const makePayment = async (paymentToken) => { const response = await fetch('https://api.sandbox.paypal.com/v2/checkout/orders', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer YOUR_ACCESS_TOKEN`, }, body: JSON.stringify({ purchase_units: [{ amount: { currency_code: 'USD', value: '100.00', }, }], payment_source: { token: { id: paymentToken, }, }, }), }); const { id } = await response.json(); // 'id' is the order ID for the payment console.log(id); }; // Example Usage createToken(); // Save the returned payment token and use it later for payments // makePayment('YOUR_SAVED_PAYMENT_TOKEN'); ``` Make sure to replace `'YOUR_ACCESS_TOKEN'` and `'YOUR_SAVED_PAYMENT_TOKEN'` with your actual access token and payment token, respectively. Always consult the latest PayPal API documentation for any updates or changes in API usage.
Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.