Laravel Cashier fake user subscription for tests - phpunit

I'm using Laravel's Cashier for billing which is great. I'm trying to be good and keep my code tested but I'm having trouble "faking" a user with a subscription.
I've tried:
$user = App\Models\User::create([
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->email,
'password' => 'password1234',
'stripe_plan' => 'name_of_plan',
'stripe_active' => 1
]);
$this->be($user);
But if I then check $user->onPlan('name_of_plan') I get false :(
Is there a way to do this? As I'm sure you can appreciate I don't really want to launch the payment system until I've got tests to back it up!

Check that 'stripe_plan' and 'stripe_active' are defined as fillable for User. If they aren't then it may not be actually setting those values in User::create() which is why your test fails.
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, BillableContract {
use Authenticatable, CanResetPassword, Billable;
protected $table = 'users';
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'stripe_plan',
'stripe_active'
];
}

Related

401(woocommerce_api_user_cannot_read_product) response--Woocommerce REST API

the API works when ck_ and cs_ keys are for Admin and returns the std class object but when keys are for a different user returns You do not have permission to read this product 401(woocommerce_api_user_cannot_read_product) Error response: even when user has read/write privileges. but goes ahead to create the product in the database. Any help on this issue is highly appreciated
require_once( 'lib/woocommerce-api.php' );
$options = array(
'debug' => true,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
$client = new WC_API_Client( $the_url, 'ck_xxxx', 'cs_xxxx', $options);
Try adding into your $options array:
$options['query_string_auth'] = true;
As noted in the documentation this will "Force Basic Authentication as query string true" in other words it will append your consumer key and consumer secret to your request URL as a query string. This is only supported on HTTPS.

Silex token authentication

I create my first app with silex. Only logged in users can use the app. In the first page i create a login form, so the user can authenticate. My security provider look like:
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'secure_area_edison' => array(
'pattern' => '^/admin/',
'form' => array('login_path' => '/', 'check_path' => '/admin/login_check'),
'logout' => array('logout_path' => '/admin/logout', 'invalidate_session' => true),
'users' => function () use ($app) {
return new App\Services\UserProvider($app['db']);
},
),
)
));
Every url after '/admin' require that the user was successfull authenticated. Everything works fine and now i want to extend my app with an API. I create a new controller which retrieves data from database and return a JSON reponse, this work also fine.
But how can the user authenticate for this API? Should i create a new column in my user table like "hash" or "token"? Users which will retrieve the JSON Response must send the token in every get request, is this the correct way?
The url can look:
/admin/api/allProducts/token/<TOKEN>
you should use token base authentication instead of passing token in every get request.
refer : https://github.com/thcolin/silex-simpleuser-jwt

