PHP code example of canebaycomputers / valorpay

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

    

canebaycomputers / valorpay example snippets


use CaneBayComputers\ValorPay\Facades\ValorPay;

$page = ValorPay::createHostedPage(
    cents: 2500,
    invoiceNumber: 'INV1001',            // your reference: it's how you find the payment again
    description: 'Web hosting, November',
    successUrl: route('checkout.done', $invoice),
    failureUrl: route('checkout.failed', $invoice),
    customerName: $invoice->business_name, // cleaned to what ValorPay accepts
);

$invoice->update(['valorpay_page_uid' => $page->uid]);

return redirect($page->url);

$payments = ValorPay::transactionsForInvoice('INV1001', since: $invoice->created_at);

if ($sale = $payments->paidSale(2500)) {
    $invoice->markPaid(txnId: $sale->id(), approval: $sale->approvalCode());
}

use CaneBayComputers\ValorPay\Subscriptions\Frequency;
use CaneBayComputers\ValorPay\Subscriptions\SubscriptionRequest;

// 1. The first month is an ordinary hosted page (step 1), confirmed with the transaction list (step 2).
$sale = ValorPay::transactionsForInvoice('INV1001', $invoice->created_at)->paidSale(2500);

// 2. The sale carries the card token. Subscribe from next month on.
$subscriptionId = ValorPay::createSubscription(new SubscriptionRequest(
    cents: 2500,
    cardToken: $sale->token(),
    customerName: 'Acme Co',
    zip: '29486',
    startsOn: now()->addMonth(),         // date of the first scheduled charge
    frequency: Frequency::Monthly,       // Weekly, BiWeekly, Monthly, Quarterly, Annually, Daily
    description: 'Web hosting',
    email: '[email protected]',
));

ValorPay::deactivateSubscription($id);   // pause; already paused or finished is fine
ValorPay::activateSubscription($id);     // resume; already active is fine
ValorPay::deleteSubscription($id);       // for good; already deleted is fine
ValorPay::updateSubscription($id, new SubscriptionRequest(...)); // send the whole subscription

ValorPay::subscription($id);             // ?Subscription, active or not
ValorPay::subscriptions(active: true);   // list<Subscription>; an empty list when there are none

$charges = ValorPay::transactionsForSubscription($subscriptionId, since: $subscription->started_at);
$lastGood = $charges->standingSales()->first(); // newest first

use CaneBayComputers\ValorPay\Facades\ValorPay;

class ValorPayWebhookController
{
    public function __invoke(Request $request)
    {
        // Your own prefix ("INV") skips payments other apps make on the same merchant.
        $hint = ValorPay::webhookHint($request->getContent(), invoicePrefix: 'INV');

        foreach ($hint->invoiceNumbers as $number) {
            $invoice = Invoice::firstWhere('number', $number);
            $sale = $invoice ? ValorPay::transactionsForInvoice($number, $invoice->created_at)->paidSale($invoice->cents) : null;
            // Act only on $sale: that's ValorPay's own record.
        }

        foreach ($hint->subscriptionIds as $id) {
            // Re-check with ValorPay::transactionsForSubscription($id, ...) and ValorPay::subscription($id).
        }

        return response()->noContent();
    }
}

Http::fake(['*valorpaytech.com*' => Http::response(['status' => 'SUCCESS', 'data' => [[
    'REF_TXN_ID' => '1', 'EPI' => config('valorpay.epi'), 'INVOICE_NO' => 'INV1001', 'TXN_TYPE' => 'SALE',
    'RESPONSE_CODE' => '00', 'APPROVAL_CODE' => 'TEST01', 'BASE_AMOUNT' => '2500',
]]])]);
bash
php artisan vendor:publish --tag=valorpay-config