| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace Longyi\Pay\Airwallex\Payment;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Storage;
- use Webkul\Checkout\Facades\Cart;
- use Webkul\Payment\Payment\Payment;
- use Webkul\Sales\Models\Order;
- use Webkul\Sales\Repositories\OrderRepository;
- class Airwallex extends Payment
- {
- /**
- * Payment method code
- *
- * @var string
- */
- protected $code = 'airwallex';
- protected $clientId;
- protected $apikey;
- protected $secret;
- protected $currentOrder;
- protected $prefix = 'AS';
- protected $tokenCacheKey = 'airwallex_airwallex_token';
- protected $tokenUrl = 'https://api-demo.airwallex.com/api/v1/authentication/login';
- protected $createPaymentUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/create';
- protected $confirmIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/{id}/confirm';
- protected $captureIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/{id}/capture';
- protected $paymentIntentsUlr = 'https://api-demo.airwallex.com/api/v1/pa/payment_intents/';
- public function __construct()
- {
- if ($this->getConfigData('mode') == 'live') {
- $this->tokenUrl = 'https://api.airwallex.com/api/v1/authentication/login';
- $this->createPaymentUlr = 'https://api.airwallex.com/api/v1/pa/payment_intents/create';
- $this->confirmIntentsUlr = 'https://api.airwallex.com/api/v1/pa/payment_intents/{id}/confirm';
- $this->captureIntentsUlr = 'https://api.airwallex.com/api/v1/pa/payment_intents/{id}/capture';
- $this->paymentIntentsUlr = 'https://api.airwallex.com/api/v1/pa/payment_intents/';
- }
- $this->clientId = $this->getConfigData('client_id');
- $this->apikey = $this->getConfigData('secret_id');
- $this->secret = $this->getConfigData('webhook_id');
- }
- /**
- * Get redirect url.
- */
- public function getRedirectUrl()
- {
- }
- public function setOrder($order = null): self
- {
- $this->currentOrder = $order;
- return $this;
- }
- public function getOrder()
- {
- return $this->currentOrder;
- }
- public function createGatewayOrder()
- {
- return $this->createIntents($this->getOrder(), ['merchantOrderId' => $this->getOrder()->increment_id]);
- }
- public function createIntents($order, $override)
- {
- $shop = $order->shipping_address;
- $products = [];
- foreach ($order->items as $item) {
- $data = [
- 'name' => $item->name,
- 'desc' => $item->name,
- 'unit_price' => (float) $item->price,
- 'sku' => $item->sku,
- 'quantity' => $item->qty_ordered
- ];
- array_push($products, $data);
- }
- $grandTotal = round($order->grand_total, 2);
- $data = [
- 'request_id' => $this->createUuid(),
- 'amount' => $grandTotal,
- 'descriptor' => $this->prefix,
- 'merchant_order_id' => $this->prefix . $override['merchantOrderId'],
- 'currency' => $order->order_currency_code,
- 'customer' => [
- 'email' => $order->customer_email,
- 'first_name' => $order->customer_first_name,
- 'last_name' => $order->customer_last_name
- ],
- 'order' => [
- 'products' => $products,
- 'shipping' => [
- 'address' => [
- 'city' => $shop->city,
- 'postcode' => $shop->postcode,
- 'state' => $shop->state,
- 'country_code'=> $shop->country,
- 'street' => $shop->address,
- ],
- 'first_name' => $shop->first_name,
- 'last_name' => $shop->last_name,
- 'phone_number' => $shop->phone,
- 'email' => $shop->email
- ],
- ],
- ];
- $client = new Client();
- $response = $client->request('POST', $this->createPaymentUlr, [
- 'headers' => [
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'Authorization' => 'Bearer '.$this->getToken()
- ],
- 'json' => $data,
- 'timeout' => 30,
- 'verify' => false
- ]);
- $resultObject = json_decode($response->getBody()->getContents());
- if ($resultObject->status != 'SUCCESS') {
- throw new \Exception('airwallex api is error');
- }
- $id = $resultObject->id;
- $client_secret = $resultObject->client_secret;
- return $id . ':' . $client_secret;
- }
- public function getToken()
- {
- $token = cache()->get($this->tokenCacheKey);
- if (!$token) {
- $client = new Client();
- $response = $client->request('POST', $this->tokenUrl, [
- 'headers' => [
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'x-client-id' => $this->clientId,
- 'x-api-key' => $this->apikey
- ],
- 'timeout' => 30,
- 'verify' => false
- ]);
- $result = json_decode($response->getBody()->getContents());
- $token = $result->token;
- cache()->put($this->tokenCacheKey, $token, 20);
- }
- return $token;
- }
- //成功修改状态
- public function payments_webhook_payment_intent_succeeded($orderId, $transactionId)
- {
- $order = app(OrderRepository::class)->find($orderId);
- if (!$order) {
- return false;
- }
- if ($order->status != Order::STATUS_PENDING) {
- return false;
- }
- $order->status = Order::STATUS_PROCESSING;
- $order->save();
- if ($order->payment) {
- $order->payment->update([
- 'transaction_id' => $transactionId,
- ]);
- }
- }
- //授权成功修改状态
- public function payments_webhook_payment_intent_requires_capture($orderId, $transactionId)
- {
- $order = app(OrderRepository::class)->find($orderId);
- if (!$order) {
- return false;
- }
- if ($order->status != Order::STATUS_PENDING) {
- return false;
- }
- $order->status = Order::STATUS_PROCESSING;
- $order->save();
- if ($order->payment) {
- $order->payment->update([
- 'transaction_id' => $transactionId,
- ]);
- }
- }
- /**
- * 获取一个唯一的id
- */
- public function createUuid($prefix = "")
- {
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr($chars, 0, 8) . '-'
- . substr($chars, 8, 4) . '-'
- . substr($chars, 12, 4) . '-'
- . substr($chars, 16, 4) . '-'
- . substr($chars, 20, 12);
- return $prefix . $uuid;
- }
- public function checkSignature($data, $exp)
- {
- if (empty($data)) {
- return false;
- }
- if (hash_hmac('sha256', $data['timestamp'].$data['content'], $this->$exp) != $data['signature']) {
- return false;
- }
- return true;
- }
- public function getPrefix()
- {
- return $this->prefix;
- }
- /**
- * Get payment method image.
- *
- * @return array
- */
- public function getImage()
- {
- $url = $this->getConfigData('image');
- return $url ? Storage::url($url) : bagisto_asset('images/cash-on-delivery.png', 'shop');
- }
- public function isAvailable()
- {
- if (! parent::isAvailable()) {
- return false;
- }
- $cart = Cart::getCart();
- $billingCountry = $cart->billing_address->country;
- $allowedCountries = ['AU', 'CA', 'NZ', 'GB', 'US'];
- return in_array($billingCountry, $allowedCountries);
- }
- }
|