PayPal in Symfony2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to use JMSPaymentCoreBundle with JMSPaymentPaypalBundle and I can't find a clear example anywhere on how to do it.
I've done all steps specified in the documentation and I'm not able to get it working. Can anybody help me please?
Payum bundle supports jms payments via the bridge. The links describes how to get started.
Usage of the bundle gives you several advantages:
Secured capture action.
Have credit card form, can ask user for credit card
Ability to easy setup IPN. Notify action is also secured.
Built-in support of all omnipay gateways (25 +), jms plugins (+ 10) and payum native libs.
Payum paypal lib supports recurring payment and digital goods out of the box.
Storages integrated into payment process so you do not have worry about data that might be lost.
Domain friendly. Indeed Payum provide some models but it does not restrict you to use them.
It already supports PSR-0 logger. In dev it logs executed payum actions, to easy debug (Visit symfony profile logs tab).
It is possible setup several payments (one paypal account for EU and one for US for example)
Extremely customizable. Add your custom payum actions, or extensions, or storages.
There is a symfony sandbox (code|web) to help you to start.
P.S. It is not the full list of features.
The default way to create a payment instruction is through the jms_choose_payment_method form:
$form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
'amount' => 12.99,
'currency' => 'EUR',
'default_method' => 'payment_paypal', // Optional
'predefined_data' => array(
'paypal_express_checkout' => array(
'return_url' => $this->get('router')->generate('payment_complete', array(
'number' => $order->getOrderNumber(),
), true),
'cancel_url' => $this->get('router')->generate('payment_cancel', array(
'number' => $order->getOrderNumber(),
), true)
),
),
));
You can also create a payment instruction manually:
use JMS\Payment\CoreBundle\Entity\ExtendedData;
use JMS\Payment\CoreBundle\Entity\Payment;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException;
use JMS\Payment\CoreBundle\Plugin\Exception\Action\VisitUrl;
use JMS\Payment\CoreBundle\Entity\PaymentInstruction;
$extendedData = new ExtendedData();
$extendedData->set('return_url', $this->get('router')->generate('payment_complete', array(
'number' => $order->getOrderNumber(),
), true));
$extendedData->set('cancel_url', $this->get('router')->generate('payment_cancel', array(
'number' => $order->getOrderNumber(),
), true));
$instruction = new PaymentInstruction((float)$order->getCharge() > 0 ? $order->getCharge() : $order->getAmount(), 'EUR', 'paypal_express_checkout', $extendedData);
$this->get('payment.plugin_controller')->createPaymentInstruction($instruction);
$order->setPaymentInstruction($instruction);
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($order);
$em->flush();
My payment_complete route looks like:
public function completeAction(Booking $order)
{
$instruction = $order->getPaymentInstruction();
if (($instruction->getAmount() - $instruction->getDepositedAmount()) > 0) {
if (null === $pendingTransaction = $instruction->getPendingTransaction()) {
$payment = $this->get('payment.plugin_controller')->createPayment($instruction->getId(), $instruction->getAmount() - $instruction->getDepositedAmount());
} else {
$payment = $pendingTransaction->getPayment();
}
$result = $this->get('payment.plugin_controller')->approveAndDeposit($payment->getId(), $payment->getTargetAmount());
if (Result::STATUS_PENDING === $result->getStatus()) {
$ex = $result->getPluginException();
if ($ex instanceof ActionRequiredException) {
$action = $ex->getAction();
if ($action instanceof VisitUrl) {
return new RedirectResponse($action->getUrl());
}
throw $ex;
}
} else if (Result::STATUS_SUCCESS !== $result->getStatus()) {
throw new \RuntimeException('Transaction was not successful: '.$result->getReasonCode());
}
}
$order->setTransactionAmount((float)$order->getAmount());
$creditPurchased = (float)$order->getCharge() > (float)$order->getAmount() ? (float)$order->getCharge() - (float)$order->getAmount() : 0;
$em->persist($order);
$em->flush();
I've got it running going through http://jmsyst.com/bundles/JMSPaymentCoreBundle/master/usage

Silex: Set firewalls session lifetime

I'm trying to extend the default lifetime once a user logs in. For the login I'm using the security service provider as follows:
$app = $this->_app;
$this->_app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'pattern' => '^.*$',
'anonymous' => true, // Needed as the login path is under the secured area
'form' => array('login_path' => '/signup/', 'check_path' => 'login_check', 'failure_path' => 'login_failure'),
'logout' => array('logout_path' => '/logout/'), // url to call for logging out
'users' => $this->_app->share(function() use ($app)
{
// Specific class App\User\UserProvider is described below
return new UserProvider($app['db']);
}),
),
),
'security.access_rules' => array(
array('^/restricted/$', 'ROLE_USER'),
)
));
I've tried setting up with the sessions lifetime (cookie) like this:
$this->_app->register(new Silex\Provider\SessionServiceProvider(), array(
'session.storage.options' => array('cookie_lifetime' => (60 * 60 * 12)), // 12 hours
));
But still nothing. Session removes itself after like 15 minutes or so.
How can I extend the login security firewall lifetime to 12 hours?
I think I finally got it working:
Saving sessions in database seemed to solve the problem.
SQL:
CREATE TABLE `session` (
`session_id` varchar(255) NOT NULL,
`session_value` text NOT NULL,
`session_time` int(11) NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
PHP:
/* SESSION IN DB */
$this->_app->register(new Silex\Provider\SessionServiceProvider());
$this->_app['session.db_options'] = array(
'db_table' => 'session',
'db_id_col' => 'session_id',
'db_data_col' => 'session_value',
'db_time_col' => 'session_time',
);
$this->_app['session.storage.handler'] = $this->_app->share(function ()
{
return new PdoSessionHandler(
$this->_app['db']->getWrappedConnection(), $this->_app['session.db_options'], $this->_app['session.storage.options']
);
});
Here is a solution if you don't want to store sessions in DB: just increase session.gc_maxlifetime in php.ini.
When sessions are being stored in files, they (by default) get put to /var/lib/php/sessions/ directory. It's clear that this directory has to be cleared from time to time. In order to achieve this, there is a cron job configured at /etc/cron.d/php5 that fires script /usr/lib/php5/sessionclean every 30 minutes. This script takes php config and gets session.gc_maxlifetime from there and then removes files that are older than what is specified in this variable.
The problem is: by default session.gc_maxlifetime equals to 1440 seconds or 24 minutes. You can increase it to whatever value suits you, for instance to 24 hours (and limit your sessions by session cookie lifetime).

Zend_Service_Twitter & Zend_Oauth_Token_Access behind proxy?

I'm currently stuck with some of Zend's methods, im trying to make a simple Zend_Service_Twitter request through a proxy, however i keep getting:
Unable to Connect to tcp://api.twitter.com:80. Error #0:
php_network_getaddresses: gethostbyname failed.
I am able to do http calls with the Zend_Http_Client library by itself, so I believe my problem is with the code where I pass the httpClient instance to the Zend_Service_Twitter... But enough rant i guess, basically I have the following:
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Proxy',
'proxy_host' => self::PROXY_HOST,
'proxy_port' => self::PROXY_PORT,
'timeout' => 240,
);
$httpClient = new Zend_Http_Client(self::TWITTER_API_URL, $config);
$token = new Zend_Oauth_Token_Access;
$token->setParams(array(
Zend_Oauth_Token_Access::TOKEN_PARAM_KEY => self::TWITTER_OAUTH_TOKEN,
Zend_Oauth_Token_Access::TOKEN_SECRET_PARAM_KEY => self::TWITTER_OAUTH_TOKEN_SECRET
));
$twitter = new Zend_Service_Twitter(array(
'username' => 'MYUSERNAME',
'accessToken' => $token
));
$twitter->getHttpClient($httpClient);
$response = $twitter->account->rateLimitStatus();
Any pointers would be appreciated!
While taking a closer look at the Zend_Service_Twitter class, all you need to do in order to set up the proxy parameters is this:
$twitter = new Zend_Service_Twitter(array(
'username' => 'MYUSERNAME',
'accessToken' => $token
));
$twitter->setLocalHttpClient($twitter->getHttpClient($httpClient));
($httpClient being an instance of Zend_Http_Client which contains your proxy configuration)

Resources