PHP code example of carllee1983 / newebpay

1. Go to this page and download the library: Download carllee1983/newebpay 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/ */

    

carllee1983 / newebpay example snippets


use CarlLee\NewebPay\Laravel\Facades\NewebPay;

Route::post('/pay', function () {
    return NewebPay::payment(
        'ORDER_' . time(),  // 訂單編號
        1000,               // 金額
        '測試商品',          // 商品描述
        '[email protected]'  // 買家 Email
    )->submit();
});

NewebPay::payment($orderNo, $amount, $desc, $email)
    ->creditInstallment([3, 6]) // 僅開放 3, 6 期分期
    ->atm('2025-12-31')         // 指定 ATM 繳費期限
    ->linePay()                 // 啟用 LINE Pay
    ->submit();

use CarlLee\NewebPay\Operations\CreditPayment;
use CarlLee\NewebPay\FormBuilder;

// 初始化
$payment = new CreditPayment('MerchantID', 'HashKey', 'HashIV');

// 設定參數
$payment->setTestMode(true)
        ->setMerchantOrderNo('ORDER_' . time())
        ->setAmt(1000)
        ->setItemDesc('商品名稱')
        ->setEmail('[email protected]')
        ->setReturnURL('https://site.com/return')
        ->setNotifyURL('https://site.com/notify');

// 產生 HTML 表單並送出
echo FormBuilder::create($payment)->build();

use CarlLee\NewebPay\Notifications\PaymentNotify;

$notify = new PaymentNotify('HashKey', 'HashIV');

try {
    // 1. 自動驗證簽章與解密 (若驗證失敗會拋出例外)
    $data = $notify->verifyOrFail($_POST);
    
    // 2. 判斷交易結果
    if ($notify->isSuccess()) {
        // 交易成功!
        $orderId = $notify->getMerchantOrderNo();
        $amount = $notify->getAmt();
        
        // TODO: 更新資料庫訂單狀態...
    } else {
        // 交易失敗 (刷卡失敗、餘額不足等)
    }
    
} catch (\Exception $e) {
    // 簽章驗證失敗,可能是偽造的請求
    Log::error('Payment notify verification failed: ' . $e->getMessage());
}

namespace App\Listeners;

use CarlLee\NewebPay\Laravel\Events\PaymentReceived;

class HandlePaymentReceived
{
    public function handle(PaymentReceived $event)
    {
        $notify = $event->notify;
        
        if ($notify->isSuccess()) {
            // 處理付款成功邏輯
            $orderId = $notify->getMerchantOrderNo();
            // ...
        }
    }
}

protected $listen = [
    \CarlLee\NewebPay\Laravel\Events\PaymentReceived::class => [
        \App\Listeners\HandlePaymentReceived::class,
    ],
];

use CarlLee\NewebPay\Laravel\Facades\NewebPay;

public function test_payment_flow()
{
    // 1. 啟用模擬模式
    NewebPay::fake();

    // 2. 執行您的程式碼
    $this->post('/checkout');

    // 3. 驗證是否建立了正確的支付請求
    NewebPay::assertSent(function ($payment) {
        return $payment->get('Amt') === 1000 &&
               $payment->get('Email') === '[email protected]';
    });
}

use CarlLee\NewebPay\Queries\QueryOrder;

$result = QueryOrder::create($id, $key, $iv)
    ->query('ORDER_NO_12345', 1000); // 需帶入訂單編號與金額
    
echo $result['TradeStatus']; // 1=成功, 0=未付款...

use CarlLee\NewebPay\Actions\CreditClose;

CreditClose::create($id, $key, $iv)
    ->refund('ORDER_NO_12345', 1000); // 全額退款

public function checkout() {
    $payment = NewebPay::credit()->...; // 設定參數
    
    return response()->json([
        'url' => $payment->getApiUrl(),
        'fields' => $payment->getContent() // 取得所有加密後的隱藏欄位
    ]);
}