-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayments.go
141 lines (129 loc) · 6.23 KB
/
payments.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package mercadopago
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const (
// The user has not yet completed the payment process.
StatusPending = "pending"
// The payment has been approved and accredited.
StatusApproved = "approved"
// The payment has been authorized but not captured yet.
StatusAuthorized = "authorized"
// Payment is being reviewed.
StatusInProcess = "in_process"
// Users have initiated a dispute.
StatusInMediation = "in_mediation"
// Payment was rejected. The user may retry payment.
StatusRejected = "rejected"
// Payment was cancelled by one of the parties or because time for payment has expired
StatusCancelled = "cancelled"
// Payment was refunded to the user.
StatusRefunded = "refunded"
// Was made a chargeback in the buyer’s credit card.
StatusChargedBack = "charged_back"
)
type PaymentParams struct {
TransactionAmount float64 `json:"transaction_amount"`
PaymentMethodID string `json:"payment_method_id"`
Payer Payer `json:"payer"`
Token string `json:"token"`
Description string `json:"description"`
Installments int `json:"installments"`
NotificationURL string `json:"notification_url,omitempty"`
SponsorID interface{} `json:"sponsor_id"`
BinaryMode bool `json:"binary_mode"`
ExternalReference string `json:"external_reference"`
StatementDescriptor string `json:"statement_descriptor"`
AdditionalInfo interface{} `json:"additional_info"`
}
type Payment struct {
ID int `json:"id"`
DateCreated string `json:"date_created"`
DateApproved string `json:"date_approved"`
DateLastUpdated string `json:"date_last_updated"`
DateOfExpiration interface{} `json:"date_of_expiration"`
MoneyReleaseDate string `json:"money_release_date"`
OperationType string `json:"operation_type"`
IssuerID string `json:"issuer_id"`
PaymentMethodID string `json:"payment_method_id"`
PaymentTypeID string `json:"payment_type_id"`
Status string `json:"status"`
StatusDetail string `json:"status_detail"`
CurrencyID string `json:"currency_id"`
Description string `json:"description"`
LiveMode bool `json:"live_mode"`
SponsorID interface{} `json:"sponsor_id"`
AuthorizationCode interface{} `json:"authorization_code"`
MoneyReleaseSchema interface{} `json:"money_release_schema"`
TaxesAmount float64 `json:"taxes_amount"`
CounterCurrency interface{} `json:"counter_currency"`
ShippingAmount float64 `json:"shipping_amount"`
PosID interface{} `json:"pos_id"`
StoreID interface{} `json:"store_id"`
CollectorID int `json:"collector_id"`
Payer Payer `json:"payer"`
Metadata struct{} `json:"metadata"`
AdditionalInfo interface{} `json:"additional_info"`
Order struct{} `json:"order"`
ExternalReference string `json:"external_reference"`
TransactionAmount float64 `json:"transaction_amount"`
TransactionAmountRefunded float64 `json:"transaction_amount_refunded"`
CouponAmount float64 `json:"coupon_amount"`
DifferentialPricingID interface{} `json:"differential_pricing_id"`
DeductionSchema interface{} `json:"deduction_schema"`
TransactionDetails TransactionDetails `json:"transaction_details"`
FeeDetails FeeDetails `json:"fee_details"`
Captured bool `json:"captured"`
BinaryMode bool `json:"binary_mode"`
CallForAuthorizeID interface{} `json:"call_for_authorize_id"`
StatementDescriptor string `json:"statement_descriptor"`
Installments int `json:"installments"`
Card Card `json:"card"`
NotificationURL string `json:"notification_url"`
Refunds []interface{} `json:"refunds"`
ProcessingMode string `json:"processing_mode"`
MerchantAccountID interface{} `json:"merchant_account_id"`
Acquirer interface{} `json:"acquirer"`
MerchantNumber interface{} `json:"merchant_number"`
AcquirerReconciliation []interface{} `json:"acquirer_reconciliation"`
}
type TransactionDetails struct {
PaymentMethodReferenceID interface{} `json:"payment_method_reference_id"`
NetReceivedAmount float64 `json:"net_received_amount"`
TotalPaidAmount float64 `json:"total_paid_amount"`
OverpaidAmount float64 `json:"overpaid_amount"`
ExternalResourceURL interface{} `json:"external_resource_url"`
InstallmentAmount float64 `json:"installment_amount"`
FinancialInstitution interface{} `json:"financial_institution"`
PayableDeferralPeriod interface{} `json:"payable_deferral_period"`
AcquirerReference interface{} `json:"acquirer_reference"`
}
type FeeDetails []FeeDetail
type FeeDetail struct {
Type string `json:"type"`
Amount float64 `json:"amount"`
FeePayer string `json:"fee_payer"`
}
// NewPayment creates a new Payment
func (c *client) NewPayment(params PaymentParams) (*Payment, error) {
u := fmt.Sprintf("%s/payments", c.baseURL)
body, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, u, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
var response Payment
err = c.requestAndDecode(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}