PHP code example of tourze / wechat-mini-program-order-bundle

1. Go to this page and download the library: Download tourze/wechat-mini-program-order-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / wechat-mini-program-order-bundle example snippets


return [
    // ... other bundles
    WechatMiniProgramOrderBundle\WechatMiniProgramOrderBundle::class => ['all' => true],
];



use WechatMiniProgramOrderBundle\Request\UploadShoppingInfoRequest;
use WechatMiniProgramOrderBundle\Entity\ShoppingInfo;
use WechatMiniProgramOrderBundle\Entity\OrderKey;
use WechatMiniProgramOrderBundle\Entity\ShoppingItemList;
use WechatMiniProgramOrderBundle\Enum\OrderNumberType;
use WechatMiniProgramOrderBundle\Enum\OrderDetailType;
use WechatMiniProgramOrderBundle\Enum\LogisticsType;

// 1. Create order key
$orderKey = new OrderKey();
$orderKey->setOrderNumberType(OrderNumberType::USE_MCH_ORDER);
$orderKey->setMchId('your-mch-id');
$orderKey->setOutTradeNo('your-out-trade-no');

// 2. Create shopping info
$shoppingInfo = new ShoppingInfo();
$shoppingInfo->setOrderKey($orderKey);
$shoppingInfo->setPayer($user); // UserInterface instance
$shoppingInfo->setOrderDetailType(OrderDetailType::SHOPPING_ORDER);
$shoppingInfo->setLogisticsType(LogisticsType::EXPRESS);

// 3. Upload shopping info
$request = new UploadShoppingInfoRequest();
$request->setShoppingInfo($shoppingInfo);

use WechatMiniProgramOrderBundle\Request\UploadShoppingInfoRequest;
use WechatMiniProgramOrderBundle\Entity\ShoppingInfo;
use WechatMiniProgramOrderBundle\Entity\OrderKey;
use WechatMiniProgramOrderBundle\Entity\ShoppingItemList;

$request = new UploadShoppingInfoRequest();
$shoppingInfo = new ShoppingInfo();
$orderKey = new OrderKey();

// Configure your shopping info
$shoppingInfo->setOrderKey($orderKey);
$shoppingInfo->setPayer($user);
$shoppingInfo->setOrderDetailType(OrderDetailType::SHOPPING_ORDER);

$request->setShoppingInfo($shoppingInfo);

use WechatMiniProgramOrderBundle\Request\UploadShippingInfoRequest;
use WechatMiniProgramOrderBundle\Entity\ShippingInfo;

$request = new UploadShippingInfoRequest();
$shippingInfo = new ShippingInfo();

// Configure your shipping info
$shippingInfo->setTrackingNo('1234567890');
$shippingInfo->setDeliveryCompany('SF Express');

$request->setShippingInfo($shippingInfo);

// Upload multiple shopping items
$items = [];
foreach ($products as $product) {
    $item = new ShoppingItemList();
    $item->setMerchantItemId($product->getId());
    $item->setItemName($product->getName());
    $item->setItemCount($product->getQuantity());
    $items[] = $item;
}

$shoppingInfo->setItemList($items);

// Listen to order events
class OrderEventSubscriber implements EventSubscriberInterface
{
    public function onOrderCreated(OrderCreatedEvent $event): void
    {
        // Handle order creation
    }
}