Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pay Laravel Package

yansongda/pay

A popular PHP payment library for Laravel and other frameworks, integrating Alipay and WeChat Pay with a clean API. Supports common payment flows, refunds, queries, callbacks/notifications, and flexible configuration for multi-app and sandbox use.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • High Compatibility with Laravel Ecosystem: The package is explicitly designed for Laravel (with a dedicated laravel-pay extension) and adheres to PSR standards (PSR-2, PSR-4, PSR-7, PSR-11, PSR-14, PSR-18), ensuring seamless integration with Laravel’s dependency injection, service container, and event systems.
  • Modular & Plugin-Based Design: The plugin pipeline architecture (StartPlugin → Business Plugins → ParserPlugin) allows for easy extension (e.g., adding custom payment gateways like PayPal, Stripe) without modifying core logic. This aligns well with Laravel’s modularity and service provider patterns.
  • Multi-Tenancy Support: Built-in multi-tenancy via Tenant abstraction, which can be leveraged in Laravel using middleware or service container bindings (e.g., Pay::tenant('tenant_id')).
  • Event-Driven: PSR-14 events enable integration with Laravel’s event system (e.g., pay.success, pay.failure), allowing for observability, analytics, or custom workflows (e.g., triggering order fulfillment).

Integration Feasibility

  • Laravel-Specific Extensions: The laravel-pay extension (linked in the README) provides Laravel-specific features like:
    • Service provider bootstrapping.
    • Facade support (Pay::alipay()PayFacade::alipay()).
    • Configuration via Laravel’s config/pay.php.
    • Middleware for tenant isolation or request validation.
  • Configuration Flexibility: Supports environment-based configs (e.g., .envconfig/pay.php), aligning with Laravel’s 12-factor principles.
  • HTTP Client Agnosticism: Uses Guzzle under the hood but can be swapped (e.g., for Laravel’s HTTP client or Symfony’s HTTP client) via dependency injection.

Technical Risk

  • Certificate Management:
    • Risk: WeChat’s dynamic public certificates require auto-refresh (handled by CertManager), but misconfiguration (e.g., incorrect paths or permissions) can break payments.
    • Mitigation: Use Laravel’s filesystem (storage/app/certs) and cache (Cache::remember) to store/refresh certificates.
  • Asynchronous Payments:
    • Risk: Webhook/notify callbacks must be idempotent and validated. Laravel’s queue system can help retry failed callbacks, but race conditions (e.g., duplicate notifications) require careful handling (e.g., Pay::wechat()->callback() with Pay::MODE_SERVICE for service accounts).
    • Mitigation: Implement Laravel’s queue:failed monitoring and dead-letter queues.
  • Multi-Provider Complexity:
    • Risk: Supporting Alipay, WeChat, UnionPay, etc., requires managing provider-specific quirks (e.g., Alipay’s trade_status vs. WeChat’s trade_state).
    • Mitigation: Use Laravel’s policy classes or middleware to standardize validation logic (e.g., validatePaymentStatus($provider, $data)).
  • Swoole Support:
    • Risk: Swoole coroutine support may conflict with Laravel’s synchronous request lifecycle (e.g., in web routes).
    • Mitigation: Restrict Swoole usage to Laravel Queues or console commands (e.g., async refunds).

Key Questions

  1. Provider Prioritization:
    • Which payment gateways (Alipay/WeChat/UnionPay/etc.) are critical for MVP? Prioritize integration based on market share and complexity.
  2. Tenant Strategy:
    • How will tenants be isolated? Options:
      • Database-level (e.g., tenant_id column in payments table).
      • Config-level (e.g., Pay::tenant('tenant_id')->config()).
      • Middleware (e.g., TenantMiddleware binding tenant to request).
  3. Webhook Security:
    • How will callback URLs be secured? Options:
      • Laravel’s signed routes for notify URLs.
      • IP whitelisting (e.g., trustedProxies middleware).
      • HMAC validation (e.g., Pay::wechat()->verifyWebhookSign()).
  4. Error Handling:
    • How will payment failures be logged/retried? Options:
      • Laravel’s Log::channel('payments') + queue:failed jobs.
      • Custom PaymentException class extending Laravel’s Exception.
  5. Testing:
    • How will sandbox/mock testing be implemented? Options:
      • Laravel’s Mockery for unit tests.
      • Sandbox mode (Pay::MODE_SANDBOX) for integration tests.
      • Factory patterns for generating test payment data.

Integration Approach

Stack Fit

  • Laravel Core:
    • Service Container: Bind Pay facade/service to the container with tenant-aware configurations.
    • Configuration: Publish and merge config/pay.php using Laravel’s publishes in a service provider.
    • Events: Dispatch custom events (e.g., PaymentProcessed, PaymentFailed) to integrate with Laravel’s event system.
  • HTTP Layer:
    • Middleware: Add VerifyPaymentCallback middleware to validate webhook signatures (e.g., for /alipay/notify).
    • Routing: Use named routes (e.g., pay.alipay.notify) for callback URLs to avoid hardcoding.
  • Database:
    • Migrations: Create payments table with fields like provider, out_trade_no, status, tenant_id, and metadata (JSON).
    • Models: Extend Laravel’s Model with Payment model for CRUD and status transitions.
  • Queue:
    • Jobs: Create ProcessPaymentJob and HandleWebhookJob for async operations (e.g., refunds, notifications).

Migration Path

  1. Phase 1: Core Integration (2-3 weeks)
    • Install yansongda/pay and yansongda/laravel-pay.
    • Configure config/pay.php for primary providers (e.g., Alipay, WeChat).
    • Implement PayServiceProvider to bind Pay facade and publish configs.
    • Create Payment model and migration.
  2. Phase 2: Webhooks & Callbacks (1-2 weeks)
    • Set up routes for /alipay/notify, /wechat/callback, etc.
    • Add middleware to validate signatures and dispatch events.
    • Implement PaymentListener to handle events (e.g., update Payment model on success).
  3. Phase 3: Multi-Tenancy (1 week)
    • Extend Pay config to support tenant-specific settings.
    • Add TenantMiddleware to bind tenant to request context.
  4. Phase 4: Testing & Observability (1 week)
    • Write unit tests for Payment model and service logic.
    • Set up Laravel Telescope for payment event tracking.
    • Implement sandbox testing for CI/CD.

Compatibility

  • Laravel Versions: Tested with Laravel 9+ (PHP 8.0+). Ensure compatibility with your Laravel version (e.g., use laravel/framework:^9.0 in Docker).
  • PHP Extensions: Requires openssl, curl, and dom (for XML parsing in Alipay/UnionPay).
  • Dependencies:
    • Guzzle: For HTTP requests (can be replaced with Laravel’s HTTP client).
    • Artful: For event dispatching (PSR-14 compatible).
    • Monolog: For logging (integrates with Laravel’s logging).

Sequencing

  1. Prerequisites:
    • Laravel project with PHP 8.0+ and Composer.
    • Database (MySQL/PostgreSQL) for payments table.
  2. Order of Implementation:
    • Step 1: Core setup (composer install, config, service provider).
    • Step 2: Basic payments (e.g., Alipay web payments).
    • Step 3: Webhook handling (async callbacks).
    • Step 4: Multi-tenancy and advanced features (e.g., refunds, transfers).
    • Step 5: Testing and monitoring.

Operational Impact

Maintenance

  • Configuration Management:
    • Use Laravel’s config/caching to avoid runtime config reloads.
    • Store sensitive keys (e.g., app_secret_cert) in .env or Laravel Vault.
  • Certificate Rotation:
    • Schedule Laravel tasks (scheduler:run) to refresh WeChat certificates via CertManager::reloadWechatPublicCerts().
    • Monitor certificate expiry dates (log warnings via Log::warning).
  • Logging:
    • Enable logger.enable: true in config and route logs to Laravel’s single or daily channels.
    • Use structured logging (e.g., JSON) for payment events.

Support

  • Troubleshooting:
    • Payment Failures: Check Laravel’s logs/payments.log and queue:failed jobs.
    • Webhook Issues: Verify callback URLs in provider dashboards (e.g., Alipay’s "Notify
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
directorytree/opensearch-client
directorytree/opensearch-adapter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata