PHP code example of perafan / cashier-openpay

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

    

perafan / cashier-openpay example snippets


use Perafan\CashierOpenpay\Billable;

class User extends Authenticatable
{
    use Billable;
}



namespace App\Exceptions;

use Perafan\CashierOpenpay\OpenpayExceptionsHandler;
...

class Handler extends ExceptionHandler
{
    use OpenpayExceptionsHandler;

    ...

    public function render($request, Throwable $exception)
    {
        if ($this->isOpenpayException($exception)) {
            return $this->renderOpenpayException($request, $exception);
        }
        return parent::render($request, $exception);
    }
}

trait MyOpenpayExceptionsHandler
{
    use OpenpayExceptionsHandler {
        OpenpayExceptionsHandler::renderOpenpayException as parentRenderOpenpayException;
    }
    
    public function renderOpenpayException(Request $request, OpenpayApiError $exception)
    {
        $this->parentRenderOpenpayException($request, $exception);
        
        //your code
    }
} 

$user->createAsOpenpayCustomer();

//Or you can send additional data

$data = [
    'name' => 'Teofilo',
    'last_name' => 'Velazco',
    'phone_number' => '4421112233',
    'address' => [
        'line1' => 'Privada Rio No. 12',
        'line2' => 'Co. El Tintero',
        'line3' => '',
        'postal_code' => '76920',
        'state' => 'Querétaro',
        'city' => 'Querétaro.',
        'country_code' => 'MX'
    ]
];

$openpay_customer = $user->createAsOpenpayCustomer($data);

$openpay_customer = $user->asOpenpayCustomer();

$openpay_customer  = $user->asOpenpayCustomer();
$openpay_customer->name = 'Juan';
$openpay_customer->last_name = 'Godinez';
$openpay_customer->save();

$openpay_customer = $user->asOpenpayCustomer();
$openpay_customer->delete();

use Perafan\CashierOpenpay\Openpay\Customer as OpenpayCustomer;

OpenpayCustomer::create([
    'name' => 'Teofilo',
    'last_name' => 'Velazco',
    'email' => '[email protected]',
    'phone_number' => '4421112233',
    'address' => [
        'line1' => 'Privada Rio No. 12',
        'line2' => 'Co. El Tintero',
        'line3' => '',
        'postal_code' => '76920',
        'state' => 'Querétaro',
        'city' => 'Querétaro.',
        'country_code' => 'MX'
    ]
]);

use Perafan\CashierOpenpay\Openpay\Customer as OpenpayCustomer;

$customer = OpenpayCustomer::find('a9ualumwnrcxkl42l6mh');

use Perafan\CashierOpenpay\Openpay\Customer as OpenpayCustomer;

$customer_list = OpenpayCustomer::all();

// with filters

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$customer_list = OpenpayCustomer::all($filters);

use Perafan\CashierOpenpay\Openpay\Customer as OpenpayCustomer;

$customer = OpenpayCustomer::find('a9ualumwnrcxkl42l6mh');
$customer->name = 'Juan';
$customer->last_name = 'Godinez';
$customer->save();

use Perafan\CashierOpenpay\Openpay\Customer as OpenpayCustomer;

$customer = OpenpayCustomer::find('a9ualumwnrcxkl42l6mh');
$customer->delete();

$card_data = [
	'holder_name' => 'Teofilo Velazco',
	'card_number' => '4916394462033681',
	'cvv2' => '123',
	'expiration_month' => '12',
	'expiration_year' => '15'
];

$card = $user->addCard($card_data);

// with token

$card_data = [
    'token_id' => 'tokgslwpdcrkhlgxqi9a',
    'device_session_id' => '8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o'
];

$card = $user->addCard($card_data);

// with address

$address_data = [
    'line1' => 'Privada Rio No. 12',
    'line2' => 'Co. El Tintero',
    'line3' => '',
    'postal_code' => '76920',
    'state' => 'Querétaro',
    'city' => 'Querétaro.',
    'country_code' => 'MX'
];

$card = $user->addCard($card_data, $address_data);

use Perafan\CashierOpenpay\Card;

$card = $user->cards->first;
//or
$card = Card::find(1);

