-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
482 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,32 @@ | |
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.impactupgrade.nucleus.environment.Environment; | ||
import com.impactupgrade.nucleus.util.HttpClient; | ||
import com.impactupgrade.nucleus.util.OAuth2Util; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.impactupgrade.nucleus.util.HttpClient.get; | ||
import static com.impactupgrade.nucleus.util.HttpClient.post; | ||
|
||
public class RaiselyClient{ | ||
public class RaiselyClient { | ||
|
||
private static final Logger log = LogManager.getLogger(RaiselyClient.class); | ||
private static final String RAISELY_API_URL = "https://api.raisely.com/v3/"; | ||
private static final String APPLICATION_JSON = "application/json"; | ||
private static final String AUTH_URL = RAISELY_API_URL + "login"; | ||
|
||
protected final Environment env; | ||
|
||
private String _accessToken = null; | ||
protected static OAuth2Util.Tokens tokens; | ||
|
||
private String username; | ||
private String password; | ||
|
||
public RaiselyClient(Environment env){ | ||
public RaiselyClient(Environment env) { | ||
this.env = env; | ||
this.username = env.getConfig().raisely.username; | ||
this.password = env.getConfig().raisely.password; | ||
} | ||
|
||
//*Note this uses the donation ID from the Stripe metadata. Different from the donation UUID | ||
|
@@ -34,7 +40,7 @@ public RaiselyClient(Environment env){ | |
* like this: https://api.raisely.com/v3/donations?idGTE=14619704&idLTE=14619704&private=true | ||
*/ | ||
|
||
public RaiselyClient.Donation getDonation(String donationId){ | ||
public RaiselyClient.Donation getDonation(String donationId) { | ||
DonationResponse response = get( | ||
RAISELY_API_URL + "donations?idGTE=" + donationId + "&idLTE=" + donationId + "&private=true", | ||
HttpClient.HeaderBuilder.builder().authBearerToken(getAccessToken()), | ||
|
@@ -49,27 +55,14 @@ public RaiselyClient.Donation getDonation(String donationId){ | |
} | ||
|
||
protected String getAccessToken() { | ||
if (_accessToken == null) { | ||
String username = env.getConfig().raisely.username; | ||
String password = env.getConfig().raisely.password; | ||
|
||
log.info("Getting token..."); | ||
HttpClient.HeaderBuilder headers = HttpClient.HeaderBuilder.builder(); | ||
TokenResponse response = post(RAISELY_API_URL + "login", Map.of("requestAdminToken", "true", "username", username, "password", password), APPLICATION_JSON, headers, TokenResponse.class); | ||
log.info("Token: {}", response.token); | ||
this._accessToken = response.token; | ||
tokens = OAuth2Util.refreshTokens(tokens, AUTH_URL); | ||
if (tokens == null) { | ||
tokens = OAuth2Util.getTokensForUsernameAndPassword(username, password, AUTH_URL); | ||
} | ||
|
||
return this._accessToken; | ||
return tokens != null ? tokens.accessToken() : null; | ||
} | ||
|
||
//Response Objects | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class TokenResponse { | ||
@JsonProperty("token") | ||
public String token; | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class DonationResponse { | ||
public List<Donation> data; | ||
|
@@ -94,12 +87,12 @@ public static class Donation { | |
@Override | ||
public String toString() { | ||
return "Donation{" + | ||
"amount=" + amount + | ||
", fee=" + fee + | ||
", feeOptIn = " + feeCovered + | ||
", total='" + total + '\'' + | ||
", items='" + items + '\'' + | ||
'}'; | ||
"amount=" + amount + | ||
", fee=" + fee + | ||
", feeOptIn = " + feeCovered + | ||
", total='" + total + '\'' + | ||
", items='" + items + '\'' + | ||
'}'; | ||
} | ||
} | ||
|
||
|
@@ -113,13 +106,21 @@ public static class DonationItem { | |
@Override | ||
public String toString() { | ||
return "Item{" + | ||
"amount=" + amount + | ||
", amountRefunded=" + amountRefunded + | ||
", type='" + type + '\'' + | ||
", quantity='" + quantity + '\'' + | ||
'}'; | ||
"amount=" + amount + | ||
", amountRefunded=" + amountRefunded + | ||
", type='" + type + '\'' + | ||
", quantity='" + quantity + '\'' + | ||
'}'; | ||
} | ||
} | ||
|
||
//TODO test with real creds | ||
public static void main(String[] args) { | ||
String username = "[email protected]"; | ||
String password = "6.tw*gghr.fyDDjkjaZj"; | ||
String tokenServerUrl = AUTH_URL; | ||
|
||
tokens = OAuth2Util.getTokensForUsernameAndPassword(username, password, Map.of("requestAdminToken", "true"), tokenServerUrl); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.