From 3cc347b1ca337299178990782b9b4282b4d5802d Mon Sep 17 00:00:00 2001 From: WhatACotton Date: Mon, 27 May 2024 00:19:51 +0900 Subject: [PATCH] fix: timing of reduce stock --- internal/cash/cash.go | 45 +++++++++++++++++++++++++++++++++++++++++ internal/cash/stripe.go | 34 +++++++++++++++++-------------- test/frontE2E_test.go | 8 +++++++- 3 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 internal/cash/cash.go diff --git a/internal/cash/cash.go b/internal/cash/cash.go new file mode 100644 index 0000000..e23438f --- /dev/null +++ b/internal/cash/cash.go @@ -0,0 +1,45 @@ +package cash + +import ( + "log" + + "github.com/stripe/stripe-go/v76" + "github.com/stripe/stripe-go/v76/checkout/session" +) + +func createCheckoutSession(amount int64) (StripeInfo, error) { + stripe.Key = "sk_test_51Nj1urA3bJzqElthGP4F3QjdR0SKk77E4pGHrsBAQEHia6lasXyujFOKXDyrodAxaE6PH6u2kNCVSdC5dBIRh82u00XqHQIZjM" + + params := &stripe.CheckoutSessionParams{ + Mode: stripe.String(string(stripe.CheckoutSessionModePayment)), + LineItems: []*stripe.CheckoutSessionLineItemParams{ + { + PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{ + Currency: stripe.String("jpy"), + ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{ + Name: stripe.String("購入金額"), + }, + UnitAmount: stripe.Int64(amount), + }, + Quantity: stripe.Int64(1), + }, + }, + SuccessURL: stripe.String("https://beta.charis.works/success"), + CancelURL: stripe.String("https://beta.charis.works/cancelled"), + } + s, _ := session.New(params) + log.Print(s.ID) + + stripe_info := StripeInfo{ + URL: s.URL, + AmountTotal: s.AmountTotal, + ID: s.ID, + } + return stripe_info, nil +} + +type StripeInfo struct { + URL string + AmountTotal int64 + ID string +} diff --git a/internal/cash/stripe.go b/internal/cash/stripe.go index 93c4e8c..c5126a6 100644 --- a/internal/cash/stripe.go +++ b/internal/cash/stripe.go @@ -11,7 +11,6 @@ import ( "github.com/stripe/stripe-go/v76/account" "github.com/stripe/stripe-go/v76/accountlink" "github.com/stripe/stripe-go/v76/loginlink" - "github.com/stripe/stripe-go/v76/paymentintent" "github.com/ttacon/libphonenumber" ) @@ -131,23 +130,28 @@ func GetAccount(stripeAccountId string) (*stripe.Account, error) { } func (r Requests) CreatePaymentintent(userId string, totalAmount int) (ClientSecret string, StripeTransactionId string, err error) { - stripe.Key = "sk_test_51Nj1urA3bJzqElthGP4F3QjdR0SKk77E4pGHrsBAQEHia6lasXyujFOKXDyrodAxaE6PH6u2kNCVSdC5dBIRh82u00XqHQIZjM" + // stripe.Key = "sk_test_51Nj1urA3bJzqElthGP4F3QjdR0SKk77E4pGHrsBAQEHia6lasXyujFOKXDyrodAxaE6PH6u2kNCVSdC5dBIRh82u00XqHQIZjM" - // Create a PaymentIntent with amount and currency - params := &stripe.PaymentIntentParams{ - Amount: stripe.Int64(int64(totalAmount)), //合計金額を算出する関数をインジェクト + // // Create a PaymentIntent with amount and currency + // params := &stripe.PaymentIntentParams{ + // Amount: stripe.Int64(int64(totalAmount)), //合計金額を算出する関数をインジェクト - Currency: stripe.String(string(stripe.CurrencyJPY)), - // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. - PaymentMethodTypes: []*string{stripe.String("card")}, - } + // Currency: stripe.String(string(stripe.CurrencyJPY)), + // // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. + // PaymentMethodTypes: []*string{stripe.String("card")}, + // } - pi, err := paymentintent.New(params) - log.Printf("pi.New: %v", pi.ClientSecret) + // pi, err := paymentintent.New(params) + // log.Printf("pi.New: %v", pi.ClientSecret) + // if err != nil { + // log.Printf("pi.New: %v", err) + // log.Print("Stripe Error: ", err) + // return ClientSecret, StripeTransactionId, &utils.InternalError{Message: utils.InternalErrorFromStripe} + // } + // return pi.ClientSecret, pi.ID, nil + info, err := createCheckoutSession(int64(totalAmount)) if err != nil { - log.Printf("pi.New: %v", err) - log.Print("Stripe Error: ", err) - return ClientSecret, StripeTransactionId, &utils.InternalError{Message: utils.InternalErrorFromStripe} + return "", "", err } - return pi.ClientSecret, pi.ID, nil + return info.URL, info.ID, nil } diff --git a/test/frontE2E_test.go b/test/frontE2E_test.go index 14bf243..363e26e 100644 --- a/test/frontE2E_test.go +++ b/test/frontE2E_test.go @@ -57,7 +57,6 @@ func TestE2E(t *testing.T) { }, }, } - //a for _, u := range user_data { if err = userRequests.Create(u.userId); err != nil { t.Errorf(err.Error()) @@ -312,7 +311,10 @@ func TestE2EforAfterPurchase(t *testing.T) { return } } + itemIds := new([]utils.Item) + db.Table("items").Where("1=1").Find(&itemIds) + log.Print(itemIds) clientSecret, transactionId, err := transactionRequests.Purchase("WQElviFCW3TEV77prNZB7Q2TwGt2", "cowatanabe26@gmail.com") if err != nil { t.Errorf(err.Error()) @@ -321,11 +323,15 @@ func TestE2EforAfterPurchase(t *testing.T) { } log.Print("clientSecret: ", clientSecret) log.Print("transactionId: ", transactionId) + db.Table("items").Where("1=1").Find(&itemIds) + log.Print(itemIds) transactionDetails, err := webhook.PurchaseComplete(transactionId) if err != nil { t.Errorf("got %v", err) After(t) } + db.Table("items").Where("1=1").Find(&itemIds) + log.Print(itemIds) opt := option.WithCredentialsFile("./serviceAccountKey.json") app, err := firebase.NewApp(context.Background(), nil, opt) if err != nil {