Advertisement
sehpa

Untitled

Nov 18th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. <script>
  2. function sendInvoice() {
  3. let apiResponse = JSON.parse(document.getElementById("apiResponse").innerText);
  4. let chargeDescription = document.getElementById("invoiceDescription").value;
  5. //let customerEmail = document.getElementById("customerEmail").value;
  6. let customerEmail = document.getElementsByName("accounts")[0].value;
  7. let authToken = 'Bearer rk_live_';
  8. $.ajax({
  9. type: 'POST',
  10. url: 'https://api.stripe.com/v1/customers/' + apiResponse.data.customer,
  11. headers: {
  12. Authorization: authToken
  13. },
  14. data: {
  15. email: customerEmail,
  16. },
  17. success: (response) => {
  18. console.log('email updated: ', response);
  19.  
  20. // Create Invoice Item
  21. $.ajax({
  22. type: 'POST',
  23. url: 'https://api.stripe.com/v1/invoiceitems',
  24. headers: {
  25. Authorization: authToken
  26. },
  27. data: {
  28. customer: apiResponse.data.customer,
  29. amount: apiResponse.data.amount,
  30. currency: 'usd',
  31. description: chargeDescription,
  32. },
  33. success: (response) => {
  34. console.log('Invoice Item Created: ', response);
  35.  
  36. // Create Invoice
  37. $.ajax({
  38. type: 'POST',
  39. url: 'https://api.stripe.com/v1/invoices',
  40. headers: {
  41. Authorization: authToken
  42. },
  43. data: {
  44. customer: apiResponse.data.customer,
  45. collection_method: 'send_invoice',
  46. days_until_due: '15',
  47.  
  48. },
  49. success: (response) => {
  50. console.log('Invoice Created: ', response);
  51.  
  52. // Mark Invoice Paid
  53. $.ajax({
  54. type: 'POST',
  55. url: 'https://api.stripe.com/v1/invoices/'+response.id+'/pay',
  56. headers: {
  57. Authorization: authToken
  58. },
  59. data: {
  60. paid_out_of_band: 'True',
  61. },
  62. success: (response) => {
  63. console.log('Invoice Paid: ', response);
  64. // Email the Invoice
  65. $.ajax({
  66. type: 'POST',
  67. url: 'https://api.stripe.com/v1/invoices/'+response.id+'/send',
  68. headers: {
  69. Authorization: authToken
  70. },
  71. success: (response) => {
  72. console.log('Invoice Emailed: ', response);
  73. },
  74. error: (response) => {
  75. console.log('Invoice Emailing Failed: ', response);
  76. }
  77. }); // Email Invoice End
  78. },
  79. error: (response) => {
  80. console.log('Invoice Pay Failed: ', response);
  81. }
  82. }); // Mark invoice Paid End
  83. },
  84. error: (response) => {
  85. console.log('Invoice Creation Failed: ', response);
  86. }
  87. });
  88. },
  89. error: (response) => {
  90. console.log('Invoice Item Failed: ', response);
  91. }
  92. });
  93.  
  94. }, // email update success end
  95. error: (response) => {
  96. console.log('error payment: ', response);
  97. }
  98. })
  99. };
  100.  
  101. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement