Skip to content

Commit

Permalink
fix: timing of reduce stock
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatACotton committed May 26, 2024
1 parent 92c9011 commit 3cc347b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
45 changes: 45 additions & 0 deletions internal/cash/cash.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 19 additions & 15 deletions internal/cash/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion test/frontE2E_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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 {
Expand Down

0 comments on commit 3cc347b

Please sign in to comment.