mivita/tests/Unit/Services/PaymentAboCallbackCreatesUserAboTest.php
2026-04-10 17:15:27 +02:00

147 lines
4.2 KiB
PHP

<?php
/**
* Payone-Server-Callback kann vor dem Checkout-Redirect fertig sein: dann muss
* Payment::paymentStatusPaidAction trotzdem UserAbo/UserAboOrder anlegen (createNewAbo).
*/
use App\Models\Country;
use App\Models\PaymentTransaction;
use App\Models\Shipping;
use App\Models\ShippingCountry;
use App\Models\ShoppingOrder;
use App\Models\ShoppingPayment;
use App\Models\ShoppingUser;
use App\Models\UserAbo;
use App\Models\UserAboOrder;
use App\Models\UserShop;
use App\Services\AboHelper;
use App\Services\Payment;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
function paymentAboCallbackTestBootstrap(): array
{
$country = Country::create([
'code' => 'DE',
'phone' => '49',
'en' => 'Germany',
'de' => 'Deutschland',
'es' => 'Alemania',
'fr' => 'Allemagne',
'it' => 'Germania',
'ru' => 'Германия',
]);
$shipping = Shipping::create([
'name' => 'Standard',
'active' => true,
]);
$shippingCountry = ShippingCountry::create([
'shipping_id' => $shipping->id,
'country_id' => $country->id,
]);
$shopOwner = User::forceCreate([
'email' => 'shop-owner-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$userShop = UserShop::create([
'user_id' => $shopOwner->id,
'name' => 'TS'.substr(uniqid('', true), 0, 8),
'slug' => 'ts-'.uniqid(),
'active' => true,
]);
$consultant = User::forceCreate([
'email' => 'consultant-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$customer = User::forceCreate([
'email' => 'customer-'.uniqid('', true).'@example.com',
'password' => bcrypt('secret'),
'lang' => 'de',
]);
$shoppingUser = ShoppingUser::create([
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'billing_country_id' => $country->id,
'shipping_country_id' => $country->id,
'billing_email' => 'cust-'.uniqid('', true).'@example.com',
'is_for' => 'ot',
]);
$shoppingOrder = ShoppingOrder::create([
'shopping_user_id' => $shoppingUser->id,
'auth_user_id' => $customer->id,
'member_id' => $consultant->id,
'country_id' => $shippingCountry->id,
'user_shop_id' => $userShop->id,
'payment_for' => 6,
'points' => 10,
'is_abo' => true,
'abo_interval' => 15,
'paid' => false,
'txaction' => 'prev',
'mode' => 'test',
'total' => 100,
'subtotal' => 90,
]);
$shoppingPayment = ShoppingPayment::create([
'shopping_order_id' => $shoppingOrder->id,
'reference' => 'ref-'.uniqid(),
'amount' => 10000,
'currency' => 'EUR',
'clearingtype' => 'cc',
'abo_interval' => 15,
'mode' => 'test',
]);
PaymentTransaction::create([
'shopping_payment_id' => $shoppingPayment->id,
'request' => 'transaction',
'txid' => 123456,
'userid' => 987654,
'status' => 'PAYONE',
'txaction' => 'paid',
'mode' => 'test',
]);
return [$shoppingOrder, $shoppingPayment];
}
it('legt UserAbo an wenn Payone-Callback vor Checkout-Erfolgsseite bezahlt (paymentStatusPaidAction)', function () {
[$shoppingOrder, $shoppingPayment] = paymentAboCallbackTestBootstrap();
expect(UserAbo::count())->toBe(0);
Payment::paymentStatusPaidAction($shoppingOrder, true, $shoppingPayment);
$shoppingOrder->refresh();
expect((bool) $shoppingOrder->paid)->toBeTrue();
$userAbo = $shoppingOrder->getUserAbo();
expect($userAbo)->not->toBeNull();
expect($userAbo->status)->toBe(2);
expect(UserAboOrder::where('shopping_order_id', $shoppingOrder->id)->count())->toBe(1);
});
it('createNewAbo ist idempotent wenn UserAboOrder bereits existiert', function () {
[$shoppingOrder, $shoppingPayment] = paymentAboCallbackTestBootstrap();
AboHelper::createNewAbo($shoppingPayment);
expect(UserAbo::count())->toBe(1);
AboHelper::createNewAbo($shoppingPayment);
expect(UserAbo::count())->toBe(1);
});