> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Unit Tests to Your Payments Service

export const UseCaseHero = ({title, description, prompt, category, features, devinUrl, agent, intent, playbookId, type}) => {
  const encodedPrompt = encodeURIComponent(prompt || '');
  const tag = 'docs-use-case-gallery';
  const utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=hero-cta';
  const agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  const devinHref = type === 'schedule' ? 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + (prompt ? '&prompt=' + encodedPrompt : '') : type === 'review' ? 'https://app.devin.ai/review?' + utm : agent === 'ada' ? 'https://app.devin.ai/search?' + utm + '&noSubmit=true' + (prompt ? '&prompt=' + encodedPrompt : '') : devinUrl ? devinUrl.includes('?') ? devinUrl + '&' + utm + agentParams : devinUrl + '?' + utm + agentParams : prompt ? 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encodedPrompt : 'https://app.devin.ai/?' + utm + agentParams;
  const buttonLabel = type === 'schedule' ? 'Schedule in Devin ↗' : type === 'review' ? 'Set Up Devin Review ↗' : agent === 'advanced' ? 'Try in Devin ↗' : agent === 'dana' ? 'Try in Dana ↗' : agent === 'ada' ? 'Try in Ask Devin ↗' : 'Try in Devin ↗';
  const featureList = features ? features.split(',').map(f => f.trim()) : [];
  return <div className="uc-hero">
      <div className="uc-hero-inner">
        <div className="uc-hero-left">
          <h1 className="uc-hero-title">{title}</h1>
          <p className="uc-hero-desc">{description}</p>
          <div>
            <a href={devinHref} target="_blank" rel="noopener noreferrer" className="try-in-devin-btn">
              {buttonLabel}
            </a>
          </div>
        </div>
        <div className="uc-hero-meta">
          <div className="uc-meta-item">
            <span className="uc-meta-label">Author</span>
            <span className="uc-meta-value">Cognition</span>
          </div>
          <div className="uc-meta-item">
            <span className="uc-meta-label">Category</span>
            <span className="uc-meta-value">{category}</span>
          </div>
          {featureList.length > 0 && <div className="uc-meta-item">
              <span className="uc-meta-label">Features</span>
              <span className="uc-meta-value">{featureList.join(', ')}</span>
            </div>}
        </div>
      </div>
    </div>;
};

export const PromptBlock = ({children, type, agent, intent, playbookId}) => {
  var utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=prompt-block';
  var tag = 'docs-use-case-gallery';
  var agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  var label = type === 'schedule' ? 'Schedule in Devin' : type === 'playbook' ? 'Create Playbook' : type === 'knowledge' ? 'Add to Knowledge' : agent === 'advanced' ? 'Try in Devin' : agent === 'dana' ? 'Try in Dana' : agent === 'ada' ? 'Try in Ask Devin' : 'Try in Devin';
  var buildUrl = function (text) {
    var encoded = encodeURIComponent(text);
    if (type === 'schedule') return 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + '&prompt=' + encoded;
    if (type === 'playbook') return 'https://app.devin.ai/settings/playbooks/create?' + utm + '&body=' + encoded;
    if (type === 'knowledge') return 'https://app.devin.ai/knowledge?' + utm + '&body=' + encoded;
    if (agent === 'ada') return 'https://app.devin.ai/search?' + utm + '&noSubmit=true&prompt=' + encoded;
    return 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encoded;
  };
  const ref = React.useRef(null);
  const [href, setHref] = React.useState('#');
  React.useEffect(() => {
    if (!ref.current) return;
    var codeEl = ref.current.querySelector('pre code');
    if (codeEl) {
      var text = codeEl.textContent.trim();
      if (text) setHref(buildUrl(text));
    }
    var header = ref.current.querySelector('[data-component-part="code-block-header"]');
    if (header && !header.querySelector('.prompt-block-devin-link')) {
      var link = document.createElement('a');
      link.href = href;
      link.target = '_blank';
      link.rel = 'noopener noreferrer';
      link.className = 'prompt-block-devin-link';
      link.style.cssText = 'display:inline-flex;align-items:center;gap:6px;text-decoration:none;color:#fff;font-size:11px;font-weight:500;padding:4px 10px;border-radius:6px;white-space:nowrap;background:#317CFF;transition:background 0.2s;margin-left:8px;';
      link.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg> ' + label;
      link.onmouseenter = function () {
        link.style.background = '#2968D9';
      };
      link.onmouseleave = function () {
        link.style.background = '#317CFF';
      };
      header.appendChild(link);
    }
    var existingLink = ref.current.querySelector('.prompt-block-devin-link');
    if (existingLink && href !== '#') existingLink.href = href;
  });
  return <div className="prompt-block" ref={ref}>{children}</div>;
};

