function DoReferenceTransaction($billing_agreement_id)
 {
     global $order, $order_total_modules;
     global $currencies;
     $method = "DoReferenceTransaction";
     $parm = "";
     $parm .= "&REFERENCEID=" . $billing_agreement_id;
     // 送付先
     $parm .= "&SHIPTONAME=" . paypal_esacpe_parm($order->delivery['firstname'] . ' ' . $order->delivery['lastname']);
     $parm .= "&SHIPTOSTREET=" . paypal_esacpe_parm($order->delivery['street_address']);
     $parm .= "&SHIPTOSTREET2=" . paypal_esacpe_parm($order->delivery['suburb']);
     $parm .= "&SHIPTOCITY=" . paypal_esacpe_parm($order->delivery['city']);
     $parm .= "&SHIPTOSTATE=" . paypal_esacpe_parm($order->delivery['state']);
     $parm .= "&SHIPTOZIP=" . paypal_esacpe_parm($order->delivery['postcode']);
     $parm .= "&SHIPTOCOUNTRYCODE=" . paypal_esacpe_parm($order->delivery['country']['iso_code_2']);
     $parm .= "&SHIPTOPHONENUM=" . paypal_esacpe_parm($order->delivery['telephone']);
     // 通貨
     if (MODULE_PAYMENT_PAYPAL_EC_CURRENCY == 'USER') {
         $amt = paypal_get_ot_total($order, $order_total_modules);
         $currency = $_SESSION['currency'];
     } else {
         $amt = paypal_get_ot_total($order, $order_total_modules, false);
         $currency = MODULE_PAYMENT_PAYPAL_EC_CURRENCY;
         $amt = number_format(paypal_convert_money_string($amt) * $currencies->get_value($currency), $currencies->get_decimal_places($currency));
         $amt = paypal_convert_money_string($amt);
     }
     // 金額
     //    $tax   = paypal_get_ot_tax($order, $order_total_modules);
     $parm .= "&AMT=" . $amt;
     $parm .= "&CURRENCYCODE=" . paypal_esacpe_parm($currency);
     //    $parm .= "&ITEMAMT=".(paypal_get_ot_subtotal($order, $order_total_modules)-$tax);
     //    $parm .= "&SHIPPINGAMT=".paypal_get_ot_shipping($order, $order_total_modules);
     //    $parm .= "&HANDLINGAMT=".paypal_get_ot_loworderfee($order, $order_total_modules);
     //    $parm .= "&SHIPDISCAMT=".paypal_get_ot_coupon($order, $order_total_modules);
     //    $parm .= "&TAXAMT=".$tax;
     $parm .= "&PAYMENTACTION=" . MODULE_PAYMENT_PAYPAL_EC_SETTLEMENT_TYPE;
     $parm .= "&L_PAYMENTREQUEST_0_NAME0=" . paypal_esacpe_parm(MODULE_PAYMENT_PAYPAL_TEXT_ITEMNAME);
     $parm .= "&L_PAYMENTREQUEST_0_AMT0=" . $amt;
     $parm .= "&L_PAYMENTREQUEST_0_QTY0=1";
     // 商品情報
     //    $no = 0;
     //    foreach($order->products as $product) {
     //      $tax   = 0;//$product['final_price']*$product['tax']/100;
     //      $parm .= "&L_NAME".$no."=".paypal_esacpe_parm($product['name']);
     //      $parm .= "&L_AMT".$no."=".($product['final_price']+$tax);
     //      $parm .= "&L_NUMBER".$no."=".paypal_esacpe_parm($product['model']);
     //      $parm .= "&L_QTY".$no."=".$product['qty'];
     //      $no++;
     //    }
     // API呼び出し
     $resArray = paypal_hash_call($method, $parm);
     $ack = strtoupper($resArray["ACK"]);
     if ($ack == "SUCCESS") {
         // do nothing
     } else {
         global $messageStack;
         $messageStack->add_session('checkout_payment', $resArray['L_LONGMESSAGE0'], 'error');
         zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, "", "SSL"));
         exit;
     }
 }
 function CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL)
 {
     global $wpdb;
     $nvpstr = '';
     $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
     $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
     $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
     $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
     $data = array();
     if (!isset($this->cart_data['shipping_address']['first_name']) && !isset($this->cart_data['shipping_address']['last_name'])) {
         $this->cart_data['shipping_address']['first_name'] = $this->cart_data['billing_address']['first_name'];
         $this->cart_data['shipping_address']['last_name'] = $this->cart_data['billing_address']['last_name'];
     }
     if ($this->cart_data['shipping_address']['country'] == 'UK') {
         $this->cart_data['shipping_address']['country'] = 'GB';
     }
     $data += array('PAYMENTREQUEST_0_SHIPTONAME' => $this->cart_data['shipping_address']['first_name'] . ' ' . $this->cart_data['shipping_address']['last_name'], 'PAYMENTREQUEST_0_SHIPTOSTREET' => $this->cart_data['shipping_address']['address'], 'PAYMENTREQUEST_0_SHIPTOCITY' => $this->cart_data['shipping_address']['city'], 'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => $this->cart_data['shipping_address']['country'], 'PAYMENTREQUEST_0_SHIPTOZIP' => $this->cart_data['shipping_address']['post_code']);
     if (!empty($this->cart_data['shipping_address']['state'])) {
         $data += array('PAYMENTREQUEST_0_SHIPTOSTATE' => $this->cart_data['shipping_address']['state']);
     }
     $i = 0;
     $item_total = 0;
     $tax_total = 0;
     $shipping_total = 0;
     $is_free_shipping = false;
     if ($this->cart_data['has_discounts'] && (double) $this->cart_data['cart_discount_value'] > 0) {
         $coupon = new wpsc_coupons($this->cart_data['cart_discount_coupon']);
         $is_free_shipping = $coupon->is_free_shipping();
     }
     foreach ($this->cart_items as $cart_item) {
         $data["L_PAYMENTREQUEST_0_NAME{$i}"] = urlencode(apply_filters('the_title', $cart_item['name']));
         $data["L_PAYMENTREQUEST_0_AMT{$i}"] = $this->convert($cart_item['price']);
         $data["L_PAYMENTREQUEST_0_NUMBER{$i}"] = $i;
         $data["L_PAYMENTREQUEST_0_QTY{$i}"] = $cart_item['quantity'];
         $item_total += $this->convert($cart_item['price']) * $cart_item['quantity'];
         $shipping_total += $cart_item['shipping'];
         $i++;
     }
     //if we have a discount then include a negative amount with that discount
     // in php 0.00 = true so we will change that here
     if ($this->cart_data['cart_discount_value'] == 0.0) {
         $this->cart_data['cart_discount_value'] = 0;
     }
     $discount_value = $this->convert($this->cart_data['cart_discount_value']);
     if ($this->cart_data['cart_discount_value'] && !$is_free_shipping) {
         // if item total < discount amount, leave at least 0.01 unit in item total, then subtract
         // 0.01 from shipping as well
         if (!$is_free_shipping && $discount_value >= $item_total) {
             $discount_value = $item_total - 0.01;
             $shipping_total -= 0.01;
         }
         $item_total -= $discount_value;
         $data["L_PAYMENTREQUEST_0_NAME{$i}"] = "Discount / Coupon";
         $data["L_PAYMENTREQUEST_0_AMT{$i}"] = -$discount_value;
         $data["L_PAYMENTREQUEST_0_NUMBER{$i}"] = $i;
         $data["L_PAYMENTREQUEST_0_QTY{$i}"] = 1;
     }
     $data["PAYMENTREQUEST_0_ITEMAMT"] = $this->format_price($item_total);
     if ($discount_value && $is_free_shipping) {
         $data["PAYMENTREQUEST_0_SHIPPINGAMT"] = 0;
     } else {
         $data["PAYMENTREQUEST_0_SHIPPINGAMT"] = $this->convert($this->cart_data['base_shipping'] + $shipping_total);
     }
     $total = $data["PAYMENTREQUEST_0_ITEMAMT"] + $data["PAYMENTREQUEST_0_SHIPPINGAMT"];
     if (!wpsc_tax_isincluded()) {
         $data["PAYMENTREQUEST_0_TAXAMT"] = $this->convert($this->cart_data['cart_tax']);
         $total += $data["PAYMENTREQUEST_0_TAXAMT"];
     }
     // adjust total amount in case we had to round up after converting currency
     // or discount calculation
     if ($total != $paymentAmount) {
         $paymentAmount = $total;
     }
     $data["PAYMENTREQUEST_0_AMT"] = $paymentAmount;
     if (count($data) >= 4) {
         $temp_data = array();
         foreach ($data as $key => $value) {
             $temp_data[] = $key . "=" . $value;
         }
         $nvpstr = $nvpstr . "&" . implode("&", $temp_data);
     }
     wpsc_update_customer_meta('paypal_express_currency_code_type', $currencyCodeType);
     wpsc_update_customer_meta('paypal_express_payment_type', $paymentType);
     $resArray = paypal_hash_call("SetExpressCheckout", $nvpstr);
     $ack = strtoupper($resArray["ACK"]);
     if ($ack == "SUCCESS") {
         $token = urldecode($resArray["TOKEN"]);
         wpsc_update_customer_meta('paypal_express_token', $token);
     }
     return $resArray;
 }
 function CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL)
 {
     global $wpdb;
     $nvpstr = "&Amt=" . $paymentAmount;
     $nvpstr = $nvpstr . "&PAYMENTACTION=" . $paymentType;
     $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL;
     $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL;
     $nvpstr = $nvpstr . "&CURRENCYCODE=" . $currencyCodeType;
     $data = array();
     if (!isset($this->cart_data['shipping_address']['first_name']) && !isset($this->cart_data['shipping_address']['last_name'])) {
         $this->cart_data['shipping_address']['first_name'] = $this->cart_data['billing_address']['first_name'];
         $this->cart_data['shipping_address']['last_name'] = $this->cart_data['billing_address']['last_name'];
     }
     $data += array('SHIPTONAME' => $this->cart_data['shipping_address']['first_name'] . ' ' . $this->cart_data['shipping_address']['last_name'], 'SHIPTOSTREET' => $this->cart_data['shipping_address']['address'], 'SHIPTOCITY' => $this->cart_data['shipping_address']['city'], 'SHIPTOCOUNTRYCODE' => $this->cart_data['shipping_address']['country'], 'SHIPTOZIP' => $this->cart_data['shipping_address']['post_code']);
     if ('' != $this->cart_data['shipping_address']['state']) {
         $data += array('SHIPTOSTATE' => $this->cart_data['shipping_address']['state']);
     }
     if (count($data) >= 4) {
         $temp_data = array();
         foreach ($data as $key => $value) {
             $temp_data[] = $key . "=" . $value;
         }
         $nvpstr = $nvpstr . "&" . implode("&", $temp_data);
     }
     $_SESSION["currencyCodeType"] = $currencyCodeType;
     $_SESSION["PaymentType"] = $paymentType;
     $resArray = paypal_hash_call("SetExpressCheckout", $nvpstr);
     $ack = strtoupper($resArray["ACK"]);
     if ($ack == "SUCCESS") {
         $token = urldecode($resArray["TOKEN"]);
         $_SESSION['token'] = $token;
     }
     return $resArray;
 }