本文整理汇总了Golang中github.com/stripe/stripe-go/charge.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRefundGet
func TestRefundGet(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "378282246310005",
Month: "06",
Year: "20",
},
},
}
ch, err := charge.New(chargeParams)
if err != nil {
t.Error(err)
}
ref, err := New(&stripe.RefundParams{Charge: ch.ID})
if err != nil {
t.Error(err)
}
target, err := Get(ref.ID, &stripe.RefundParams{Charge: ch.ID})
if err != nil {
t.Error(err)
}
if target.Charge != ch.ID {
t.Errorf("Refund charge %q does not match expected value %v\n", target.Charge, ch.ID)
}
}
示例2: chargeAccount
func chargeAccount(ctx context.Context, stripeToken string) error {
// because we're on app engine, use a custom http client
// this is being set globally, however
// if we wanted to do it this way, we'd have to use a lock
// https://youtu.be/KT4ki_ClX2A?t=1018
hc := urlfetch.Client(ctx)
stripe.SetHTTPClient(hc)
id, _ := uuid.NewV4()
for {
chargeParams := &stripe.ChargeParams{
Amount: 100 * 200000,
Currency: "usd",
Desc: "Charge for [email protected]",
}
chargeParams.IdempotencyKey = id.String()
chargeParams.SetSource(stripeToken)
ch, err := charge.New(chargeParams)
// https://youtu.be/KT4ki_ClX2A?t=1310
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(time.Second)
continue
}
return err
}
log.Infof(ctx, "CHARGE: %v", ch)
log.Infof(ctx, "IDEMPOTENCY: %v", chargeParams.IdempotencyKey)
return nil
}
}
示例3: newDisputedCharge
func newDisputedCharge() (*stripe.Charge, error) {
chargeParams := &stripe.ChargeParams{
Amount: 1001,
Currency: currency.USD,
}
chargeParams.SetSource(&stripe.CardParams{
Number: "4000000000000259",
Month: "06",
Year: "20",
})
res, err := charge.New(chargeParams)
if err != nil {
return nil, err
}
target, err := charge.Get(res.ID, nil)
if err != nil {
return target, err
}
for target.Dispute == nil {
time.Sleep(time.Second * 10)
target, err = charge.Get(res.ID, nil)
if err != nil {
return target, err
}
}
return target, err
}
示例4: buyPrint
func buyPrint(responseWriter http.ResponseWriter, request *http.Request, requestParameters httprouter.Params) {
successMessage := validBuyPrintPostVariables(request)
if !successMessage.Success {
json.NewEncoder(responseWriter).Encode(successMessage)
return
}
buyPrintToken := request.PostFormValue("buy-print-token")
costInCents := printCostInCents[request.PostFormValue("print-size")]
if credentials.LiveOrDev == "live" {
stripe.Key = credentials.StripeLiveSecretKey
} else {
stripe.Key = credentials.StripeTestSecretKey
}
chargeParams := &stripe.ChargeParams{
Amount: costInCents,
Currency: "usd",
Desc: request.PostFormValue("shipping-email") + " " + request.PostFormValue("destination-link"),
}
chargeParams.SetSource(buyPrintToken)
chargeResults, error := charge.New(chargeParams)
if error != nil {
successMessage.SetMessage(false, error.Error())
json.NewEncoder(responseWriter).Encode(successMessage)
return
}
json.NewEncoder(responseWriter).Encode(successMessage)
sendBuyPrintSuccessEmail(request, chargeResults.ID)
}
示例5: TestTransferList
func TestTransferList(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
charge.New(chargeParams)
recipientParams := &stripe.RecipientParams{
Name: "Recipient Name",
Type: recipient.Individual,
Card: &stripe.CardParams{
Name: "Test Debit",
Number: "4000056655665556",
Month: "10",
Year: "20",
},
}
rec, _ := recipient.New(recipientParams)
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Recipient: rec.ID,
}
for i := 0; i < 5; i++ {
New(transferParams)
}
i := List(&stripe.TransferListParams{Recipient: rec.ID})
for i.Next() {
if i.Transfer() == nil {
t.Error("No nil values expected")
}
if i.Meta() == nil {
t.Error("No metadata returned")
}
}
if err := i.Err(); err != nil {
t.Error(err)
}
recipient.Del(rec.ID)
}
示例6: TestTransferUpdate
func TestTransferUpdate(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
charge.New(chargeParams)
recipientParams := &stripe.RecipientParams{
Name: "Recipient Name",
Type: recipient.Corp,
Bank: &stripe.BankAccountParams{
Country: "US",
Routing: "110000000",
Account: "000123456789",
},
}
rec, _ := recipient.New(recipientParams)
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Recipient: rec.ID,
Desc: "Original",
}
trans, _ := New(transferParams)
updated := &stripe.TransferParams{
Desc: "Updated",
}
target, err := Update(trans.ID, updated)
if err != nil {
t.Error(err)
}
if target.Desc != updated.Desc {
t.Errorf("Description %q does not match expected description %q\n", target.Desc, updated.Desc)
}
recipient.Del(rec.ID)
}
示例7: TestTransferGet
func TestTransferGet(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
charge.New(chargeParams)
recipientParams := &stripe.RecipientParams{
Name: "Recipient Name",
Type: recipient.Individual,
Card: &stripe.CardParams{
Name: "Test Debit",
Number: "4000056655665556",
Month: "10",
Year: "20",
},
}
rec, _ := recipient.New(recipientParams)
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Recipient: rec.ID,
}
trans, _ := New(transferParams)
target, err := Get(trans.ID, nil)
if err != nil {
t.Error(err)
}
if target.Card == nil {
t.Errorf("Card is not set\n")
}
if target.Type != Card {
t.Errorf("Unexpected type %q\n", target.Type)
}
recipient.Del(rec.ID)
}
示例8: TestTransferSourceType
func TestTransferSourceType(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
_, err := charge.New(chargeParams)
if err != nil {
t.Error(err)
}
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Dest: "default_for_currency",
SourceType: SourceCard,
}
target, err := New(transferParams)
if err != nil {
t.Error(err)
}
if target.Amount != transferParams.Amount {
t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, transferParams.Amount)
}
if target.Currency != transferParams.Currency {
t.Errorf("Curency %q does not match expected currency %q\n", target.Currency, transferParams.Currency)
}
if target.Created == 0 {
t.Errorf("Created date is not set\n")
}
if target.Date == 0 {
t.Errorf("Date is not set\n")
}
if target.Type != Bank {
t.Errorf("Unexpected type %q\n", target.Type)
}
}
示例9: chargeAccount
func chargeAccount(ctx context.Context, stripeToken string) error {
chargeParams := &stripe.ChargeParams{
Amount: 100 * 200000,
Currency: "usd",
Desc: "Charge for [email protected]",
}
chargeParams.SetSource(stripeToken)
ch, err := charge.New(chargeParams)
if err != nil {
return err
}
log.Infof(ctx, "CHARGE: %v", ch)
return nil
}
示例10: TestRefundListByCharge
func TestRefundListByCharge(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "378282246310005",
Month: "06",
Year: "20",
},
},
}
ch, err := charge.New(chargeParams)
if err != nil {
t.Error(err)
}
for i := 0; i < 4; i++ {
_, err = New(&stripe.RefundParams{Charge: ch.ID, Amount: 200})
if err != nil {
t.Error(err)
}
}
listParams := &stripe.RefundListParams{}
listParams.Filters.AddFilter("charge", "", ch.ID)
i := List(listParams)
for i.Next() {
target := i.Refund()
if target.Amount != 200 {
t.Errorf("Amount %v does not match expected value\n", target.Amount)
}
if target.Charge != ch.ID {
t.Errorf("Refund charge %q does not match expected value %q\n", target.Charge, ch.ID)
}
if i.Meta() == nil {
t.Error("No metadata returned")
}
}
if err := i.Err(); err != nil {
t.Error(err)
}
}
示例11: TestReversalGet
func TestReversalGet(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
charge.New(chargeParams)
recipientParams := &stripe.RecipientParams{
Name: "Recipient Name",
Type: recipient.Individual,
Bank: &stripe.BankAccountParams{
Country: "US",
Routing: "110000000",
Account: "000123456789",
},
}
rec, _ := recipient.New(recipientParams)
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Recipient: rec.ID,
Desc: "Transfer Desc",
Statement: "Transfer",
}
trans, _ := transfer.New(transferParams)
rev, _ := New(&stripe.ReversalParams{Transfer: trans.ID})
target, err := Get(rev.ID, &stripe.ReversalParams{Transfer: trans.ID})
if err != nil {
// TODO: replace this with t.Error when this can be tested
fmt.Println(err)
}
fmt.Printf("%+v\n", target)
}
示例12: ExampleCharge_new
func ExampleCharge_new() {
stripe.Key = "sk_key"
params := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
}
params.SetSource(&stripe.CardParams{
Name: "Go Stripe",
Number: "4242424242424242",
Month: "10",
Year: "20",
})
params.AddMeta("key", "value")
ch, err := charge.New(params)
if err != nil {
log.Fatal(err)
}
log.Printf("%v\n", ch.ID)
}
示例13: createDebit
func createDebit(token string, amount uint64, description string) *stripe.Charge {
// get your secret test key from
// https://dashboard.stripe.com/account/apikeys and place it here
stripe.Key = "sk_test_goes_here"
params := &stripe.ChargeParams{
Amount: amount,
Currency: currency.USD,
Desc: "test charge",
}
// obtain token from stripe
params.SetSource(token)
ch, err := charge.New(params)
if err != nil {
log.Fatalf("error while trying to charge a cc", err)
}
log.Printf("debit created successfully %v\n", ch.ID)
return ch
}
示例14: TestTransferToAccount
func TestTransferToAccount(t *testing.T) {
chargeParams := &stripe.ChargeParams{
Amount: 1000,
Currency: currency.USD,
Source: &stripe.SourceParams{
Card: &stripe.CardParams{
Number: "4000000000000077",
Month: "06",
Year: "20",
},
},
}
charge, err := charge.New(chargeParams)
if err != nil {
t.Error(err)
}
params := &stripe.AccountParams{
Managed: true,
Country: "US",
LegalEntity: &stripe.LegalEntity{
Type: stripe.Individual,
DOB: stripe.DOB{
Day: 1,
Month: 2,
Year: 1990,
},
},
}
acc, err := account.New(params)
if err != nil {
t.Error(err)
}
transferParams := &stripe.TransferParams{
Amount: 100,
Currency: currency.USD,
Dest: acc.ID,
SourceTx: charge.ID,
}
target, err := New(transferParams)
if err != nil {
t.Error(err)
}
if target.Amount != transferParams.Amount {
t.Errorf("Amount %v does not match expected amount %v\n", target.Amount, transferParams.Amount)
}
if target.Currency != transferParams.Currency {
t.Errorf("Curency %q does not match expected currency %q\n", target.Currency, transferParams.Currency)
}
if target.Created == 0 {
t.Errorf("Created date is not set\n")
}
if target.Date == 0 {
t.Errorf("Date is not set\n")
}
if target.SourceTx != transferParams.SourceTx {
t.Errorf("SourceTx %q does not match expected SourceTx %q\n", target.SourceTx, transferParams.SourceTx)
}
if target.Type != StripeAccount {
t.Errorf("Unexpected type %q\n", target.Type)
}
account.Del(acc.ID)
}
示例15: HandleConfirmation
func (s *Server) HandleConfirmation(w http.ResponseWriter, r *http.Request) {
// POST /reservations/confirmation
if r.Method != "POST" {
http.Error(w, "Method must be POST", http.StatusBadRequest)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var vars ConfirmationVars
if err := s.decoder.Decode(&vars, r.PostForm); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Look up requested tours.
var tourIDs []int32
for _, item := range vars.Items {
tourIDs = append(tourIDs, item.TourID)
}
tourDetails, err := s.store.GetTourDetailsByID(tourIDs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Compute total.
var total float64
for _, item := range vars.Items {
tourDetail, ok := tourDetails[item.TourID]
if !ok {
http.Error(w, fmt.Sprintf("Invalid tour ID: %d", item.TourID), http.StatusBadRequest)
return
}
total += float64(item.Quantity) * tourDetail.Price
}
// Convert float64(13.57) to uint64(1357).
stripeAmount := uint64(total*100 + 0.5)
// Add order to database.
orderID, err := s.store.CreateOrder(vars.Name, vars.Email, vars.Hotel, vars.Mobile, vars.Items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Charge to Stripe.
stripe.Key = s.stripeSecretKey
ch, err := charge.New(&stripe.ChargeParams{
Amount: stripeAmount,
Currency: "USD",
Source: &stripe.SourceParams{Token: vars.StripeToken},
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Update order in database to record payment.
if err := s.store.UpdateOrderPaymentRecorded(orderID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Email the customer.
if err := s.emailCustomer(&vars); err != nil {
log.Print(err)
}
// Render confirmation page.
data := &ConfirmationData{
Charge: ch,
DisplayAmount: fmt.Sprintf("%d.%02d", ch.Amount/100, ch.Amount%100),
}
tmpl, err := template.ParseFiles(path.Join(s.templatesDir, "confirmation.html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}