mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
EmbedConfig.Ceiling is declared by the host as Born is: Test where the network holds no real money, "" or Live for no cap, and any other value caps at Test. Under a Test ceiling TestMode is true for every org, a record marked Live included, so it charges sandbox credentials, writes the test books and publishes the sandbox application. TestMode is the one reading of the mode: the ledger, balance, transaction, library, account and Authorize.Net paths, StripeToken and PayPal read it instead of org.Live, so the ceiling holds on each of them. Co-authored-by: Hanzo Dev <[email protected]>
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package authorizenet
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/models/order"
|
|
"github.com/hanzoai/commerce/models/organization"
|
|
"github.com/hanzoai/commerce/models/payment"
|
|
"github.com/hanzoai/commerce/models/user"
|
|
"github.com/hanzoai/commerce/thirdparty/authorizenet"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
)
|
|
|
|
var NothingToAuthorizeError = errors.New("Nothing to Authorize (Items or Subscriptions)")
|
|
|
|
func Authorize(org *organization.Organization, ord *order.Order, usr *user.User, pay *payment.Payment) error {
|
|
ctx := ord.Datastore().Context
|
|
|
|
// Create Authorize.Net client
|
|
con := org.AuthorizeNetToken(ord.Test)
|
|
|
|
log.Warn("Connection: %v", con, ctx)
|
|
log.Warn("Test?: %v", org.TestMode(), ctx)
|
|
|
|
loginId := con.LoginId
|
|
transactionKey := con.TransactionKey
|
|
key := con.Key
|
|
|
|
pay.Amount = ord.Total
|
|
|
|
client := authorizenet.New(ctx, loginId, transactionKey, key, org.TestMode())
|
|
|
|
if ord.Total > 0 {
|
|
// Do authorization
|
|
_, err := client.Authorize(pay)
|
|
if err != nil {
|
|
log.Error("Failed to authorize payment '%s'", pay.Id(), ctx)
|
|
log.JSON(pay)
|
|
return err
|
|
}
|
|
} else if len(ord.Subscriptions) == 0 {
|
|
return NothingToAuthorizeError
|
|
}
|
|
|
|
usr.Accounts.AuthorizeNet = pay.Account.AuthorizeNet
|
|
pay.Live = !org.TestMode()
|
|
|
|
now := time.Now()
|
|
|
|
// TODO: FIGURE THIS OUT LATER
|
|
for i, sub := range ord.Subscriptions {
|
|
log.Info("Before New Subscription %v\n %v", i, json.Encode(sub), ctx)
|
|
|
|
sub.PeriodStart = now
|
|
sub.Buyer = pay.Buyer
|
|
sub.Account = pay.Account
|
|
sub2, err := client.NewSubscription(&sub)
|
|
|
|
log.Info("After New Subscription %v\n %v", i, json.Encode(sub2), ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ord.Subscriptions[i] = *sub2
|
|
}
|
|
|
|
return nil
|
|
}
|