PHP code example of tourze / alipay-mini-program-bundle

1. Go to this page and download the library: Download tourze/alipay-mini-program-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 / alipay-mini-program-bundle example snippets


use AlipayMiniProgramBundle\Entity\MiniProgram;

$miniProgram = new MiniProgram();
$miniProgram->setAppId('your_app_id');
$miniProgram->setPrivateKey('your_private_key');
$miniProgram->setAlipayPublicKey('alipay_public_key');
$miniProgram->setEncryptKey('your_encrypt_key'); // Optional

return [
    // ...
    AlipayMiniProgramBundle\AlipayMiniProgramBundle::class => ['all' => true],
];

use AlipayMiniProgramBundle\Service\UserService;

// Update user information
$userService->updateUserInfo($user, [
    'nick_name' => 'John Doe',
    'avatar' => 'https://example.com/avatar.jpg',
    'province' => 'Guangdong',
    'city' => 'Shenzhen',
    'gender' => 'male'
]);

// Bind phone to user
$userService->bindPhone($user, '13800138000');

use AlipayMiniProgramBundle\Service\FormIdService;

// Save a form ID
$formId = $formIdService->saveFormId($miniProgram, $user, $formIdString);

// Get an available form ID
$availableFormId = $formIdService->getAvailableFormId($miniProgram, $user);

// Clean expired form IDs
$deletedCount = $formIdService->cleanExpiredFormIds();

use AlipayMiniProgramBundle\Service\TemplateMessageService;

// Send a template message
$success = $templateMessageService->send($templateMessage);

// Send pending messages (batch processing)
$templateMessageService->sendPendingMessages($limit = 10);

use AlipayMiniProgramBundle\Entity\TemplateMessage;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Events;

#[AsEntityListener(event: Events::postPersist, method: 'postPersist', entity: TemplateMessage::class)]
class CustomTemplateMessageListener
{
    public function postPersist(TemplateMessage $message, PostPersistEventArgs $args): void
    {
        // Custom processing logic
    }
}

use AlipayMiniProgramBundle\Service\UserService;

class CustomUserService extends UserService
{
    public function updateUserInfo(User $user, array $userInfo): void
    {
        // Custom validation
        parent::updateUserInfo($user, $userInfo);
        // Additional processing
    }
}

// Use environment variables for sensitive configuration
$miniProgram->setPrivateKey($_ENV['ALIPAY_PRIVATE_KEY']);
$miniProgram->setAlipayPublicKey($_ENV['ALIPAY_PUBLIC_KEY']);
$miniProgram->setEncryptKey($_ENV['ALIPAY_ENCRYPT_KEY']);
bash
php bin/console doctrine:migrations:migrate
bash
php bin/console alipay:mini-program:clean-expired-form-ids