Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function sendInvoice() {
- let apiResponse = JSON.parse(document.getElementById("apiResponse").innerText);
- let chargeDescription = document.getElementById("invoiceDescription").value;
- //let customerEmail = document.getElementById("customerEmail").value;
- let customerEmail = document.getElementsByName("accounts")[0].value;
- let authToken = 'Bearer rk_live_';
- $.ajax({
- type: 'POST',
- url: 'https://api.stripe.com/v1/customers/' + apiResponse.data.customer,
- headers: {
- Authorization: authToken
- },
- data: {
- email: customerEmail,
- },
- success: (response) => {
- console.log('email updated: ', response);
- // Create Invoice Item
- $.ajax({
- type: 'POST',
- url: 'https://api.stripe.com/v1/invoiceitems',
- headers: {
- Authorization: authToken
- },
- data: {
- customer: apiResponse.data.customer,
- amount: apiResponse.data.amount,
- currency: 'usd',
- description: chargeDescription,
- },
- success: (response) => {
- console.log('Invoice Item Created: ', response);
- // Create Invoice
- $.ajax({
- type: 'POST',
- url: 'https://api.stripe.com/v1/invoices',
- headers: {
- Authorization: authToken
- },
- data: {
- customer: apiResponse.data.customer,
- collection_method: 'send_invoice',
- days_until_due: '15',
- },
- success: (response) => {
- console.log('Invoice Created: ', response);
- // Mark Invoice Paid
- $.ajax({
- type: 'POST',
- url: 'https://api.stripe.com/v1/invoices/'+response.id+'/pay',
- headers: {
- Authorization: authToken
- },
- data: {
- paid_out_of_band: 'True',
- },
- success: (response) => {
- console.log('Invoice Paid: ', response);
- // Email the Invoice
- $.ajax({
- type: 'POST',
- url: 'https://api.stripe.com/v1/invoices/'+response.id+'/send',
- headers: {
- Authorization: authToken
- },
- success: (response) => {
- console.log('Invoice Emailed: ', response);
- },
- error: (response) => {
- console.log('Invoice Emailing Failed: ', response);
- }
- }); // Email Invoice End
- },
- error: (response) => {
- console.log('Invoice Pay Failed: ', response);
- }
- }); // Mark invoice Paid End
- },
- error: (response) => {
- console.log('Invoice Creation Failed: ', response);
- }
- });
- },
- error: (response) => {
- console.log('Invoice Item Failed: ', response);
- }
- });
- }, // email update success end
- error: (response) => {
- console.log('error payment: ', response);
- }
- })
- };
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement