{"id":19706719,"url":"https://github.com/unisharp/pricing","last_synced_at":"2025-04-29T16:33:37.738Z","repository":{"id":57075598,"uuid":"125821659","full_name":"UniSharp/pricing","owner":"UniSharp","description":"A modularized pricing package for buyable.","archived":false,"fork":false,"pushed_at":"2019-09-06T16:23:27.000Z","size":18,"stargazers_count":4,"open_issues_count":1,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-05T18:12:29.020Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UniSharp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-19T07:57:18.000Z","updated_at":"2022-02-08T15:52:13.000Z","dependencies_parsed_at":"2022-08-24T14:55:44.572Z","dependency_job_id":null,"html_url":"https://github.com/UniSharp/pricing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Fpricing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Fpricing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Fpricing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UniSharp%2Fpricing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UniSharp","download_url":"https://codeload.github.com/UniSharp/pricing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251540542,"owners_count":21605927,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T21:36:41.061Z","updated_at":"2025-04-29T16:33:32.700Z","avatar_url":"https://github.com/UniSharp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pricing\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status][ico-travis]][link-travis]\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nA modularized pricing package for buyalbe.\n\n## Installation\n\n```\ncomposer require unisharp/pricing dev-master\n```\n\n## Configuration\n\n```\nphp artisan vendor:publish --tag pricing\n```\n\nSet available pricing modules in `config/pricing.php`\n\n```php\nreturn [\n    'modules' =\u003e [\n        UniSharp\\Pricing\\Tests\\Fixtures\\TestModule::class,\n    ]\n];\n```\n\n## Module Principles\n\n* Module must implement `UniSharp\\Pricing\\ModuleContract` which needs `handle` and `finish` functions.\n* Modules will be processed by the sequence in `config/pricing.php`, the first module will handle the pricing logic and pass pricing instance to the next module (Pipeline Pattern).\n* There are some APIs a pricing module can call in the handle function:\n    * $pricing-\u003eaddFee(int $fee);\n    * $pricing-\u003eaddDeduction(int $deduction);\n    * $pricing-\u003ewriteModuleLog(mix $log);\n    * $pricing-\u003egetModuleInfo();\n\n\u003e `addFee`, `addDeduction`, `writeModuleLog` will only change pricing instance's properties. `getModuleInfo` can get extra info of that module.\n\n* Finish functions will be called after pricing execute. The logic after module successfully applied can be implemented here.\n\n```php\nnamespace UniSharp\\Pricing\\Tests\\Fixtures;\n\nuse Closure;\nuse UniSharp\\Pricing\\Pricing;\nuse UniSharp\\Pricing\\ModuleContract;\nuse Illuminate\\Contracts\\Pipeline\\Pipeline;\n\nclass TestModule implements ModuleContract\n{\n    const FEE = 99;\n    const DEDUCTION = 88;\n    const LOG = 'log';\n\n    public function handle(Pricing $pricing, Closure $next)\n    {\n        $pricing-\u003eaddFee(static::FEE);\n        $pricing-\u003eaddDeduction(static::DEDUCTION);\n        $pricing-\u003ewriteModuleLog(static::LOG);\n\n        $info = $pricing-\u003egetModuleInfo();\n\n        return $next($pricing);\n    }\n\n    public function finish(Pricing $pricing)\n    {\n        //\n    }\n}\n```\n\n## Pricing Usages\n\n```php\nuse UniSharp\\Pricing\\Facades\\Pricing;\n\nclass Foo {\n    // set items and get original price\n    Pricing::setItems(UniSharp\\Cart\\CartItemCollection $items)\n        -\u003egetOriginalTotal();\n\n    // apply some modules\n    Pricing::apply(ModuleA::class)\n        -\u003eapply(ModuleB::class);\n    \n    // apply some modules with some extra info\n    Pricing::apply(ModuleA::class)\n        -\u003eapply(ModuleB::class)\n        -\u003ewith([\n            ModuleA::class =\u003e 'extra info A',\n            ModuleB::class =\u003e 'extra info B',\n        ]);\n    \n    // apply modules and get final total\n    Pricing::apply(ModuleA::class)\n        -\u003eapply(ModuleB::class)\n        -\u003egetTotal();\n\n    // apply modules and execute them\n    Pricing::apply(ModuleA::class)\n        -\u003eapply(ModuleB::class)\n        -\u003eexecute();\n    \n    // get all applied modules\n    Pricing::getAppliedModules();\n\n    // get all fees\n    Pricing::getFees();\n\n    // get fee of a specific module\n    Pricing::getFee(ModuleA::class);\n    \n    // get all deductions\n    Pricing::getDeductions();\n\n    // get deduction of a specific module\n    Pricing::getDeduction(ModuleA::class);\n\n    // get items\n    Pricing::getItems();\n}\n```\n\n[ico-version]: https://img.shields.io/packagist/v/UniSharp/pricing.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/UniSharp/pricing/master.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/UniSharp/pricing.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/UniSharp/pricing.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/UniSharp/pricing.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/unisharp/pricing\n[link-travis]: https://travis-ci.org/UniSharp/pricing\n[link-scrutinizer]: https://scrutinizer-ci.com/g/UniSharp/pricing/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/UniSharp/pricing\n[link-downloads]: https://packagist.org/packages/UniSharp/pricing\n[link-author]: https://github.com/UniSharp\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funisharp%2Fpricing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funisharp%2Fpricing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funisharp%2Fpricing/lists"}