1. Go to this page and download the library: Download tourze/wechat-pay-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/ */
// Create a payment order
$params = new AppOrderParams();
$params->setMchId('your_merchant_id');
$params->setAppId('your_app_id');
// ... set other parameters
$payOrder = $unifiedOrder->createAppOrder($params);
// Refund processing is handled automatically via RefundOrder entity
$refundOrder = new RefundOrder();
$refundOrder->setPayOrder($payOrder);
$refundOrder->setMoney($amount);
$refundOrder->setReason($reason);
$entityManager->persist($refundOrder);
$entityManager->flush(); // This triggers the refund via RefundOrderListener
use WechatPayBundle\Service\UnifiedOrder;
use WechatPayBundle\Request\AppOrderParams;
class CustomPaymentService
{
public function __construct(private UnifiedOrder $unifiedOrder) {}
public function processCustomPayment(array $orderData): array
{
$params = new AppOrderParams();
$params->setMchId($orderData['merchant_id']);
$params->setAppId($orderData['app_id']);
$params->setContractId($orderData['order_id']);
$params->setDescription($orderData['description']);
$params->setMoney($orderData['amount']);
return $this->unifiedOrder->createAppOrder($params);
}
}
use WechatPayBundle\Entity\PayOrder;
use WechatPayBundle\Enum\PayOrderStatus;
class PaymentCallbackHandler
{
public function handleCallback(array $callbackData): void
{
$orderId = $callbackData['out_trade_no'];
$payOrder = $this->findPayOrder($orderId);
if ($callbackData['result_code'] === 'SUCCESS') {
$payOrder->setStatus(PayOrderStatus::SUCCESS);
$payOrder->setPayTime(new \DateTime());
} else {
$payOrder->setStatus(PayOrderStatus::FAIL);
}
$this->entityManager->flush();
}
}
use WechatPayBundle\Entity\Merchant;
class MerchantService
{
public function configureMerchant(string $mchId, string $pemKey): Merchant
{
$merchant = new Merchant();
$merchant->setMchId($mchId);
$merchant->setPemKey($pemKey);
$merchant->setIsActive(true);
$this->entityManager->persist($merchant);
$this->entityManager->flush();
return $merchant;
}
}