Skip to content

Commit

Permalink
Merge pull request OpenBazaar#310 from cpacia/master
Browse files Browse the repository at this point in the history
Add mispayment buffer
  • Loading branch information
cpacia authored Jan 9, 2017
2 parents dc0060f + 33b769b commit 5e8e303
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,10 @@ func (i *jsonAPIHandler) POSTSettings(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusConflict, "Settings is already set. Use PUT.")
return
}
if settings.MisPaymentBuffer == nil {
i := float32(1)
settings.MisPaymentBuffer = &i
}
err = i.node.Datastore.Settings().Put(settings)
if err != nil {
ErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
16 changes: 16 additions & 0 deletions core/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,19 @@ func validateVersionNumber(rc *pb.RicardianContract) error {
}
return nil
}

func (n *OpenBazaarNode) ValidatePaymentAmount(requestedAmount, paymentAmount uint64) bool {
settings, err := n.Datastore.Settings().Get()
if err != nil {
return false
}
bufferPercent := float32(0)
if settings.MisPaymentBuffer != nil {
bufferPercent = *settings.MisPaymentBuffer
}
buffer := float32(requestedAmount) * (bufferPercent / 100)
if float32(paymentAmount)+buffer < float32(requestedAmount) {
return false
}
return true
}
4 changes: 2 additions & 2 deletions net/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (service *OpenBazaarService) handleOrder(peer peer.ID, pmes *pb.Message, op
if err != nil {
return errorResponse("Error calculating payment amount"), err
}
if total != contract.BuyerOrder.Payment.Amount {
if !service.node.ValidatePaymentAmount(total, contract.BuyerOrder.Payment.Amount) {
return errorResponse("Calculated a different payment amount"), err
}
contract, err = service.node.NewOrderConfirmation(contract, true)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (service *OpenBazaarService) handleOrder(peer peer.ID, pmes *pb.Message, op
if err != nil {
return errorResponse("Error calculating payment amount"), err
}
if total != contract.BuyerOrder.Payment.Amount {
if !service.node.ValidatePaymentAmount(total, contract.BuyerOrder.Payment.Amount) {
return errorResponse("Calculated a different payment amount"), err
}
err = service.node.ValidateModeratedPaymentAddress(contract.BuyerOrder)
Expand Down
3 changes: 3 additions & 0 deletions repo/db/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (s *SettingsDB) Update(settings repo.SettingsData) error {
if settings.StoreModerators == nil {
settings.StoreModerators = current.StoreModerators
}
if settings.MisPaymentBuffer == nil {
settings.MisPaymentBuffer = current.MisPaymentBuffer
}
if settings.SMTPSettings == nil {
settings.SMTPSettings = current.SMTPSettings
}
Expand Down
1 change: 1 addition & 0 deletions repo/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type SettingsData struct {
RefundPolicy *string `json:"refundPolicy"`
BlockedNodes *[]string `json:"blockedNodes"`
StoreModerators *[]string `json:"storeModerators"`
MisPaymentBuffer *float32 `json:"mispaymentBuffer"`
SMTPSettings *SMTPSettings `json:"smtpSettings"`
Version *string `json:"version"`
}
Expand Down

0 comments on commit 5e8e303

Please sign in to comment.