I started using Docker recently and there was one thing I didn’t understand – initially.
So, when running your Dockerfile if you don’t have a CMD command the container will continue to run. However, when you include CMD command the container will exit unless you run one of your programs in the foreground. So, you’ll have to do something like this:
CMD bash -c "apachectl -D FOREGROUND"
if you are running Apache, of course, that will be different if you are running different software.
Another thing is that the CMD is where you want to run you dependencies installation, like PHP’s composer intall and NOT in the RUN commands. Here is how my CMD looked like in the end:
Today, I wanted to get a fresh Laravel 8 install on a project I started recently, basically restarting, as I needed the teams functionality of Jetstream and you can’t add the teams functionality into a project which already has Jetstream. So, I tried creating a fresh Laravel install and copying the files over to my project but for some reason the MySQL container didn’t get installed, which was confusing at first. Anyway, I ended up deleting the ‘vendor’ folder and installing everything a new with composer and then running:
phpartisansail:install
then I run
./vendor/bin/sailbuild--no-cache
and then just
./vendor/bin/sailup
this time MySQL worked and I could run my migrations.
I am sharing this in case someone else encounters a similar problem.
I was using the free version of Advanced Custom Fields (ACF) when I needed a way to make a gallery field where you select some images, order them and it displays as a gallery on a certain page and that’s impossible with the free version of ACF. I couldn’t pay for the Pro version of ACF either, so I looked for an alternative and I found Custom Field Suite. It has a good amount of the basic fields plus a repeatable/loop field which lets you repeat groups of the basic fields, incredibly handy!
When developing PHP applications sometimes I try to use the installed on my system ‘phpunit’ binary but I often get errors with it; however when using the ‘vendor/bin/phpunit’ that comes with my dependencies it works like a charm.
Recently, I’ve been trying to integrate PayPal subscriptions into a website of mine but I’ve hit a few problems which I’ve managed to resolve thanks to PayPal’s support. Hopefully this will help others. Here we go.
I followed this integration guide and I’ve managed to create a product and a plan and then subscription and then use the link from the subscription to redirect to PayPal. However, when I tried to pay for the subscription using my sandbox account I got this error:
We're sorry, we couldn't set up your subscription using the funding source you've selected. Please try another funding source.
I was baffled for a while but then I contacted PayPal’s support which told me that I don’t have balance in the same currency in which was the subscription (my case was euro) so I went on this page and I created a new sandbox account based in Germany which automatically filled the account with 5000 euro. However, when I tried to pay again for the subscription using the new account I hit another similar problem, here is the error:
Sorry, we couldn't set up your subscription using the payment method you selected. Please try another payment method.
So I contacted PayPal’s support again and after some back and forth they told me that the sandbox account wasn’t verified and that they’ve verified it and now it was working, which I tested immediately and yes it was working.
Problem is, why sandbox accounts with automatically generated email addresses, which look something like this: [email protected], have to be verified at all and how can you do that if you don’t own the email address? Baffling. Even worse, I have a sandbox account set up with one of my email addresses, which is not verified, and I tried to verify it but I never got a verification email!
So, generally, I am quite disappointed by PayPal even though I’ve managed to solved my problem. Lack of documentation is frustrating. I even found pieces of their documentation that simply don’t work.
Recently, I started trying to integrate PayPal subsriptions into a project of mine, however, it turns out I’ve started by using the old PayPal PHP SDK which is not the recommended way anymore. The recommended way is using the new Checkout PHP SDK, however that one doesn’t support subscriptions so the way to integrate PayPal subscriptions is by making direct https calls to their API. In this post I’ll show you how I started integrating the subscriptions using the old, deprecated SDK in case somebody needs it. Note that it’s not full integration (I’ve only gotten to a certain point) and it’s also Laravel specific.
<?phpnamespaceApp\Logic\Paypal;useException;useApp\Setting;usePayPal\Api\Currency;usePayPal\Api\MerchantPreferences;usePayPal\Api\PaymentDefinition;usePayPal\Api\Plan;usePayPal\Api\Patch;usePayPal\Api\PatchRequest;usePayPal\Api\VerifyWebhookSignature;usePayPal\Api\Webhook;usePayPal\Auth\OAuthTokenCredential;usePayPal\Common\PayPalModel;usePayPal\Api\Agreement;usePayPal\Api\Payer;usePayPal\Rest\ApiContext;classPaypal {public $apiContext;publicfunction__construct() {$this->apiContext =newApiContext(newOAuthTokenCredential(config('services.paypal.client_id'), // ClientIDconfig('services.paypal.secret') ) ); }publicfunctioncreatePlan() { $monthlyAmount =10; $currency ='EUR';// Create a new instance of Plan object $plan =newPlan();// # Basic Information// Fill up the basic information that is required for the plan $plan->setName('Epic Plan')->setDescription('Unlimited form submissions. €10 paid monthly.')->setType('infinite');// # Payment definitions for this billing plan. $paymentDefinition =newPaymentDefinition(); $paymentDefinition->setName('Regular Payments')->setType('REGULAR')->setFrequency('Day')->setFrequencyInterval("1")->setCycles("0")->setAmount(newCurrency(array('value'=> $monthlyAmount, 'currency'=> $currency))); $merchantPreferences =newMerchantPreferences(); $baseUrl =env('APP_URL'); $merchantPreferences->setReturnUrl("$baseUrl/paypal/execute-agreement?success=true")->setCancelUrl("$baseUrl/paypal/execute-agreement?success=false")->setAutoBillAmount("yes")->setInitialFailAmountAction("CONTINUE")->setMaxFailAttempts("0")->setSetupFee(newCurrency(array('value'=> $monthlyAmount, 'currency'=> $currency))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences);// ### Create Plan $output = $plan->create($this->apiContext);return $output; }publicfunctionactivatePlan() { $createdPlan =$this->createPlan(); $patch =newPatch(); $value =newPayPalModel('{ "state":"ACTIVE" }'); $patch->setOp('replace')->setPath('/')->setValue($value); $patchRequest =newPatchRequest(); $patchRequest->addPatch($patch); $createdPlan->update($patchRequest, $this->apiContext); $plan =Plan::get($createdPlan->getId(), $this->apiContext);// try to find previous setting with plan id $setting =Setting::where('name', '=', 'active_plan_id')->first();// save the plan id in the databaseif ($setting) { $setting->value = $plan->getId(); $setting->save(); } else { $newSetting =newSetting( ['name'=>'active_plan_id','value'=> $plan->getId(), ] ); $newSetting->save(); }return $plan; }publicfunctioncreateBillingAgreement() { $setting =Setting::where('name', '=', 'active_plan_id')->first();if (! $setting) {thrownewException('Active plan id not found.'); } $activePlanId = $setting->value; $agreement =newAgreement(); $dateAgreementStarts =gmdate("Y-m-d\TH:i:s\Z",time()+120); $agreement->setName('Base Agreement')->setDescription('€10 a month in return for accessing our unlimited plan.')->setStartDate($dateAgreementStarts);// Add Plan ID// Please note that the plan Id should be only set in this case. $plan =newPlan(); $plan->setId($activePlanId); $agreement->setPlan($plan);// Add Payer $payer =newPayer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer);// ### Create Agreement// Please note that as the agreement has not yet activated, we wont be receiving the ID just yet. $agreement = $agreement->create($this->apiContext);// ### Get redirect url $approvalUrl = $agreement->getApprovalLink();returnredirect($approvalUrl); }/** * Get list of all webhooks * * @return\PayPal\Api\WebhookList */publicfunctionlistAllWebhooks() { $output =Webhook::getAllWithParams([], $this->apiContext);return $output; }/** * Delete all webhooks * * @returnbool */publicfunctiondeleteAllWebhooks() { $webhookList =$this->listAllWebhooks();foreach ($webhookList->getWebhooks() as $webhook) { $webhook->delete($this->apiContext); }returntrue; }/** * Register primary webhooks * * @returnWebhook|string */publicfunctionregisterPrimaryWebhooks() { $setting =Setting::where('name', '=', 'primary_webhooks_registered')->first();if ($setting) {return'primary webhooks already registered'; } $baseUrl =env('APP_URL'); $webhook =newWebhook(); $webhook->setUrl("$baseUrl/paypal/webhooks/primary");// # Event Types// Event types correspond to what kind of notifications you want to receive on the given URL. $webhookEventTypes =array(); $webhookEventTypes[] =new\PayPal\Api\WebhookEventType('{ "name":"PAYMENT.AUTHORIZATION.CREATED" }' ); $webhookEventTypes[] =new\PayPal\Api\WebhookEventType('{ "name":"PAYMENT.AUTHORIZATION.VOIDED" }' ); $webhook->setEventTypes($webhookEventTypes);// ### Create Webhook $output = $webhook->create($this->apiContext); $newSetting =newSetting( ['name'=>'primary_webhooks_registered','value'=> $output->getId(), ] ); $newSetting->save();return $output; }publicfunctionvalidatePrimaryWebhooks() { $setting =Setting::where('name', '=', 'primary_webhooks_registered')->first();if (! $setting) {thrownewException('Primary webhooks not registered.'); } $webhookId = $setting->value; $requestBody =file_get_contents('php://input'); $headers =getallheaders(); $headers =array_change_key_case($headers,CASE_UPPER); $signatureVerification =newVerifyWebhookSignature(); $signatureVerification->setAuthAlgo($headers['PAYPAL-AUTH-ALGO']); $signatureVerification->setTransmissionId($headers['PAYPAL-TRANSMISSION-ID']); $signatureVerification->setCertUrl($headers['PAYPAL-CERT-URL']); $signatureVerification->setWebhookId($webhookId); $signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']); $signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']); $signatureVerification->setRequestBody($requestBody); $output = $signatureVerification->post($this->apiContext); $status = $output->getVerificationStatus(); (newSetting( ['name'=>'webhook '.date('Y-m-d H:i:s'),'value'=> $status . $requestBody, ] ))->save();returnresponse()->json(['status'=>'OK' ]); }}
Here is the code. I’ll explain the four main methods in the class. First, ‘activatePlan’ – this will create the billing plan and activate it. Second, ‘registerPrimaryWebhooks’ – which will register some webhooks with PayPal. Third, ‘validatePrimaryWebhooks’ – which will validate/handle the primary webhooks, when one is received. And last, ‘createBillingAgreement’ – which will create a billing agreement and return a redirect to the PayPal’s approval link for that agreement.
Here is also my ‘PaypalController’ which handles the requests and also has some PayPal logic to execute the billing agreement once the user is redirected to the success url after approval.
<?phpnamespaceApp\Http\Controllers;useApp\Setting;useApp\Logic\Paypal\Paypal;useIlluminate\Http\Request;usePayPal\Api\Agreement;classPaypalControllerextendsController{publicfunctioncreateAndActivatePlan() { $setting =Setting::where('name', '=', 'active_plan_id')--->first();if ($setting) {return'plan already activated'; } $paypal =newPaypal(); $plan = $paypal->activatePlan();if ($plan) {return'success'; } else {return'failure'; } }publicfunctionredirectToPaypal() { $paypal =newPaypal();return $paypal->createBillingAgreement(); }publicfunctionexecuteAgreement() {// ## Approval Status// Determine if the user accepted or denied the requestif (isset($_GET['success']) && $_GET['success'] =='true') { $paypal =newPaypal(); $token = $_GET['token']; $agreement =newAgreement();// ## Execute Agreement// Execute the agreement by passing in the token $agreement->execute($token, $paypal->apiContext);// ## Get Agreement// Make a get call to retrieve the executed agreement details $agreement =Agreement::get($agreement->getId(), $paypal->apiContext);dd($agreement); } }publicfunctionregisterPrimaryWebhooks() { $paypal =newPaypal();return $paypal->registerPrimaryWebhooks(); }publicfunctionhandlePrimaryWebhooks() { $paypal =newPaypal();return $paypal->validatePrimaryWebhooks(); }}
I just got an issue where the permalinks on a WordPress didn’t work even though I had an .htaccess file and I had selected “Post name” in the permalinks option page in WordPress.
The problem was to do with mod_rewrite not being enabled on my server. So I had to do “sudo a2enmod rewrite” and then restart Apache and the problem got fixed.
cd/path/to/dirshopt-snullglobforfilein*.{avi,rmvb,mkv}dodirectory="${file%.*}"# remove file extension directory="${directory//_/ }"# replace underscores with spacesdarr=( $directory )darr="${darr[@]^}"# capitalize the directory nameechomkdir-p--"$darr"# create the directory; echomv-b--"$file""$darr"# move the file to the directoryechodone