|
|
@@ -0,0 +1,148 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Longyi\Pay\Afterpay\Payment;
|
|
|
+
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+use Webkul\Payment\Payment\Payment;
|
|
|
+use Webkul\Sales\Models\Order;
|
|
|
+use Webkul\Sales\Repositories\OrderRepository;
|
|
|
+use Webkul\Checkout\Facades\Cart;
|
|
|
+use function Symfony\Component\Translation\t;
|
|
|
+
|
|
|
+class Clearpay extends Afterpay
|
|
|
+{
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ $this->clientId = $this->getConfigData('client_id');
|
|
|
+ $this->apikey = $this->getConfigData('secret_id');
|
|
|
+ $this->secret = $this->getConfigData('webhook_id');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected $code = 'clearpay';
|
|
|
+
|
|
|
+ public function createGatewayOrder()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return $this->createPayment(
|
|
|
+ $this->getOrder(),
|
|
|
+ ['merchantOrderId' => $this->getOrder()->increment_id],
|
|
|
+ $this->getInput()
|
|
|
+ );
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new \Exception($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function createPayment($order, $override, $input = null)
|
|
|
+ {
|
|
|
+ $shop = $order->shipping_address;
|
|
|
+ $bill = $order->billing_address;
|
|
|
+ $telephone = $shop->phone ?: $bill->phone;
|
|
|
+ $grandTotal = round($order->grand_total, 2);
|
|
|
+ foreach ($order->items as $item) {
|
|
|
+ $products[] = [
|
|
|
+ 'name' => (string)$item->name,
|
|
|
+ 'quantity' => $item->qty_ordered,
|
|
|
+ 'sku' => $item->sku,
|
|
|
+ 'price' => [
|
|
|
+ 'amount' => $this->formatCurrencyValue((float) $item->price),
|
|
|
+ 'currency' => $order->order_currency_code
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'purchaseCountry' => $shop->country,
|
|
|
+ 'amount' => [
|
|
|
+ 'amount' => $grandTotal,
|
|
|
+ 'currency' => $order->order_currency_code
|
|
|
+ ],
|
|
|
+ 'consumer' => [
|
|
|
+ 'email' => $order->customer_email,
|
|
|
+ 'givenNames' => $order->customer_first_name,
|
|
|
+ 'surname' => $order->customer_last_name,
|
|
|
+ 'phoneNumber' => $telephone
|
|
|
+ ],
|
|
|
+ 'merchantReference' => $override['merchantOrderId'],
|
|
|
+ 'billing' => [
|
|
|
+ 'name' => $bill->first_name . ' ' .$bill->last_name,
|
|
|
+ 'line1' => $bill->address,
|
|
|
+ 'area1' => $bill->state,
|
|
|
+ 'area2' => $bill->city,
|
|
|
+ 'region' => $bill->state,
|
|
|
+ 'postcode' => $bill->postcode,
|
|
|
+ 'phoneNumber' => $bill->phone,
|
|
|
+ 'countryCode' => $bill->country,
|
|
|
+ ],
|
|
|
+ 'shipping' => [
|
|
|
+ 'name' => $shop->first_name . ' ' .$shop->last_name,
|
|
|
+ 'line1' => $shop->address,
|
|
|
+ 'area1' => $shop->state,
|
|
|
+ 'area2' => $shop->city,
|
|
|
+ 'region' => $shop->state,
|
|
|
+ 'postcode' => $shop->postcode,
|
|
|
+ 'phoneNumber' => $shop->phone,
|
|
|
+ 'countryCode' => $shop->country,
|
|
|
+ ],
|
|
|
+ 'merchant' => [
|
|
|
+ 'redirectConfirmUrl' => $input->paymentSuccessUrl . '?orderId=' . $order->id,
|
|
|
+ 'redirectCancelUrl' => $input->paymentCancelUrl. '?orderId=' . $order->id
|
|
|
+ ],
|
|
|
+ 'items' => $products
|
|
|
+ ];
|
|
|
+ $client = new Client();
|
|
|
+ $response = $client->request('POST', $this->createPaymentUlr, [
|
|
|
+ 'headers' => [
|
|
|
+ 'Accept' => 'application/json',
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ 'Authorization' => 'Basic '.base64_encode("{$this->clientId}:{$this->apikey}")
|
|
|
+ ],
|
|
|
+ 'json' => $data,
|
|
|
+ 'timeout' => 30,
|
|
|
+ 'verify' => false
|
|
|
+ ]);
|
|
|
+ $responseBody = $response->getBody()->getContents();
|
|
|
+ Log::channel('payment')->info('Clearpay createPayment response', [
|
|
|
+ 'order_id' => $order->increment_id ?? null,
|
|
|
+ 'status' => $response->getStatusCode(),
|
|
|
+ 'body' => $responseBody,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $resultObject = json_decode($responseBody);
|
|
|
+ if (empty($resultObject->token)) {
|
|
|
+ throw new \Exception($resultObject->message);
|
|
|
+ } else {
|
|
|
+ return $resultObject->token;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getCountryOptions(): array
|
|
|
+ {
|
|
|
+ return core()->countries()->map(function ($country) {
|
|
|
+ return [
|
|
|
+ 'title' => $country->name,
|
|
|
+ 'value' => $country->code,
|
|
|
+ ];
|
|
|
+ })->toArray();
|
|
|
+ }
|
|
|
+ public function isAvailable($isShow = true)
|
|
|
+ {
|
|
|
+ if (! parent::isAvailable($isShow)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $info = $this->getOrder() ?: Cart::getCart();
|
|
|
+ if ($info && $billingCountry = $info->billing_address->country ?? false) {
|
|
|
+// $allowedCountries = $this->getConfigData('allowed_countries');
|
|
|
+// if (empty($allowedCountries)) {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// $allowedCountries = explode(',', $allowedCountries);
|
|
|
+// return in_array($billingCountry, $allowedCountries);
|
|
|
+ $allowedCountries = ['GB'];//英国
|
|
|
+ return in_array($billingCountry, $allowedCountries);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|