Squareup.com的自定义Woocommerce付款网关

穆罕默德·乌默(Mohammad Umer)

我正在为Woocommerce开发Square Connect付款网关。

这是给我嵌入作为网关的网关文件和信用卡表格。

https://docs.connect.squareup.com/articles/adding-payment-form/

现在,该过程为“付款表单返回卡现时”,用于从客户中扣除付款。

这是示例代码。

require_once 'SquareConnect/autoload.php';

# Assume you have assigned values to the following variables:
#   $nonce
#   $location_id
#   $access_token

$transaction_api = new \SquareConnect\Api\TransactionApi();

$request_body = array (

  "card_nonce" => $nonce,

  # Monetary amounts are specified in the smallest unit of the applicable currency.
  # This amount is in cents. It's also hard-coded for $1, which is not very useful.
  "amount_money" => array (
    "amount" => 100,
    "currency" => "USD"
  ),

  # Every payment you process for a given business have a unique idempotency key.
  # If you're unsure whether a particular payment succeeded, you can reattempt
  # it with the same idempotency key without worrying about double charging
  # the buyer.
  "idempotency_key" => uniqid()
);

# The SDK throws an exception if a Connect endpoint responds with anything besides 200 (success).
# This block catches any exceptions that occur from the request.
try {
  print_r($transaction_api->charge($access_token, $location_id, $request_body));  
} catch (Exception $e) {
  echo "Caught exception " . $e->getMessage();

问题是带有javascript函数的付款表单返回随机数。

这是我在插件中嵌入表单的方式。

function payment_fields(){
?>
<p  class="form-row form-row form-row-wide">
  <label>Card Number</label>
  <div id="sq-card-number"></div>
</p>
<p  class="form-row form-row form-row-wide">
  <label>CVV</label>
  <div id="sq-cvv"></div>
</p>
<p  class="form-row form-row form-row-wide">
  <label>Expiration Date</label>
  <div id="sq-expiration-date"></div>
</p>
<p  class="form-row form-row form-row-wide">
  <label>Postal Code</label>
  <div id="sq-postal-code"></div>
</p>
<input type="hidden" id="payment_nonce" value="" />
<script src="https://js.squareup.com/v2/paymentform" type="text/javascript"></script>
<script type="text/javascript">
var paymentForm = new SqPaymentForm({
    applicationId: 'sandbox-sq0idp-Yjm9KUoP_AiqDuGgwV6q4A', // <-- REQUIRED: Add Application ID
    inputClass: 'sq-input',
    inputStyles: [
      {
        fontSize: '15px'
      }
    ],
    cardNumber: {
      elementId: 'sq-card-number',
      placeholder: '.... .... .... ....'
    },
    cvv: {
      elementId: 'sq-cvv',
      placeholder: 'CVV'
    },
    expirationDate: {
      elementId: 'sq-expiration-date',
      placeholder: 'MM/YY'
    },
    postalCode: {
      elementId: 'sq-postal-code'
    },
    callbacks: {
      cardNonceResponseReceived: function(errors, nonce, cardData) {
        if (errors) {
          // handle errors
          errors.forEach(function(error) { console.log(error.message); });
        } else {
          // handle nonce
          console.log('Nonce received:');
          console.log(nonce);
        }
      },
      unsupportedBrowserDetected: function() {
        // Alert the buyer that their browser is not supported
      }
    }
  });
  function requestCardNonce() {
    paymentForm.requestCardNonce();
  }
paymentForm.build();
</script>
<?php
    }

这是随机数响应

console.log('Nonce receive:'); console.log(nonce);

现在不确定在woocomerce的处理付款部分如何获得此现时?

穆罕默德·乌默(Mohammad Umer)

好吧,我自己用ajax和session变量完成了它。

这是我的代码。

    function payment_fields(){
?>

    <p  class="form-row form-row form-row-wide">
      <label>Card Number</label>
      <div id="sq-card-number"></div>
    </p>
    <p  class="form-row form-row form-row-wide">
      <label>CVV</label>
      <div id="sq-cvv"></div>
    </p>
    <p  class="form-row form-row form-row-wide">

      <label>Expiration Date</label>
      <div id="sq-expiration-date"></div>
    </p>
    <p  class="form-row form-row form-row-wide">
      <label>Postal Code</label>
      <div id="sq-postal-code"></div>
    </p>
    <input type="hidden" id="payment_nonce" value="" />
    <script src="https://js.squareup.com/v2/paymentform" type="text/javascript"></script>
    <script type="text/javascript">

    var paymentForm = new SqPaymentForm({
        applicationId: '<?php echo $this->api_id; ?>', // <-- REQUIRED: Add Application ID
        inputClass: 'sq-input',
        inputStyles: [
          {
            fontSize: '15px'
          }
        ],
        cardNumber: {
          elementId: 'sq-card-number',
          placeholder: '.... .... .... ....'
        },
        cvv: {
          elementId: 'sq-cvv',
          placeholder: 'CVV'
        },
        expirationDate: {
          elementId: 'sq-expiration-date',
          placeholder: 'MM/YY'
        },
        postalCode: {
          elementId: 'sq-postal-code'
        },
        callbacks: {
          cardNonceResponseReceived: function(errors, nonce, cardData) {
            if (errors) {
              // handle errors
              errors.forEach(function(error) { console.log(error.message); });
            } else {
              // handle nonce
              console.log('Nonce received:');
              console.log(nonce);
                //localStorage.setItem('card_nonce',nonce);

                  var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
                      var data = {
                        'action': 'payment_nonce',
                        'nonce_recived': nonce,
                    };
                      jQuery.post(ajaxurl, data, function(response) {
                        });
            }
          },
          unsupportedBrowserDetected: function() {
            // Alert the buyer that their browser is not supported
          }
        }
      });

      function requestCardNonce() {
        paymentForm.requestCardNonce();
      }
    paymentForm.build();

    jQuery(document).ready(function ($) {

      $("form.woocommerce-checkout")
      .on('submit', function() { return requestCardNonce(); } ); 

    });

    </script>
    <style type="text/css">
        .sq-input {
          border: 1px solid rgb(223, 223, 223);
          outline-offset: -2px;
          margin-bottom: 5px;
        }
        .sq-input--focus {
          /* how your inputs should appear when they have focus */
          outline: 5px auto rgb(59, 153, 252);
        }
        .sq-input--error {
          /* how your inputs should appear when invalid */
          outline: 5px auto rgb(255, 97, 97);
        }
      </style>

    <?php
        }
        // Submit payment and handle response
        public function process_payment( $order_id ) {
            session_start();
            global $woocommerce;
            sleep(8);
            // Get this Order's information so that we know
            // who to charge and how much
                $customer_order = new WC_Order( $order_id );
                $amount = floatval( preg_replace( '#[^\d.]#', '', $customer_order->order_total ) ); 
               //$cart_total = number_format((int)$customer_order->order_total, 2, '.', ''); 
                $nonce = $_SESSION['nonce_recived']; 

                $access_token = $this->access_token;
                $location_api = new \SquareConnect\Api\LocationApi();
                $location_id = $location_api->listLocations($access_token); 
                $location_arr = json_decode ($location_id);

                $location_id = $location_arr->locations[0]->id; 

                $transaction_api = new \SquareConnect\Api\TransactionApi();

                $request_body = array (

                  "card_nonce" => $nonce,

                  # Monetary amounts are specified in the smallest unit of the applicable currency.
                  # This amount is in cents. It's also hard-coded for $1, which is not very useful.
                  "amount_money" => array (
                    "amount" => round($amount),
                    "currency" => "USD"
                  ),

                  # Every payment you process for a given business have a unique idempotency key.
                  # If you're unsure whether a particular payment succeeded, you can reattempt
                  # it with the same idempotency key without worrying about double charging
                  # the buyer.
                  "idempotency_key" => uniqid()
                );

                # The SDK throws an exception if a Connect endpoint responds with anything besides 200 (success).
                # This block catches any exceptions that occur from the request.
                try {
                  $payment_sucess = $transaction_api->charge($access_token, $location_id, $request_body);  

                    // Payment has been successful
                    $customer_order->add_order_note( __( 'payment completed.', 'spyr-square-gateway' ) );

                    // Mark order as Paid
                    $customer_order->payment_complete();

                    // Empty the cart (Very important step)
                    $woocommerce->cart->empty_cart();

                    // Redirect to thank you page
                    return array(
                        'result'   => 'success',
                        'redirect' => $this->get_return_url( $customer_order ),
                    );
                } catch (Exception $e) {
                  $error = "Caught exception " . $e->getMessage();
                // Transaction was not succesful
                // Add notice to the cart
                wc_add_notice( $error, 'error' );
                // Add note to the order for your reference
                $customer_order->add_order_note( 'Error: '. $error);
                }

        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法导入com.squareup.okhttp.OkHttpClient;

来自分类Dev

集成 iPad 阅读器时 SquareUp 支付网关问题

来自分类Dev

java.lang.NoClassDefFoundError:com.squareup.picasso.Picasso

来自分类Dev

java.lang.NoClassDefFoundError: com.squareup.picasso.Picasso

来自分类Dev

找不到com.squareup.picasso:picasso:2.5.3-SNAPSHOT

来自分类Dev

java.lang.NoClassDefFoundError:com.squareup.okhttp.MediaType

来自分类Dev

找不到com.squareup.picasso:picasso:2.5.2

来自分类Dev

无法解析com.squareup.sqldelight:native-driver:1.3.0

来自分类Dev

java.lang.NoClassDefFoundError:com.squareup.okhttp.OkHttpClient

来自分类Dev

使用自定义Woocommerce付款网关对“下订单按钮”进行JavaScript覆盖

来自分类Dev

在APK META-INF / maven / com.squareup / otto / pom.xml中复制的重复文件

来自分类Dev

编译com.squareup.retrofit:converter-simplexml:2.0.0-beta2时出错

来自分类Dev

由以下原因引起:java.lang.NoSuchFieldError:com.squareup.okhttp.internal.http.HttpMethod.METHODS

来自分类Dev

如何在GRPC OkHttp版本(1.29.0)中升级com.squareup.okhttp(4.6.0)?

来自分类Dev

OkHttp请求在com.squareup.okhttp中不公开;无法从外部包访问

来自分类Dev

java.lang.NoSuchMethodError:com.squareup.okhttp.internal.Internal.callEngineGetConnection

来自分类Dev

Gradle 无法下载 okhttp.jar (com.squareup.okhttp3:okhttp:3.8.0)

来自分类Dev

WordPress + Woocommerce自定义支付网关

来自分类Dev

WooCommerce自定义支付网关

来自分类Dev

转换为Dalvik格式失败:无法执行dex:多个dex文件定义了Lcom / squareup / okhttp / Address;

来自分类Dev

Nopcommerce - 自定义付款方式的 PostProcessPayment 不重定向到付款网关 URL

来自分类Dev

错误:无法解析':app @ debug / compileClasspath'的依赖项:无法下载okhttp.jar(com.squareup.okhttp3:okhttp:4.3.1)

来自分类Dev

无法从Squareup API收到正确答案

来自分类Dev

不允许使用Squareup iFrame

来自分类Dev

在WooCommerce中禁用基于自定义送货方式的付款方式

来自分类Dev

根据所选付款方式显示隐藏自定义 Woocommerce 结帐字段

来自分类Dev

WordPress插件WooCommerce,自定义支付网关设置未保存

来自分类Dev

Woocommerce自定义支付网关未执行form.submit

来自分类Dev

处理后如何将数据发送回woocommerce自定义网关

Related 相关文章

  1. 1

    无法导入com.squareup.okhttp.OkHttpClient;

  2. 2

    集成 iPad 阅读器时 SquareUp 支付网关问题

  3. 3

    java.lang.NoClassDefFoundError:com.squareup.picasso.Picasso

  4. 4

    java.lang.NoClassDefFoundError: com.squareup.picasso.Picasso

  5. 5

    找不到com.squareup.picasso:picasso:2.5.3-SNAPSHOT

  6. 6

    java.lang.NoClassDefFoundError:com.squareup.okhttp.MediaType

  7. 7

    找不到com.squareup.picasso:picasso:2.5.2

  8. 8

    无法解析com.squareup.sqldelight:native-driver:1.3.0

  9. 9

    java.lang.NoClassDefFoundError:com.squareup.okhttp.OkHttpClient

  10. 10

    使用自定义Woocommerce付款网关对“下订单按钮”进行JavaScript覆盖

  11. 11

    在APK META-INF / maven / com.squareup / otto / pom.xml中复制的重复文件

  12. 12

    编译com.squareup.retrofit:converter-simplexml:2.0.0-beta2时出错

  13. 13

    由以下原因引起:java.lang.NoSuchFieldError:com.squareup.okhttp.internal.http.HttpMethod.METHODS

  14. 14

    如何在GRPC OkHttp版本(1.29.0)中升级com.squareup.okhttp(4.6.0)?

  15. 15

    OkHttp请求在com.squareup.okhttp中不公开;无法从外部包访问

  16. 16

    java.lang.NoSuchMethodError:com.squareup.okhttp.internal.Internal.callEngineGetConnection

  17. 17

    Gradle 无法下载 okhttp.jar (com.squareup.okhttp3:okhttp:3.8.0)

  18. 18

    WordPress + Woocommerce自定义支付网关

  19. 19

    WooCommerce自定义支付网关

  20. 20

    转换为Dalvik格式失败:无法执行dex:多个dex文件定义了Lcom / squareup / okhttp / Address;

  21. 21

    Nopcommerce - 自定义付款方式的 PostProcessPayment 不重定向到付款网关 URL

  22. 22

    错误:无法解析':app @ debug / compileClasspath'的依赖项:无法下载okhttp.jar(com.squareup.okhttp3:okhttp:4.3.1)

  23. 23

    无法从Squareup API收到正确答案

  24. 24

    不允许使用Squareup iFrame

  25. 25

    在WooCommerce中禁用基于自定义送货方式的付款方式

  26. 26

    根据所选付款方式显示隐藏自定义 Woocommerce 结帐字段

  27. 27

    WordPress插件WooCommerce,自定义支付网关设置未保存

  28. 28

    Woocommerce自定义支付网关未执行form.submit

  29. 29

    处理后如何将数据发送回woocommerce自定义网关

热门标签

归档