<?php

$I = new AcceptanceTester\CustomerSteps($scenario);
$I->wantTo('purchase with my card and make the payment authorized');
$admin = $I->haveFriend('admin');
$admin->does(function (AcceptanceTester $I) {
    $steps = new AcceptanceTester\AdminSteps($I);
    $steps->registerWebPayModule('authorize');
});
$I->login();
$I->amOnPage('/products/list.php');
$I->click('おなべ');
$I->proceedToPay();
$I->uncheckOption('input[type="checkbox"][name="card_info"]');
$I->pushMockChargeResponse(['captured' => false]);
$I->payWithNewCard();
$I->waitForText('Test shopの商品をご購入いただき、ありがとうございました', 5);
$I->loadRequest();
$I->seeInData('amount', 2782);
$I->seeInData('card');
$I->seeInData('currency', 'jpy');
$I->seeInData('description', '1');
$I->seeInData('capture', false);
$I->seeInLogs(['/https:\\/\\/api\\.webpay\\.jp charge\\.create: a:5:{s:6:"amount";i:2782;s:8:"currency";s:3:"jpy";s:11:"description";s:1:"1";s:7:"capture";b:0;s:4:"card";s:19:"tok_[^"]*";} from 127.0.0.1/']);
$admin->does(function (AcceptanceTester $I) {
    $steps = new AcceptanceTester\AdminSteps($I);
    $steps->seeLastOrder(['customer_id' => '1', 'payment_id' => 'クレジットカード決済', 'status' => '入金待ち']);
});
<?php

$chargeId = 'ch_authcharge';
$I = new AcceptanceTester\AdminSteps($scenario);
$I->wantTo('capture a charge for an order');
$I->registerWebPayModule('authorize');
$customer = $I->haveFriend('customer');
$customer->does(function (AcceptanceTester $I) {
    $I = new AcceptanceTester\CustomerSteps($I);
    $I->login();
    $I->amOnPage('/products/list.php');
    $I->click('おなべ');
    $I->proceedToPay();
    $I->uncheckOption('input[type="checkbox"][name="card_info"]');
    $I->pushMockChargeResponse(['id' => 'ch_authcharge', 'captured' => false]);
    $I->payWithNewCard();
    $I->waitForText('Test shopの商品をご購入いただき、ありがとうございました', 5);
    $I->loadRequest();
    $I->seeInData('amount', 2782);
    $I->seeInData('card');
    $I->seeInData('currency', 'jpy');
    $I->seeInData('description', '1');
    $I->seeInData('capture', false);
    $I->seeInLogs(['/https:\\/\\/api\\.webpay\\.jp charge\\.create/']);
});
$I->seeLastOrder(['customer_id' => '1', 'payment_id' => 'クレジットカード決済', 'status' => '入金待ち']);
$I->seeLink($chargeId);
$I->see('2,782円の仮売上');
$I->seeLink('実売上化する');
$I->fillField('deliv_fee', '1200');
$I->executeJS('window.confirm = function (text) { return text == "この内容で登録しても宜しいでしょうか"; }');
$customer_id = 'cus_registered';
$customer_attrs = ['id' => $customer_id, 'description' => '1'];
$I = new AcceptanceTester\CustomerSteps($scenario);
$I->am('a new customer');
$I->wantTo('register my card to purchase again');
$admin = $I->haveFriend('admin');
$admin->does(function (AcceptanceTester $I) {
    $steps = new AcceptanceTester\AdminSteps($I);
    $steps->registerWebPayModule();
});
$I->login();
$I->registerCardByPurchase($customer_attrs);
$I->expectTo('buy another item with the previously registered card');
$I->amOnPage('/products/list.php');
$I->click('おなべ');
$I->pushMockCustomerResponse($customer_attrs);
$I->proceedToPay();
$I->loadRequest();
$I->seeRequestTo('GET', '/customers/' . $customer_id);
$I->see('次のカードが登録されています。');
$I->see('Visa');
$I->see('****-****-****-4242');
$I->see('TEST TEST');
$I->see('12/2019');
$I->see('登録してあるカードで支払う');
$I->see('登録したカードを削除する');
$I->see('別のカードで支払う');
$I->pushMockChargeResponse(['captured' => true]);
$I->click('登録してあるカードで支払う');
$I->waitForText('Test shopの商品をご購入いただき、ありがとうございました', 5);
$customer_id = 'cus_registered';
$customer_attrs = ['id' => $customer_id, 'description' => '1'];
$I = new AcceptanceTester\CustomerSteps($scenario);
$I->am('a new customer');
$I->wantTo('remove the registered card');
$admin = $I->haveFriend('admin');
$admin->does(function (AcceptanceTester $I) {
    $steps = new AcceptanceTester\AdminSteps($I);
    $steps->registerWebPayModule();
});
$I->login();
$I->registerCardByPurchase($customer_attrs);
$I->expectTo('buy another item with the new card');
$I->amOnPage('/products/list.php');
$I->click('おなべ');
$I->pushMockCustomerResponse($customer_attrs);
$I->proceedToPay();
$I->loadRequest();
$I->seeRequestTo('GET', '/customers/' . $customer_id);
$I->see('次のカードが登録されています。');
$I->see('Visa');
$I->see('****-****-****-4242');
$I->see('TEST TEST');
$I->see('12/2019');
$I->pushMockCustomerResponse(array_merge($customer_attrs, ['card' => null]));
$I->click('登録したカードを削除する');
$I->dontSee('次のカードが登録されています。');
$I->waitForElement('#WP_checkoutBox', 5);
$I->seeInLogs(['/https:\\/\\/api\\.webpay\\.jp customer\\.retrieve: s:14:"cus_registered"/', '/https:\\/\\/api\\.webpay\\.jp customer\\.delete_active_card: s:14:"cus_registered"/']);
$I->loadRequest();
$customer_id = 'cus_registered';
$customer_attrs = ['id' => $customer_id, 'description' => '1'];
$I = new AcceptanceTester\CustomerSteps($scenario);
$I->am('a new customer');
$I->wantTo('update my card');
$admin = $I->haveFriend('admin');
$admin->does(function (AcceptanceTester $I) {
    $steps = new AcceptanceTester\AdminSteps($I);
    $steps->registerWebPayModule();
});
$I->login();
$I->registerCardByPurchase($customer_attrs);
$I->expectTo('buy another item with the new card');
$I->amOnPage('/products/list.php');
$I->click('おなべ');
$I->pushMockCustomerResponse($customer_attrs);
$I->proceedToPay();
$I->loadRequest();
$I->seeRequestTo('GET', '/customers/' . $customer_id);
$I->see('次のカードが登録されています。');
$I->see('Visa');
$I->see('****-****-****-4242');
$I->see('TEST TEST');
$I->see('12/2019');
$I->click('別のカードで支払う');
$I->pushMockCustomerResponse($customer_attrs);
$I->pushMockChargeResponse(['captured' => true]);
$I->payWithNewCard();
$I->waitForText('Test shopの商品をご購入いただき、ありがとうございました', 5);
$I->loadRequest();