Skip to content

Commit

Permalink
integration completed
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Feb 28, 2024
1 parent 047155e commit 161a667
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
37 changes: 19 additions & 18 deletions app/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ abstract class AbstractGateway extends \Learndash_Payment_Gateway
*/
protected static string $title;

/**
* @var string
*/
protected static string $ldPath;

/**
* @var string
*/
Expand Down Expand Up @@ -194,10 +189,16 @@ public function setup_payment(): void
$paymentIntentData = null;
$subscriptionData = null;

if ($product->is_price_type_paynow()) {
$paymentIntentData = $this->get_order_data($coursePrice, $product);
} elseif ($product->is_price_type_subscribe()) {
$subscriptionData = $this->get_subscription_data($coursePrice, $productPricing, $product);
try {
if ($product->is_price_type_paynow()) {
$paymentIntentData = $this->get_order_data($coursePrice, $product);
} elseif ($product->is_price_type_subscribe()) {
$subscriptionData = $this->get_subscription_data($coursePrice, $productPricing, $product);
}
} catch (\Throwable $th) {
wp_send_json_error([
'msg' => $th->getMessage()
]);
}

wp_send_json_success($paymentIntentData ?? $subscriptionData);
Expand All @@ -212,12 +213,12 @@ private function get_order_data(float $amount, Product $product): array
{
$transactionMetaDto = \Learndash_Transaction_Meta_DTO::create(
array(
Transaction::$meta_key_gateway_name => $this::get_name(),
Transaction::$meta_key_gateway_name => self::get_name(),
Transaction::$meta_key_price_type => LEARNDASH_PRICE_TYPE_PAYNOW,
Transaction::$meta_key_pricing_info => \Learndash_Pricing_DTO::create(
array(
'currency' => $this->currency_code,
'price' => $amount,
'price' => number_format($amount / 100, 2, '.', ''),
)
),
)
Expand Down Expand Up @@ -254,16 +255,16 @@ function ($value) {
* @param \Learndash_Pricing_DTO $pricing Pricing DTO.
* @param Product $product Product.
*
* @throws Exception Exception.
* @throws \Exception Exception.
*
* @return array{metadata: array<mixed>, items: array<mixed>|null, payment_data: array<mixed>}
* @return array<string,mixed>
*/
private function get_subscription_data(float $amount, \Learndash_Pricing_DTO $pricing, Product $product): array
{
if (empty($pricing->duration_length)) {
throw new Exception(__('The Billing Cycle Interval value must be set.', 'learndash'));
throw new \Exception(__('The Billing Cycle Interval value must be set.', 'learndash'));
} elseif (0 === $pricing->duration_value) {
throw new Exception(__('The minimum Billing Cycle value is 1.', 'learndash'));
throw new \Exception(__('The minimum Billing Cycle value is 1.', 'learndash'));
}

$trialDurationInDays = $this->map_trial_duration_in_days(
Expand All @@ -276,7 +277,7 @@ private function get_subscription_data(float $amount, \Learndash_Pricing_DTO $pr

$transactionMetaDto = \Learndash_Transaction_Meta_DTO::create(
array(
Transaction::$meta_key_gateway_name => $this::get_name(),
Transaction::$meta_key_gateway_name => self::get_name(),
Transaction::$meta_key_price_type => LEARNDASH_PRICE_TYPE_SUBSCRIBE,
Transaction::$meta_key_pricing_info => $pricing,
Transaction::$meta_key_has_trial => $hasTrial,
Expand Down Expand Up @@ -339,7 +340,7 @@ private function map_trial_duration_in_days(int $durationValue, string $duration
public function map_payment_button_markup(array $params, \WP_Post $post): string
{
$buttonLabel = $this->map_payment_button_label(
static::$title,
self::get_label(),
$post
);

Expand All @@ -355,7 +356,7 @@ public function map_payment_button_markup(array $params, \WP_Post $post): string
'productId' => $product->get_id()
];

$button = '<div class="' . esc_attr($this->get_form_class_name()) . '"><button class="ldlms-cp-btn" type="button" data-name="' . esc_attr(static::$name) . '" data-json="\'' . esc_attr(json_encode($jsonData)) . '\'">' . esc_attr($buttonLabel) . '</button></div>';
$button = '<div class="' . esc_attr($this->get_form_class_name()) . '"><button class="ldlms-cp-btn" type="button" data-name="' . esc_attr(self::get_name()) . '" data-json="\'' . esc_attr(json_encode($jsonData)) . '\'">' . esc_attr($buttonLabel) . '</button></div>';

return $button;
}
Expand Down
6 changes: 2 additions & 4 deletions app/Gateways/GatewayLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

class GatewayLite extends AbstractGateway
{
protected static string $name = 'cryptopay_lite';
public static string $name = 'cryptopay_lite';

protected static string $title = 'CryptoPay Lite';

protected static string $ldPath = 'ld_cryptopay_lite';
public static string $title = 'CryptoPay Lite';

/**
* Constructor
Expand Down
6 changes: 2 additions & 4 deletions app/Gateways/GatewayPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

class GatewayPro extends AbstractGateway
{
protected static string $name = 'cryptopay';
public static string $name = 'cryptopay';

protected static string $title = 'CryptoPay';

protected static string $ldPath = 'ld_cryptopay';
public static string $title = 'CryptoPay';

/**
* Constructor
Expand Down
6 changes: 3 additions & 3 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
cryptoPayLiteProcess(response.data);
}
} else {
helpers.errorPopup(response.msg);
helpers.errorPopup(response.data.msg);
}
},
error: (error) => {
console.log(error);
if (error?.response?.msg) {
helpers.errorPopup(error.response.msg);
if (error?.response?.data?.msg) {
helpers.errorPopup(error.response.data.msg);
} else {
alert(error.responseText);
}
Expand Down

0 comments on commit 161a667

Please sign in to comment.