剪裁 / 购买
多店铺市场核心组件
Requires
- clippings/transfer: ~0.2.0
- harp-orm/locations: ~0.3.0
- harp-orm/materialized-path: ~0.3.0
- harp-orm/random-key: ~0.3.0
- harp-orm/timestamps: ~0.3.0
Requires (Dev)
- omnipay/dummy: ~2.0
This package is not auto-updated.
Last update: 2020-01-20 04:17:04 UTC
README
这是一个 Kohana 模块,提供开箱即用的多店铺购买功能(每个购买可能包含来自不同卖家的商品,每个卖家独立处理其产品部分)
它使用 omnipay 软件包
使用方法
提供的模型直接可用。您还将有 "Product" 和 "Store" 模型,您应该使用并扩展它们。
// Initialize some products and stores. $store1 = new Store(['name' => 'My First Store']); $product1 = new Product(['name' => 'My First Product', 'currency' => 'GBP', 'value' => 2000]); $product2 = new Product(['name' => 'My Second Product', 'currency' => 'EUR', 'value' => 1000]); $store1->getProducts() ->add($product1) ->add($product2); $store2 = new Store(['name' => 'My Second Store']); $product3 = new Product(['name' => 'My Third Product', 'currency' => 'GBP', 'value' => 5000]); $store2->getProducts() ->add($product3); Store::saveArray([$store1, $store2]); // Now we can purchase something $purchase = new Purchase(); $purchase ->addProduct($product1) ->addProduct($product2) ->addProduct($product3, 2); // Freeze all the values of the order, // so that it remains "frozen", even if prices of products change $purchase->freeze(); // Initialize omnipay getway - here you will have Paypal, Stripe etc. $gateway = Omnipay::getFactory()->create('Dummy'); $parameters = [ 'card' => [ 'number' => '4242424242424242', 'expiryMonth' => 7, 'expiryYear' => 2014, 'cvv' => 123, ], 'clientIp' => '192.168.0.1', ]; $response = $purchase->purchase($gateway, $parameters); echo $response->isSuccessful();
模型图
┌─────────┐ ┌──────────┐ ┌───────┐
│ Address ├──→│ Purchase │ │ Store │
└─────────┘ └─┬─────┬──┘ └─┬─────┘
│ ↓ ↓
│ ┌───────────────┐
│ │ StorePurchase │
│ └───────┬───────┘
│ ↓
│ ┌──────────────┐
└────→│ PurchaseItem │
┌─────────┐ ├──────────────┤
│ Product ├──────────→│ ProductItem │
└─────────┘ └──────────────┘
购买由 "PurchaseItems" 组成,这是一个模型,它包含用户购买的所有商品。ProductItem 扩展 PurchaseItem 以使它们共享相同的表,使用 InheritedTrait。
然而,每个 PurchaseItem 也属于一个 "StorePurchase" 模型,它表示特定商店的购买。当您通过 "addProduct" 方法或 addPurchaseItem 添加产品时,此关系将自动管理。
向购买中添加项目
$purchase = new Purchase(); // Adding a product $purchase->addProduct($product); // Adding multiple products $purchase->addProduct($product, 5);
由于产品已属于特定商店,这将为此商店创建一个特定的 StorePurchase(如果尚不存在),否则它将向现有的 StorePurchase 添加项目。如果此产品已以 ProductItem 的形式存在,则将增加数量。
$purchase = new Purchase(); // Adding a purchaseItem $purchase->addPurchaseItem($store, $purchaseItem);
如果您需要添加其他类型的购买项目,该项目与商店没有固有的关系,您可以使用 addPurchaseItem 方法。它将添加任何类型的 PurchaseItem。
冻结资产
由于购买使用 harp-orm/transfers 软件包,它具有在购买实际完成时“冻结”购买项目值的能力。
$purchase = new Purchase(); $product = new Product(['price' => 100]); $purchase->addProduct($product, 3); $productItem = $purchase->getItems()->getFirst(); // This will return the value of the product * quantity // In this case 300 echo $productItem->getValue(); // Perform a freeze: $purchase->freeze(); $product->price = 4000; // Will still be 300 echo $productItem->getValue();
冻结是通过显式设置 "isFrozen" 标志来实现的,因此后续对 freeze()
的调用不会重新计算值。您可以通过显式将此标志设置为 false 并再次调用 freeze()
来“重新冻结”这些值,以利用此功能。
如果您冻结一个购买,所有相关的 PurchaseItems 和 StorePurchases 将与之一起冻结。
货币计算
购买有一个 "currency" 值,所有购买项目都使用此值。任何具有不同货币的产品都将使用 harp-orm/money 进行转换。这就是为什么购买项目在附加到包含其货币的购买之前无法正常工作。
$purchase = new Purchase(['currency' => 'GBP']); $product = new Product(['price' => 100, 'currency' => 'EUR']); $purchase->addProduct($product, 3); $productItem = $purchase->getItems()->getFirst(); // This will not 300, but convert it to GBP (around 240) echo $productItem->getValue();
退款
您可以全额或部分退款单个店铺购买。
$purchase = Purchase::find(1);
$storePurchase = $purchase->getStorePurchases()->getFirst();
$refund = new Refund();
// This will
$refund->setValue($storePurchase->getValue());
$storePurchase->getRefunds()->add($refund);
// Initialize omnipay getway - here you will have Paypal, Stripe etc.
$gateway = Omnipay::getFactory()->create('Dummy');
$refund->refund($gateway);
退款对象对其值进行验证,因此您不会退款超过商店购买剩余金额,考虑到之前的退款。
许可证
版权(c)2014,Clippings Ltd。由 Ivan Kerin 开发
在 BSD-3-Clause 许可证下,请参阅 LICENSE 文件。