001/* 002 * Copyright 2024 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.subaccounts; 017 018import com.fasterxml.jackson.annotation.JsonProperty; 019import com.vonage.client.Jsonable; 020import java.math.BigDecimal; 021import java.time.Instant; 022import java.util.Objects; 023import java.util.UUID; 024 025/** 026 * Represents a credit or balance transfer between accounts. 027 */ 028public class MoneyTransfer extends AbstractTransfer { 029 private UUID id; 030 private Instant createdAt; 031 private BigDecimal amount; 032 private String reference; 033 034 protected MoneyTransfer() { 035 } 036 037 protected MoneyTransfer(Builder builder) { 038 super(builder); 039 amount = Objects.requireNonNull(builder.amount, "Amount is required."); 040 reference = builder.reference; 041 } 042 043 /** 044 * Unique ID of the transfer. 045 * 046 * @return The transfer ID. 047 */ 048 @JsonProperty("id") 049 public UUID getId() { 050 return id; 051 } 052 053 /** 054 * The date and time when the transfer was executed. 055 * 056 * @return The transfer timestamp in ISO 8601 format. 057 */ 058 @JsonProperty("created_at") 059 public Instant getCreatedAt() { 060 return createdAt; 061 } 062 063 /** 064 * Transfer amount. 065 * 066 * @return The monetary amount to transfer between accounts. 067 */ 068 @JsonProperty("amount") 069 public BigDecimal getAmount() { 070 return amount; 071 } 072 073 /** 074 * Reference for the transfer. 075 * 076 * @return The transfer reference message, or {@code null} if not set (the default). 077 */ 078 @JsonProperty("reference") 079 public String getReference() { 080 return reference; 081 } 082 083 /** 084 * Creates an instance of this class from a JSON payload. 085 * 086 * @param json The JSON string to parse. 087 * @return An instance of this class with the fields populated, if present. 088 */ 089 public static MoneyTransfer fromJson(String json) { 090 return Jsonable.fromJson(json); 091 } 092 093 /** 094 * Entry point for constructing an instance of this class. 095 * 096 * @return A new Builder. 097 */ 098 public static Builder builder() { 099 return new Builder(); 100 } 101 102 public static class Builder extends AbstractTransfer.Builder<MoneyTransfer, Builder> { 103 private BigDecimal amount; 104 private String from, to, reference; 105 106 Builder() {} 107 108 /** 109 * (REQUIRED) Transfer amount. 110 * 111 * @param amount The monetary amount to transfer between accounts. 112 * 113 * @return This builder. 114 */ 115 public Builder amount(BigDecimal amount) { 116 this.amount = amount; 117 return this; 118 } 119 120 /** 121 * (REQUIRED) Transfer amount. 122 * 123 * @param amount The monetary amount to transfer between accounts. 124 * 125 * @return This builder. 126 */ 127 public Builder amount(double amount) { 128 return amount(BigDecimal.valueOf(amount)); 129 } 130 131 /** 132 * (OPTIONAL) Reference for the transfer. 133 * 134 * @param reference The transfer reference message, or {@code null} if not set (the default). 135 * 136 * @return This builder. 137 */ 138 public Builder reference(String reference) { 139 this.reference = reference; 140 return this; 141 } 142 143 /** 144 * Builds the {@linkplain MoneyTransfer}. 145 * 146 * @return An instance of MoneyTransfer, populated with all fields from this builder. 147 */ 148 public MoneyTransfer build() { 149 return new MoneyTransfer(this); 150 } 151 } 152 153}