feat: add decline code categorization and Stripe error integration#7
Conversation
## New Features ### Decline Code Categorization - Add `getDeclineCategory()` to classify codes as SOFT_DECLINE or HARD_DECLINE - Add `isHardDecline()` helper to identify permanent declines - Add `isSoftDecline()` helper to identify temporary declines - Based on official Stripe documentation guidelines ### Stripe Error Object Support - Add `getMessageFromStripeError()` for direct error object handling - Simplifies integration with Stripe SDK error handling ## Implementation Details - Added `category` field to all 44 decline codes in database - Classification based on Stripe's soft/hard decline guidelines - Added `DeclineCategory` and `StripeError` types - Comprehensive test coverage with 34 passing tests - Updated documentation with usage examples ## Testing - All tests passing (34/34) - TDD approach: Red-Green-Refactor cycle - Type-safe implementation with full TypeScript support
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughSummary by CodeRabbitリリースノート
✏️ Tip: You can customize this high-level summary in your review settings. WalkthroughStripe拒否コードの分類とエラーメッセージ抽出機能を追加するPRです。拒否コードをSOFT_DECLINEとHARD_DECLINEに分類する4つの新規パブリックAPI関数を導入し、全拒否コードデータに分類カテゴリフィールドを追加しました。 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
Summary of ChangesHello @hideokamoto, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces excellent new features for categorizing Stripe decline codes into soft and hard declines, and for easily extracting messages from Stripe error objects. The implementation is clean, well-tested, and properly documented. I have one suggestion to improve the type safety of the example code in the README to better align with modern TypeScript practices.
README.md
Outdated
| } catch (error) { | ||
| if (error.type === 'StripeCardError') { | ||
| // Get localized message directly from error object | ||
| const userMessage = getMessageFromStripeError(error, 'ja'); | ||
|
|
||
| // Check if retry is recommended | ||
| if (error.decline_code && isHardDecline(error.decline_code)) { | ||
| // Ask for a different payment method | ||
| console.log('Please use a different card'); | ||
| } else { | ||
| // Retry is possible | ||
| console.log('You may retry this payment'); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The example for handling Stripe errors in the catch block could be improved to demonstrate safer TypeScript practices. In modern TypeScript (with useUnknownInCatchVariables: true), the error variable in a catch block is of type unknown. Accessing its properties directly (e.g., error.type) is unsafe and will cause a type error. The example should show a safe way to handle this, for instance by using a type assertion.
This also requires importing the StripeError type:
import { getMessageFromStripeError, isHardDecline, StripeError } from 'stripe-decline-codes';| } catch (error) { | |
| if (error.type === 'StripeCardError') { | |
| // Get localized message directly from error object | |
| const userMessage = getMessageFromStripeError(error, 'ja'); | |
| // Check if retry is recommended | |
| if (error.decline_code && isHardDecline(error.decline_code)) { | |
| // Ask for a different payment method | |
| console.log('Please use a different card'); | |
| } else { | |
| // Retry is possible | |
| console.log('You may retry this payment'); | |
| } | |
| } | |
| } | |
| } catch (rawError) { | |
| // In modern TypeScript, the catch variable is `unknown`. It's safer to cast it. | |
| const error = rawError as StripeError; | |
| if (error.type === 'StripeCardError') { | |
| // Get localized message directly from error object | |
| const userMessage = getMessageFromStripeError(error, 'ja'); | |
| // Check if retry is recommended | |
| if (error.decline_code && isHardDecline(error.decline_code)) { | |
| // Ask for a different payment method | |
| console.log('Please use a different card'); | |
| } else { | |
| // Retry is possible | |
| console.log('You may retry this payment'); | |
| } | |
| } | |
| } |
|
@coderabbitai resolve |
✅ Actions performedComments resolved. |
…-updates-01Szcu7sR6UL6yCmcQTMEDtN ## Merge Details - Resolved test file location conflict (tests/ → src/) - Integrated new categorization and Stripe error tests into src/index.test.ts - Fixed TypeScript type safety in README examples (catch block error handling) - All 66 tests passing (32 existing + 34 new) ## Changes Applied - Moved tests from tests/index.test.ts to src/index.test.ts - Added comprehensive property-based tests for new features - Updated README examples to use proper type assertions for Stripe errors - Fixed error.type check to use 'card_error' instead of 'StripeCardError'
New Features
Decline Code Categorization
getDeclineCategory()to classify codes as SOFT_DECLINE or HARD_DECLINEisHardDecline()helper to identify permanent declinesisSoftDecline()helper to identify temporary declinesStripe Error Object Support
getMessageFromStripeError()for direct error object handlingImplementation Details
categoryfield to all 44 decline codes in databaseDeclineCategoryandStripeErrortypesTesting