<UseCaseHero title="Add Unit Tests to Your Payments Service" description="Build a test-writing playbook for payment processing and have Devin cover charge flows, refund logic, and webhook handlers with comprehensive unit tests." prompt="Write unit tests for all untested functions in src/services/PaymentService.ts. Cover processCharge, issueRefund, and handleWebhook. Mock the Stripe SDK and database calls. Each function needs tests for success, error, and edge cases like duplicate charges and partial refunds. Target 90% line coverage." category="Code Quality" features="Playbooks" />

<div className="uc-detail-wrapper">
  <Tip>Don't want to set this up manually? Paste a link to this page into a Devin session and ask it to set everything up for you.</Tip>

  <Steps>
    <Step title="Write a payments-specific test playbook">
      A [playbook](/product-guides/creating-playbooks) encodes your team's testing conventions so Devin writes tests the way your engineers do. Payment code has unique concerns — idempotency, currency precision, gateway retries, PCI-safe mocking — so a payments-focused playbook catches issues a generic one would miss.

      **Option 1: Write the playbook yourself.** Go to [**Settings > Playbooks > Create playbook**](https://app.devin.ai/settings/playbooks/create?utm_source=docs\&utm_medium=use-case-gallery) and define your standards:

      <PromptBlock type="playbook">
        ```txt Payments Test Playbook theme={null}
        ## Procedure
        1. Read the target payment module and identify all exported functions
        2. Study existing test files in src/services/__tests__/ for patterns
        3. Create a test file named {Module}.test.ts in __tests__/ next to the source
        4. Write tests using the AAA pattern (Arrange, Act, Assert)
        5. Mock the Stripe SDK with jest.mock('stripe') — never call real payment APIs
        6. Mock database calls with the helpers from src/test/helpers.ts
        7. Use integer cents for all currency values — never floating-point dollars
        8. Test idempotency: calling processCharge twice with the same idempotency key must not create duplicate charges
        9. Cover success paths, error paths, and edge cases for every function
        10. Run `npm test -- --coverage` and verify 90%+ line coverage for the file

        ## Specifications
        - Framework: Jest with TypeScript
        - Mocking: jest.mock() for Stripe SDK, jest.spyOn() for internal methods
        - Currency: Always use integer cents (e.g., 2999 not 29.99)
        - Assertions: Prefer specific matchers (toHaveBeenCalledWith, toThrow)
        - Naming: describe('PaymentService') > describe('processCharge') > it('charges a valid card')

        ## Forbidden Actions
        - Do not call real Stripe, PayPal, or any live payment gateway in tests
        - Do not use floating-point arithmetic for currency calculations
        - Do not modify the source module to make it easier to test
        - Do not skip idempotency or retry-logic edge cases
        ```
      </PromptBlock>

      **Option 2: Let Devin create the playbook for you.** Describe your testing conventions and Devin will generate a complete playbook:

      <PromptBlock agent="advanced">
        ```txt Generate a payments test playbook theme={null}
        Create a playbook for writing unit tests for our payment modules in
        src/services/. We use Jest with TypeScript, mock the Stripe SDK with
        jest.mock('stripe'), and use integer cents for all currency values.
        Look at our existing tests in src/services/__tests__/ to learn our
        patterns (AAA style, specific matchers, describe/it naming). The
        playbook should cover mocking strategy, idempotency testing, currency
        precision rules, and forbidden actions like calling real payment APIs.
        ```
      </PromptBlock>

      Then add your best existing test file (e.g., `src/services/__tests__/UserService.test.ts`) as a [Knowledge](/product-guides/knowledge) entry so Devin has a concrete example of your team's style.
    </Step>

    <Step title="Identify untested payment code">
      Before pointing Devin at specific files, find where the gaps are in your payment modules. Ask Devin to run your coverage tool and surface the worst offenders:

      <PromptBlock>
        ```txt Analyze payment module coverage theme={null}
        Run npm test -- --coverage and list every file under src/services/
        that handles payments, billing, or subscriptions with less than 80%
        line coverage. Sort by coverage ascending so I can see the biggest
        gaps first.
        ```
      </PromptBlock>

      Devin runs the suite in its terminal, parses the coverage report, and gives you a prioritized list:

      ```
      File                                | Lines | Uncovered functions
      ------------------------------------|-------|------------------------------
      src/services/PaymentService.ts      |  34%  | processCharge, issueRefund, handleWebhook
      src/services/SubscriptionService.ts |  41%  | renewSubscription, cancelTrial, proratePlan
      src/services/InvoiceService.ts      |  52%  | generateInvoice, applyPromoCode, calculateTax
      src/services/PayoutService.ts       |  58%  | initiateTransfer, reconcileSettlement
      ```
    </Step>

    <Step title="Have Devin write tests for PaymentService">
      Start a new session, attach your payments test playbook (you'll see a blue pill confirming it's attached), and tell Devin which module to cover:

      <PromptBlock>
        ```txt Write tests for PaymentService theme={null}
        Write unit tests for all untested functions in
        src/services/PaymentService.ts. Focus on processCharge,
        issueRefund, and handleWebhook. Each function needs tests for
        success, error, and edge cases. Mock the Stripe SDK — never call
        real APIs. Use integer cents for all currency values. Run the
        tests and verify 90%+ line coverage for this file.
        ```
      </PromptBlock>

      Devin reads the module, studies your existing tests for patterns, writes a comprehensive test file following your playbook, and runs it:

      ```
      PASS src/services/__tests__/PaymentService.test.ts
        PaymentService
          processCharge
            ✓ charges a valid credit card and returns a receipt (14ms)
            ✓ uses integer cents to avoid floating-point errors (6ms)
            ✓ rejects duplicate charges with the same idempotency key (5ms)
            ✓ retries on Stripe gateway timeout up to 3 times (18ms)
            ✓ throws InsufficientFundsError for declined cards (4ms)
          issueRefund
            ✓ refunds full amount for a completed charge (8ms)
            ✓ refunds partial amount in cents when specified (6ms)
            ✓ prevents refund exceeding the original charge amount (3ms)
            ✓ throws AlreadyRefundedError on duplicate refund attempts (4ms)
          handleWebhook
            ✓ processes charge.succeeded events and updates order status (7ms)
            ✓ processes charge.refunded events and credits the customer (6ms)
            ✓ verifies Stripe webhook signature before processing (3ms)
            ✓ ignores unrecognized event types without error (2ms)

      Coverage: 94% lines | 91% branches | 100% functions
      ```

      Devin opens a PR with the test file and a coverage summary in the description.
    </Step>

    <Step title="Work through the remaining payment modules">
      Review the first PR. If the mock strategy or assertion style isn't quite right, update your playbook before running it on more modules — one round of feedback saves you from correcting the same issue across multiple PRs.

      Then work down your gap list:

      <PromptBlock>
        ```txt Cover SubscriptionService theme={null}
        Write tests for src/services/SubscriptionService.ts using the same
        playbook. Focus on renewSubscription, cancelTrial, and proratePlan.
        Test proration math using integer cents. Target 90%+ line coverage.
        ```
      </PromptBlock>

      To go faster, ask Devin to launch parallel sessions — one per payment module — all following the same playbook. Or [schedule](/product-guides/scheduled-sessions) a weekly session that finds any payment modules that have dropped below your coverage threshold and writes tests for them automatically.
    </Step>
  </Steps>
</div>