$openpay_card = $card->asOpenpayCard();

use Perafan\CashierOpenpay\Card;

$cards = $user->cards;
// or
$cards = Card::where('user_id', $user->id)->get();

use Perafan\CashierOpenpay\Card;
use Perafan\CashierOpenpay\Openpay\Card as OpenpayCard;

$cards = $user->cards;

$openpay_cards = $cards->map(function($card) {
    return $card->asOpenpayCard();
});

// or

$cards = Card::where('user_id', $user->id)->get();

$openpay_cards = $cards->map(function($card) {
    return $card->asOpenpayCard();
});

// or 

$openpay_customer = $user->asOpenpayCustomer();

$openpay_cards = OpenpayCard::all([], $openpay_customer);

// with filters

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_cards = OpenpayCard::all($filters, $openpay_customer);

use Perafan\CashierOpenpay\Card;

$card = $user->cards->first;
//or
$card = Card::find(1);

$openpay_card = $card->asOpenpayCard();
$deleted_card = $openpay_card->delete();

if (!is_array($deleted_card)) {
    //The card was deleted in Openpay
    $card->delete();
}

use Perafan\CashierOpenpay\Openpay\Card as OpenpayCard;

$card_data = [
	'holder_name' => 'Teofilo Velazco',
	'card_number' => '4916394462033681',
	'cvv2' => '123',
	'expiration_month' => '12',
	'expiration_year' => '15'
];

$openpay_card = OpenpayCard::add($card_data);

// with token

$card_data = [
    'token_id' => 'tokgslwpdcrkhlgxqi9a',
    'device_session_id' => '8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o'
];

$openpay_card = OpenpayCard::add($card_data);

// with address

$address_data = [
    'line1' => 'Privada Rio No. 12',
    'line2' => 'Co. El Tintero',
    'line3' => '',
    'postal_code' => '76920',
    'state' => 'Querétaro',
    'city' => 'Querétaro.',
    'country_code' => 'MX'
];

$card_data['address'] = $address_data;

$openpay_card = OpenpayCard::add($card_data);

use Perafan\CashierOpenpay\Openpay\Card as OpenpayCard;

$openpay_card = OpenpayCard::find('k9pn8qtsvr7k7gxoq1r5');

use Perafan\CashierOpenpay\Openpay\Card as OpenpayCard;

$openpay_card = OpenpayCard::all();

// with filters

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_card = OpenpayCard::all($filters);

use Perafan\CashierOpenpay\Openpay\Card as OpenpayCard;

$openpay_card = OpenpayCard::find('k9pn8qtsvr7k7gxoq1r5');
$openpay_card->delete();
//Card was not delete on your database, only was deleted in openpay

$bank_data = [
	'clabe' => '072910007380090615',
	'alias' => 'Cuenta principal',
	'holder_name' => 'Teofilo Velazco'
];

$bank_account = $user->addBankAccount($bank_data);

use Perafan\CashierOpenpay\BankAccount;

$bank_account = $user->bank_accounts->first;
// or
$bank_account = BankAccount::where('user_id', $user->id)->first();

$openpay_bank_account = $bank_account->asOpenpayBankAccount();

use Perafan\CashierOpenpay\BankAccount;

$bank_accounts = $user->bank_accounts;
// or
$bank_accounts = BankAccount::where('user_id', $user->id)->get();

use Perafan\CashierOpenpay\BankAccount;
use Perafan\CashierOpenpay\Openpay\BankAccount as OpenpayBankAccount;

$bank_accounts = $user->bank_accounts;

$openpay_bank_accounts = $bank_accounts->map(function($bank_account) {
    return $bank_account->asOpenpayBankAccount();
});

// or

$bank_accounts = BankAccount::where('user_id', $user->id)->get();

$openpay_bank_accounts = $bank_accounts->map(function($bank_account) {
    return $bank_account->asOpenpayBankAccount();
});

// or 

$openpay_customer = $user->asOpenpayCustomer();

$openpay_bank_accounts = OpenpayBankAccount::all([], $openpay_customer);

// with filters

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_bank_accounts = OpenpayBankAccount::all($filters, $openpay_customer);

