PHP code example of tourze / wechat-pay-score-bundle
1. Go to this page and download the library: Download tourze/wechat-pay-score-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/ */
use WechatPayScoreBundle\Entity\ScoreOrder;
use WechatPayScoreBundle\Enum\ScoreOrderState;
// Create Pay Score order
$scoreOrder = new ScoreOrder();
$scoreOrder->setOutTradeNo('20241201001')
->setAppId('your_app_id')
->setServiceId('your_service_id')
->setServiceIntroduction('Service description')
->setRiskFundName('Risk fund')
->setRiskFundAmount(10000)
->setNotifyUrl('https://example.com/notify')
->setStartTime('20241201120000')
->setState(ScoreOrderState::CREATED);
$entityManager->persist($scoreOrder);
$entityManager->flush();
// Add Pay Score order relationship in your user entity
class User
{
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ScoreOrder::class)]
private Collection $scoreOrders;
}
// Listen to Pay Score order status changes
class ScoreOrderSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ScoreOrderCallbackEvent::class => 'onScoreOrderCallback',
];
}
public function onScoreOrderCallback(ScoreOrderCallbackEvent $event): void
{
$scoreOrder = $event->getScoreOrder();
// Handle order status changes
}
}
use WechatPayScoreBundle\Event\ScoreOrderCallbackEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomScoreOrderListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ScoreOrderCallbackEvent::class => 'onScoreOrderCallback',
];
}
public function onScoreOrderCallback(ScoreOrderCallbackEvent $event): void
{
$scoreOrder = $event->getScoreOrder();
$callbackData = $event->getCallbackData();
// Custom business logic
switch ($scoreOrder->getState()) {
case ScoreOrderState::DONE:
// Handle completed order
break;
case ScoreOrderState::REVOKED:
// Handle cancelled order
break;
}
}
}
use WechatPayScoreBundle\Entity\ScoreOrder;
use WechatPayScoreBundle\Enum\ScoreOrderState;
// Complete an order
$scoreOrder->setState(ScoreOrderState::DONE);
$scoreOrder->setEndTime(date('YmdHis'));
$scoreOrder->setTotalAmount(10000);
$entityManager->flush();
// Cancel an order
$scoreOrder->setCancelReason('User requested cancellation');
$entityManager->remove($scoreOrder);
$entityManager->flush();
use WechatPayScoreBundle\Entity\PostPayment;
use WechatPayScoreBundle\Entity\PostDiscount;
// Add post-payment information
$postPayment = new PostPayment();
$postPayment->setName('Service Fee')
->setAmount(5000)
->setDescription('Basic service fee')
->setCount(1);
$scoreOrder->addPostPayment($postPayment);
// Add discount information
$postDiscount = new PostDiscount();
$postDiscount->setName('New User Discount')
->setAmount(1000)
->setDescription('First-time user discount')
->setCount(1);
$scoreOrder->addPostDiscount($postDiscount);