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.

80 lines
2.3 KiB

  1. // TRANSFER
  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.RecurringPaymentTransfer
  8. import org.cyclos.entities.banking.ScheduledPaymentInstallmentTransfer
  9. import org.cyclos.entities.banking.FailedPaymentOccurrence
  10. def url = ''
  11. def jsonBody = []
  12. def tf = scriptHelper.wrap(transfer)
  13. if (! ((transfer instanceof RecurringPaymentTransfer) | (transfer instanceof ScheduledPaymentInstallmentTransfer) | (transfer instanceof FailedPaymentOccurrence)) ){
  14. return
  15. }
  16. if( (transfer instanceof RecurringPaymentTransfer) | (transfer instanceof FailedPaymentOccurrence) ){
  17. url = 'http://front:8000/operations/sync/recurring'
  18. jsonBody = [
  19. paymentID: maskId(tf.transferId),
  20. transactionID: maskId(tf.recurringPayment.id),
  21. amount: tf.amount,
  22. description: tf.recurringPayment.description,
  23. fromAccountNumber: tf.from.number,
  24. toAccountNumber: tf.to.number,
  25. status: tf.status
  26. ]
  27. } else {
  28. url = 'http://front:8000/operations/sync/scheduled'
  29. jsonBody = [
  30. paymentID: maskId(tf.installment.transferId),
  31. transactionID: maskId(tf.transactionId),
  32. amount: tf.amount,
  33. description: tf.transaction.description,
  34. fromAccountNumber: tf.from.number,
  35. toAccountNumber: tf.to.number,
  36. status: tf.installment.status
  37. ]
  38. }
  39. // Send the POST request
  40. def http = new HTTPBuilder(url)
  41. http.headers["Content-Type"] = "application/json; charset=UTF-8"
  42. def responseJson = null
  43. def responseError = []
  44. scriptHelper.addOnCommit {
  45. CountDownLatch latch = new CountDownLatch(1)
  46. def error = false
  47. http.request(POST, JSON) {
  48. body = jsonBody
  49. response.success = { resp, json ->
  50. responseJson = json
  51. latch.countDown()
  52. }
  53. response.failure = { resp ->
  54. responseError << resp.statusLine.statusCode
  55. responseError << resp.statusLine.reasonPhrase
  56. latch.countDown()
  57. }
  58. }
  59. //Await for the response
  60. latch.await()
  61. if (!responseError.empty) {
  62. throw new RuntimeException("Error making Cyclos sync to ${url}"
  63. + ", got error code ${responseError[0]}: ${responseError[1]}")
  64. }
  65. return responseJson
  66. }