use Perafan\CashierOpenpay\BankAccount;

$bank_account = $user->bank_accounts->first;
// or
$bank_account = BankAccount::where('user_id', $user->id)->first();

$openpay_bank_account = $bank_account->asOpenpayBankAccount();

$deleted_bank_account = $openpay_bank_account->delete();

if (!is_array($deleted_bank_account)) {
    //The card was deleted in Openpay
    $bank_account->delete();
}

$charge_data = [
	'source_id' => 'tvyfwyfooqsmfnaprsuk',
	'method' => 'card',
	'currency' => 'MXN',
    'description' => 'Cargo inicial a mi merchant',
    'order_id' => 'oid-00051',
    'device_session_id' => 'kR1MiQhz2otdIuUlQkbEyitIqVMiI16f',
];

$openpay_charge = $user->charge(100, $charge_data);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$openpay_charge = OpenpayCharge::find('a9ualumwnrcxkl42l6mh');

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_customer = $user->asOpenpayCustomer();

$openpay_charge = OpenpayCharge::all($filters, $openpay_customer);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$capture_data = ['amount' => 150.00];

$openpay_charge = OpenpayCharge::find('a9ualumwnrcxkl42l6mh');

$openpay_charge->capture($capture_data);

// Send charge id as first param 
$charge_id = 'tvyfwyfooqsmfnaprsuk';
$user->refund($charge_id);
//or

$description = 'Reembolso';
$user->refund($charge_id, $description);

//or
$amount = 150.00;
$user->refund($charge_id, $description, $amount);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$charge_data = [
	'method' => 'card',
	'source_id' => 'krfkkmbvdk3hewatruem',
	'amount' => 100,
	'description' => 'Cargo inicial a mi merchant',
	'order_id' => 'ORDEN-00071'
];

$openpay_charge = OpenpayCharge::create($charge_data);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$openpay_charge = OpenpayCharge::find('tvyfwyfooqsmfnaprsuk');

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$openpay_charges = OpenpayCharge::all();

// with filters
$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_charges = OpenpayCharge::all($filters);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$capture_data = ['amount' => 150.00];

$openpay_charge = OpenpayCharge::find('tvyfwyfooqsmfnaprsuk');
$capture_data->capture($capture_data);

use Perafan\CashierOpenpay\Openpay\Charge as OpenpayCharge;

$refund_data = ['description' => 'Devolución'];

$openpay_charge = OpenpayCharge::find('tvyfwyfooqsmfnaprsuk');
$openpay_charge->refund($refund_data);

$transfer_data = [
	'customer_id' => 'aqedin0owpu0kexr2eor',
	'amount' => 12.50,
	'description' => 'Cobro de Comisión',
	'order_id' => 'ORDEN-00061'
];

$openpay_customer = $user->asOpenpayCustomer();
$transfer = $openpay_customer->transfers->create($transfer_data);

$openpay_customer = $user->asOpenpayCustomer();
$transfer = $openpay_customer->transfers->get('tyxesptjtx1bodfdjmlb');

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_customer = $user->asOpenpayCustomer();
$transfer_list = $openpay_customer->transfers->getList($filters);

$payout_data = [
	'method' => 'card',
	'destination_id' => 'k9pn8qtsvr7k7gxoq1r5',
	'amount' => 1000,
	'description' => 'Retiro de saldo semanal',
	'order_id' => 'ORDEN-00062'
];

$openpay_customer = $user->asOpenpayCustomer();
$payout = $openpay_customer->payouts->create($payout_data);

$openpay_customer = $user->asOpenpayCustomer();
$payout = $openpay_customer->payouts->get('tysznlyigrkwnks6eq2c');

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_customer = $user->asOpenpayCustomer();
$payout_list = $customer->payouts->getList($filters);

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$plan_data = [
	'amount' => 150.00,
	'status_after_retry' => 'cancelled',
	'retry_times' => 2,
	'name' => 'Plan Curso Verano',
	'repeat_unit' => 'month',
	'trial_days' => '30',
	'repeat_every' => '1',
	'currency' => 'MXN'
];

