Doc, tools for lokavaluto development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB

  1. // TRANSACTION
  2. import static groovyx.net.http.ContentType.*
  3. import static groovyx.net.http.Method.*
  4. import groovyx.net.http.HTTPBuilder
  5. import java.util.concurrent.CountDownLatch
  6. import org.cyclos.model.ValidationException
  7. import org.cyclos.entities.banking.ScheduledPayment
  8. import org.cyclos.entities.banking.RecurringPayment
  9. import org.cyclos.model.banking.transactions.RecurringPaymentStatus
  10. import org.cyclos.model.banking.transactions.RecurringPaymentOccurrenceStatus
  11. import org.cyclos.model.banking.transactions.ScheduledPaymentInstallmentStatus
  12. def url = ''
  13. def jsonBody = []
  14. def tx = scriptHelper.wrap(transaction)
  15. if (! ((transaction instanceof RecurringPayment) | (transaction instanceof ScheduledPayment)) ){
  16. return
  17. }
  18. def sendWarning = false
  19. if( transaction instanceof RecurringPayment ){
  20. if(tx.status != RecurringPaymentStatus.CANCELED){
  21. if( tx.lastOccurrence.status == RecurringPaymentOccurrenceStatus.FAILED){
  22. sendWarning = true
  23. url = 'http://172.18.0.2:8000/operations/sync/recurring'
  24. jsonBody = [
  25. paymentID: maskId(tx.lastOccurrence.transferId), //will be null
  26. transactionID: maskId(tx.id),
  27. amount: tx.amount,
  28. description: tx.description,
  29. status: tx.lastOccurrence.status
  30. ]
  31. }
  32. }
  33. } else { // scheduled payment
  34. if( tx.firstInstallment.status == ScheduledPaymentInstallmentStatus.FAILED){
  35. sendWarning = true
  36. url = 'http://172.18.0.2:8000/operations/sync/scheduled'
  37. jsonBody = [
  38. paymentID: maskId(tx.firstInstallment.transferId), //will be null
  39. transactionID: maskId(tx.id),
  40. amount: tx.amount,
  41. description: tx.description,
  42. status: tx.firstInstallment.status
  43. ]
  44. }
  45. }
  46. if (sendWarning == true){
  47. // Send the POST request
  48. def http = new HTTPBuilder(url)
  49. http.headers["Content-Type"] = "application/json; charset=UTF-8"
  50. def responseJson = null
  51. def responseError = []
  52. scriptHelper.addOnCommit {
  53. CountDownLatch latch = new CountDownLatch(1)
  54. def error = false
  55. http.request(POST, JSON) {
  56. body = jsonBody
  57. response.success = { resp, json ->
  58. responseJson = json
  59. latch.countDown()
  60. }
  61. response.failure = { resp ->
  62. responseError << resp.statusLine.statusCode
  63. responseError << resp.statusLine.reasonPhrase
  64. latch.countDown()
  65. }
  66. }
  67. //Await for the response
  68. latch.await()
  69. if (!responseError.empty) {
  70. throw new RuntimeException("Error making Cyclos sync to ${url}"
  71. + ", got error code ${responseError[0]}: ${responseError[1]}")
  72. }
  73. return responseJson
  74. }
  75. } else {
  76. return
  77. }