-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
62 lines (46 loc) · 1.98 KB
/
plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pasdk
// PlanRequest accepts a transaction amount and an optional plan ID,
// returning a full payment schedule including amounts and dates.
type PlanRequest struct {
Amount int // The invoice amount in pence.
PlanID *int // The plan ID. If empty, the account's default plan is used.
}
// PlanResponse contains the data returned by a successful call to the "plan" endpoint.
type PlanResponse struct {
PlanName string `json:"plan"` // The name of this plan.
Amount int `json:"amount"` // The amount you requested, in pence.
Interest int `json:"interest"` // The amount of interest payable, in pence.
TotalRepayable int `json:"repayable"` // The total amount that would be repayable under this plan, in pence.
PaymentSchedule []Repayment `json:"schedule"` // A breakdown of what the repayments would look like under this plan.
}
// Fetch executes the request.
func (request PlanRequest) Fetch() (response *PlanResponse, err *PASDKError) {
defer catchGenericPanic(&response, &err)
err = validatePlanRequest(request)
if err != nil {
return nil, err.Wrap("request is invalid: ")
}
requestParams := []string{
"amount=" + toString(request.Amount),
"plan_id=" + toString(request.PlanID),
}
requestParams = removeEmptyParams(requestParams)
signature := generateSignature(requestParams, userCredentials.APISecret)
requestParams = append(requestParams, "api_key="+userCredentials.APIKey)
requestParams = append(requestParams, "signature="+signature)
requestURL, err := getRequestURL()
if err != nil {
return nil, err.Wrap("failed determining request URL: ")
}
response, err = makeAPIPOSTRequest[PlanResponse](requestParams, requestURL+"plan")
if err != nil {
return nil, err.Wrap("API request failed: ")
}
return response, nil
}
func validatePlanRequest(request PlanRequest) (err *PASDKError) {
if request.Amount <= 0 {
return buildValidationFailedError("field Amount must be greater than 0")
}
return nil
}