$openpay_plan = OpenpayPlan::add($plan_data);

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$openpay_plan = OpenpayPlan::find('pduar9iitv4enjftuwyl');

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$openpay_plans = OpenpayPlan::all();
// with filters
$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$openpay_plans = OpenpayPlan::all($filters);

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$openpay_plan = OpenpayPlan::find('pduar9iitv4enjftuwyl');
$openpay_plan->name = 'Plan Curso de Verano 2021';
$openpay_plan->save();

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$openpay_plan = OpenpayPlan::find('pduar9iitv4enjftuwyl');
$openpay_plan->delete();

use Perafan\CashierOpenpay\Openpay\Plan as OpenpayPlan;

$openpay_plan = OpenpayPlan::find('pduar9iitv4enjftuwyl');

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$subscription_list = $openpay_plan->subscriptions->getList($filters);

$plan_id = 'pduar9iitv4enjftuwyl';

$subscription = $user->newSubscription($plan_id);

// Add the name of subscription
$options = [
	'trial_end_date' => '2021-01-01', 
	'card_id' => 'konvkvcd5ih8ta65umie'
];

$subscription = $user->newSubscription($plan_id, $options);

// Add the name of subscription
$name = 'plan_verano_2021';

$subscription = $user->newSubscription($plan_id, $options, $name);

$name = 'plan_verano_2021';
$user->subscribed($name);

$name_2027 = 'plan_verano_2027';
$user->subscribed($name_2027);

$plan_id = 'pduar9iitv4enjftuwyl';
$user->subscribed($name, $plan_id); 

$user->subscribed($name, 'ptyui9iit40nfwftuwyl'); 

$plans = [
    'pduar9iitv4enjftuwyl',
    'ptyui9iit40nfwftuwyl'
];

$user->subscribedToPlan($plans);

$subscription = $user->subscription();

$subscription->onTrial();

$subscription_name = 'plan_verano_2027';

$user->onTrial($subscription_name); 

use Perafan\CashierOpenpay\Subscription;
$subscription = $user->subscriptions->first;
// or
$subscription = Subscription::find('s7ri24srbldoqqlfo4vp');

$subscription->asOpenpaySubscription();

$openpay = Openpay::getInstance('moiep6umtcnanql3jrxp', 'sk_3433941e467c1055b178ce26348b0fac');

$filters = [
	'creation[gte]' => '2020-01-01',
	'creation[lte]' => '2020-12-31',
	'offset' => 0,
	'limit' => 5
];

$customer = $openpay->customers->get('a9ualumwnrcxkl42l6mh');
$subscriptionList = $customer->subscriptions->getList($filters);

$openpay = Openpay::getInstance('moiep6umtcnanql3jrxp', 'sk_3433941e467c1055b178ce26348b0fac');

$customer = $openpay->customers->get('a9ualumwnrcxkl42l6mh');
$subscription = $customer->subscriptions->get('s7ri24srbldoqqlfo4vp');
$subscription->trial_end_date = '2021-12-31';
$subscription->save();

$openpay = Openpay::getInstance('moiep6umtcnanql3jrxp', 'sk_3433941e467c1055b178ce26348b0fac');

$customer = $openpay->customers->get('a9ualumwnrcxkl42l6mh');
$subscription = $customer->subscriptions->get('s7ri24srbldoqqlfo4vp');
$subscription->delete();


$openpayCustomer = $user->asOpenpayCustomer();

$openpayCustomer->name = 'Pedro';

$openpayCustomer->save();

$openpaySubscription = $subscription->asOpenpaySubscription();

$subscription->trial_end_date = '2021-12-31';

$openpaySubscription->save();
bash
php artisan vendor:publish --tag="cashier-openpay-migrations"
php artisan vendor:publish --tag="cashier-openpay-configs"
php artisan vendor:publish --tag="cashier-openpay-webhook-controller"
 bash
php artisan migrate
dotenv
OPENPAY_LOG_ERRORS=true
blade
@if($errors->cashier->isNotEmpty())
    <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">
        @foreach ($errors->cashier->keys() as $key)
            <strong class="font-bold">{{ $key }} :</strong> {{ $errors->cashier->get($key)[0] }} <br>
        @endforeach
    </div>
@endif